Example usage for com.amazonaws.services.sqs.model SendMessageBatchRequestEntry setMessageBody

List of usage examples for com.amazonaws.services.sqs.model SendMessageBatchRequestEntry setMessageBody

Introduction

In this page you can find the example usage for com.amazonaws.services.sqs.model SendMessageBatchRequestEntry setMessageBody.

Prototype


public void setMessageBody(String messageBody) 

Source Link

Document

The body of the message.

Usage

From source file:com.amazon.sqs.javamessaging.AmazonSQSExtendedClient.java

License:Open Source License

private SendMessageBatchRequestEntry storeMessageInS3(SendMessageBatchRequestEntry batchEntry) {

    checkMessageAttributes(batchEntry.getMessageAttributes());

    String s3Key = UUID.randomUUID().toString();

    // Read the content of the message from message body
    String messageContentStr = batchEntry.getMessageBody();

    Long messageContentSize = getStringSizeInBytes(messageContentStr);

    // Add a new message attribute as a flag
    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setDataType("Number");
    messageAttributeValue.setStringValue(messageContentSize.toString());
    batchEntry.addMessageAttributesEntry(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME,
            messageAttributeValue);/*  w  ww. j av  a 2 s .c  om*/

    // Store the message content in S3.
    storeTextInS3(s3Key, messageContentStr, messageContentSize);

    LOG.info("S3 object created, Bucket name: " + clientConfiguration.getS3BucketName() + ", Object key: "
            + s3Key + ".");

    // Convert S3 pointer (bucket name, key, etc) to JSON string
    MessageS3Pointer s3Pointer = new MessageS3Pointer(clientConfiguration.getS3BucketName(), s3Key);
    String s3PointerStr = getJSONFromS3Pointer(s3Pointer);

    // Storing S3 pointer in the message body.
    batchEntry.setMessageBody(s3PointerStr);

    return batchEntry;
}

From source file:org.apache.nifi.processors.aws.sqs.PutSQS.java

License:Apache License

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    FlowFile flowFile = session.get();/*w w  w .  j  a va  2s  .c  o m*/
    if (flowFile == null) {
        return;
    }

    final long startNanos = System.nanoTime();
    final AmazonSQSClient client = getClient();
    final SendMessageBatchRequest request = new SendMessageBatchRequest();
    final String queueUrl = context.getProperty(QUEUE_URL).evaluateAttributeExpressions(flowFile).getValue();
    request.setQueueUrl(queueUrl);

    final Set<SendMessageBatchRequestEntry> entries = new HashSet<>();

    final SendMessageBatchRequestEntry entry = new SendMessageBatchRequestEntry();
    entry.setId(flowFile.getAttribute("uuid"));
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    session.exportTo(flowFile, baos);
    final String flowFileContent = baos.toString();
    entry.setMessageBody(flowFileContent);

    final Map<String, MessageAttributeValue> messageAttributes = new HashMap<>();

    for (final PropertyDescriptor descriptor : userDefinedProperties) {
        final MessageAttributeValue mav = new MessageAttributeValue();
        mav.setDataType("String");
        mav.setStringValue(context.getProperty(descriptor).evaluateAttributeExpressions(flowFile).getValue());
        messageAttributes.put(descriptor.getName(), mav);
    }

    entry.setMessageAttributes(messageAttributes);
    entry.setDelaySeconds(context.getProperty(DELAY).asTimePeriod(TimeUnit.SECONDS).intValue());
    entries.add(entry);

    request.setEntries(entries);

    try {
        client.sendMessageBatch(request);
    } catch (final Exception e) {
        getLogger().error("Failed to send messages to Amazon SQS due to {}; routing to failure",
                new Object[] { e });
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
        return;
    }

    getLogger().info("Successfully published message to Amazon SQS for {}", new Object[] { flowFile });
    session.transfer(flowFile, REL_SUCCESS);
    final long transmissionMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
    session.getProvenanceReporter().send(flowFile, queueUrl, transmissionMillis);
}