Example usage for com.amazonaws.services.sqs.model MessageAttributeValue setStringValue

List of usage examples for com.amazonaws.services.sqs.model MessageAttributeValue setStringValue

Introduction

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

Prototype


public void setStringValue(String stringValue) 

Source Link

Document

Strings are Unicode with UTF-8 binary encoding.

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);//from  w ww  .  java2 s.  c o  m

    // 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:com.amazon.sqs.javamessaging.AmazonSQSExtendedClient.java

License:Open Source License

private SendMessageRequest storeMessageInS3(SendMessageRequest sendMessageRequest) {

    checkMessageAttributes(sendMessageRequest.getMessageAttributes());

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

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

    Long messageContentSize = getStringSizeInBytes(messageContentStr);

    // Add a new message attribute as a flag
    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setDataType("Number");
    messageAttributeValue.setStringValue(messageContentSize.toString());
    sendMessageRequest.addMessageAttributesEntry(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME,
            messageAttributeValue);// w  w  w  .  j  ava  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.
    sendMessageRequest.setMessageBody(s3PointerStr);

    return sendMessageRequest;
}

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

License:Open Source License

/**
 * Not verified on the client side, but SQS Attribute names must be valid
 * letter or digit on the basic multilingual plane in addition to allowing
 * '_', '-' and '.'. No component of an attribute name may be empty, thus an
 * attribute name may neither start nor end in '.'. And it may not contain
 * ".."./* ww  w.  jav a  2s  .  c o m*/
 */
Map<String, MessageAttributeValue> propertyToMessageAttribute(SQSMessage message) throws JMSException {
    Map<String, MessageAttributeValue> messageAttributes = new HashMap<String, MessageAttributeValue>();
    Enumeration<String> propertyNames = message.getPropertyNames();

    while (propertyNames.hasMoreElements()) {
        String propertyName = propertyNames.nextElement();

        // This is generated from SQS message attribute "ApproximateReceiveCount"
        if (propertyName.equals(SQSMessagingClientConstants.JMSX_DELIVERY_COUNT)) {
            continue;
        }
        JMSMessagePropertyValue propertyObject = message.getJMSMessagePropertyValue(propertyName);
        MessageAttributeValue messageAttributeValue = new MessageAttributeValue();

        messageAttributeValue.setDataType(propertyObject.getType());
        messageAttributeValue.setStringValue(propertyObject.getStringMessageAttributeValue());

        messageAttributes.put(propertyName, messageAttributeValue);
    }
    return messageAttributes;
}

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

License:Open Source License

/**
 * Adds the message type attribute during send as part of the send message
 * request./*from  ww  w .j av a2 s.c o  m*/
 */
private void addMessageTypeReservedAttribute(Map<String, MessageAttributeValue> messageAttributes,
        SQSMessage message, String value) throws JMSException {

    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();

    messageAttributeValue.setDataType(SQSMessagingClientConstants.STRING);
    messageAttributeValue.setStringValue(value);

    /**
     * This will override the existing attribute if exists. Everything that
     * has prefix JMS_ is reserved for JMS Provider, but if the user sets that
     * attribute, it will be overwritten.
     */
    messageAttributes.put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);
}

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();//ww w .  j a  v a 2 s.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);
}