Java Code Examples for com.amazonaws.services.sqs.model.MessageAttributeValue#setDataType()

The following examples show how to use com.amazonaws.services.sqs.model.MessageAttributeValue#setDataType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SQSMessageProducerTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
private Map<String, MessageAttributeValue> createMessageAttribute(String type) {
    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setDataType("String");
    messageAttributeValue.setStringValue(type);

    Map<String, MessageAttributeValue> messageAttributes = new HashMap<String, MessageAttributeValue>();
    messageAttributes.put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);
    return messageAttributes;
}
 
Example 2
Source File: SQSMessageConsumerPrefetchTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test ConvertToJMSMessage with text message with text type attribute
 */
@Test
public void testConvertToJMSMessageTextTypeAttribute() throws JMSException, IOException {

    /*
     * Set up consumer prefetch and mocks
     */

    Map<String,MessageAttributeValue> mapMessageAttributes = new HashMap<String, MessageAttributeValue>();
    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setStringValue(SQSMessage.TEXT_MESSAGE_TYPE);
    messageAttributeValue.setDataType(SQSMessagingClientConstants.STRING);
    mapMessageAttributes.put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);

    Map<String, String> mapAttributes = new HashMap<String, String>();
    mapAttributes.put(SQSMessagingClientConstants.APPROXIMATE_RECEIVE_COUNT, "1");

    com.amazonaws.services.sqs.model.Message message = mock(com.amazonaws.services.sqs.model.Message.class);
    // Return message attributes with message type 'TEXT'
    when(message.getMessageAttributes()).thenReturn(mapMessageAttributes);
    when(message.getAttributes()).thenReturn(mapAttributes);
    when(message.getBody()).thenReturn("MessageBody");

    /*
     * Convert the SQS message to JMS Message
     */
    javax.jms.Message jsmMessage = consumerPrefetch.convertToJMSMessage(message);

    /*
     * Verify results
     */
    assertTrue(jsmMessage instanceof SQSTextMessage);
    assertEquals(message.getBody(), "MessageBody");
}
 
Example 3
Source File: SQSMessageConsumerPrefetchTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test ConvertToJMSMessage with an object message that contains illegal sqs message body
 */
@Test
public void testConvertToJMSMessageObjectIllegalBody() throws JMSException, IOException {

    /*
     * Set up consumer prefetch and mocks
     */

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

    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setStringValue(SQSMessage.OBJECT_MESSAGE_TYPE);
    messageAttributeValue.setDataType(SQSMessagingClientConstants.STRING);
    mapMessageAttributes.put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);

    Map<String, String> mapAttributes = new HashMap<String, String>();
    mapAttributes.put(SQSMessagingClientConstants.APPROXIMATE_RECEIVE_COUNT, "1");

    com.amazonaws.services.sqs.model.Message message = mock(com.amazonaws.services.sqs.model.Message.class);
    // Return message attributes with message type 'OBJECT'
    when(message.getMessageAttributes()).thenReturn(mapMessageAttributes);
    when(message.getAttributes()).thenReturn(mapAttributes);
    when(message.getBody()).thenReturn("Some text that does not represent an object");

    /*
     * Convert the SQS message to JMS Message
     */
    ObjectMessage jsmMessage = (ObjectMessage) consumerPrefetch.convertToJMSMessage(message);

    /*
     * Verify results
     */
    try {
        jsmMessage.getObject();
        fail("Expect JMSException");
    } catch (JMSException jmse) {
        // Expected JMS exception
    }
}
 
Example 4
Source File: SQSMessageConsumerPrefetchTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test ConvertToJMSMessage with byte message that contains illegal sqs message body
 */
@Test
public void testConvertToJMSMessageByteTypeIllegalBody() throws JMSException, IOException {

    /*
     * Set up consumer prefetch and mocks
     */

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

    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setStringValue(SQSMessage.BYTE_MESSAGE_TYPE);
    messageAttributeValue.setDataType(SQSMessagingClientConstants.STRING);
    mapMessageAttributes.put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);

    Map<String, String> mapAttributes = new HashMap<String, String>();
    mapAttributes.put(SQSMessagingClientConstants.APPROXIMATE_RECEIVE_COUNT, "1");

    com.amazonaws.services.sqs.model.Message message = mock(com.amazonaws.services.sqs.model.Message.class);

    // Return message attributes with message type 'BYTE'
    when(message.getMessageAttributes()).thenReturn(mapMessageAttributes);
    when(message.getAttributes()).thenReturn(mapAttributes);
    // Return illegal message body for byte message type
    when(message.getBody()).thenReturn("Text Message");

    /*
     * Convert the SQS message to JMS Message
     */
    try {
        consumerPrefetch.convertToJMSMessage(message);
        fail("Expect JMSException");
    } catch (JMSException jmse) {
        // Expected JMS exception
    }
}
 
Example 5
Source File: SQSMessageConsumerPrefetchTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test ConvertToJMSMessage with byte message type
 */
@Test
public void testConvertToJMSMessageByteTypeAttribute() throws JMSException, IOException {

    /*
     * Set up consumer prefetch and mocks
     */

    Map<String,MessageAttributeValue> mapMessageAttributes = new HashMap<String, MessageAttributeValue>();
    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setStringValue(SQSMessage.BYTE_MESSAGE_TYPE);
    messageAttributeValue.setDataType(SQSMessagingClientConstants.STRING);
    mapMessageAttributes.put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);

    Map<String, String> mapAttributes = new HashMap<String, String>();
    mapAttributes.put(SQSMessagingClientConstants.APPROXIMATE_RECEIVE_COUNT, "1");

    com.amazonaws.services.sqs.model.Message message = mock(com.amazonaws.services.sqs.model.Message.class);

    // Return message attributes with message type 'BYTE'
    when(message.getMessageAttributes()).thenReturn(mapMessageAttributes);
    when(message.getAttributes()).thenReturn(mapAttributes);

    byte[] byteArray = new byte[] { 1, 0, 'a', 65 };
    when(message.getBody()).thenReturn(Base64.encodeAsString(byteArray));

    /*
     * Convert the SQS message to JMS Message
     */
    javax.jms.Message jsmMessage = consumerPrefetch.convertToJMSMessage(message);

    /*
     * Verify results
     */
    assertTrue(jsmMessage instanceof SQSBytesMessage);
    for (byte b : byteArray) {
        assertEquals(b, ((SQSBytesMessage)jsmMessage).readByte());
    }
}
 
Example 6
Source File: SQSMessageProducerFifoTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test sendInternal input with SQSByteMessage
 */
@Test
public void testSendInternalSQSByteMessageFromReceivedMessage() throws JMSException, IOException {
    
    /*
     * Set up non JMS sqs message
     */
    Map<String,MessageAttributeValue> mapMessageAttributes = new HashMap<String, MessageAttributeValue>();
    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setStringValue(SQSMessage.BYTE_MESSAGE_TYPE);
    messageAttributeValue.setDataType(SQSMessagingClientConstants.STRING);
    mapMessageAttributes.put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);

    Map<String, String> mapAttributes = new HashMap<String, String>();
    mapAttributes.put(SQSMessagingClientConstants.APPROXIMATE_RECEIVE_COUNT, "1");
    mapAttributes.put(SQSMessagingClientConstants.MESSAGE_GROUP_ID, GROUP_ID);
    mapAttributes.put(SQSMessagingClientConstants.MESSAGE_DEDUPLICATION_ID, DEDUP_ID);
    mapAttributes.put(SQSMessagingClientConstants.SEQUENCE_NUMBER, SEQ_NUMBER);

    byte[] byteArray = new byte[] { 1, 0, 'a', 65 };
    String messageBody = Base64.encodeAsString(byteArray);
    com.amazonaws.services.sqs.model.Message message =
            new com.amazonaws.services.sqs.model.Message()
                    .withMessageAttributes(mapMessageAttributes)
                    .withAttributes(mapAttributes)
                    .withBody(messageBody);

    SQSBytesMessage msg = spy(new SQSBytesMessage(acknowledger, QUEUE_URL, message));

    when(amazonSQSClient.sendMessage(any(SendMessageRequest.class)))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID).withSequenceNumber(SEQ_NUMBER_2));

    producer.sendInternal(destination, msg);

    verify(amazonSQSClient).sendMessage(argThat(new sendMessageRequestMatcher(QUEUE_URL, messageBody, SQSMessage.BYTE_MESSAGE_TYPE, GROUP_ID, DEDUP_ID)));
    verify(msg).setJMSDestination(destination);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID);
    verify(msg).setSQSMessageId(MESSAGE_ID);
    verify(msg).setSequenceNumber(SEQ_NUMBER_2);
}
 
Example 7
Source File: SQSMessageProducerFifoTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test sendInternal input with SQSTextMessage
 */
@Test
public void testSendInternalSQSTextMessageFromReceivedMessage() throws JMSException {

    /*
     * Set up non JMS sqs message
     */
    Map<String,MessageAttributeValue> mapMessageAttributes = new HashMap<String, MessageAttributeValue>();
    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setStringValue(SQSMessage.TEXT_MESSAGE_TYPE);
    messageAttributeValue.setDataType(SQSMessagingClientConstants.STRING);
    mapMessageAttributes.put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);

    Map<String, String> mapAttributes = new HashMap<String, String>();
    mapAttributes.put(SQSMessagingClientConstants.APPROXIMATE_RECEIVE_COUNT, "1");
    mapAttributes.put(SQSMessagingClientConstants.MESSAGE_GROUP_ID, GROUP_ID);
    mapAttributes.put(SQSMessagingClientConstants.MESSAGE_DEDUPLICATION_ID, DEDUP_ID);
    mapAttributes.put(SQSMessagingClientConstants.SEQUENCE_NUMBER, SEQ_NUMBER);

    com.amazonaws.services.sqs.model.Message message =
            new com.amazonaws.services.sqs.model.Message()
                        .withMessageAttributes(mapMessageAttributes)
                        .withAttributes(mapAttributes)
                        .withBody("MessageBody");

    SQSTextMessage msg = spy(new SQSTextMessage(acknowledger, QUEUE_URL, message));

    when(amazonSQSClient.sendMessage(any(SendMessageRequest.class)))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID).withSequenceNumber(SEQ_NUMBER_2));

    producer.sendInternal(destination, msg);

    verify(amazonSQSClient).sendMessage(argThat(new sendMessageRequestMatcher(QUEUE_URL, "MessageBody", SQSMessage.TEXT_MESSAGE_TYPE, GROUP_ID, DEDUP_ID)));
    verify(msg).setJMSDestination(destination);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID);
    verify(msg).setSQSMessageId(MESSAGE_ID);
    verify(msg).setSequenceNumber(SEQ_NUMBER_2);
}
 
Example 8
Source File: SQSMessageProducerTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test sendInternal input with SQSByteMessage
 */
@Test
public void testSendInternalSQSByteMessageFromReceivedMessage() throws JMSException, IOException {
    
    /*
     * Set up non JMS sqs message
     */
    Map<String,MessageAttributeValue> mapMessageAttributes = new HashMap<String, MessageAttributeValue>();
    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setStringValue(SQSMessage.BYTE_MESSAGE_TYPE);
    messageAttributeValue.setDataType(SQSMessagingClientConstants.STRING);
    mapMessageAttributes.put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);

    Map<String, String> mapAttributes = new HashMap<String, String>();
    mapAttributes.put(SQSMessagingClientConstants.APPROXIMATE_RECEIVE_COUNT, "1");

    byte[] byteArray = new byte[] { 1, 0, 'a', 65 };
    String messageBody = Base64.encodeAsString(byteArray);
    com.amazonaws.services.sqs.model.Message message =
            new com.amazonaws.services.sqs.model.Message()
                    .withMessageAttributes(mapMessageAttributes)
                    .withAttributes(mapAttributes)
                    .withBody(messageBody);

    SQSObjectMessage msg = spy(new SQSObjectMessage(acknowledger, QUEUE_URL, message));

    Map<String, MessageAttributeValue> messageAttributes = createMessageAttribute("object");

    when(amazonSQSClient.sendMessage(any(SendMessageRequest.class)))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID_1))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID_2));

    producer.sendInternal(destination, msg);

    verify(amazonSQSClient).sendMessage(argThat(new sendMessageRequestMatcher(QUEUE_URL, Arrays.asList(messageBody),
            messageAttributes)));
    verify(msg).setJMSDestination(destination);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID_1);
    verify(msg).setSQSMessageId(MESSAGE_ID_1);
}
 
Example 9
Source File: SQSMessageProducerTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test sendInternal input with SQSTextMessage
 */
@Test
public void testSendInternalSQSTextMessageFromReceivedMessage() throws JMSException {

    /*
     * Set up non JMS sqs message
     */
    Map<String,MessageAttributeValue> mapMessageAttributes = new HashMap<String, MessageAttributeValue>();
    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setStringValue(SQSMessage.TEXT_MESSAGE_TYPE);
    messageAttributeValue.setDataType(SQSMessagingClientConstants.STRING);
    mapMessageAttributes.put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);

    Map<String, String> mapAttributes = new HashMap<String, String>();
    mapAttributes.put(SQSMessagingClientConstants.APPROXIMATE_RECEIVE_COUNT, "1");

    com.amazonaws.services.sqs.model.Message message =
            new com.amazonaws.services.sqs.model.Message()
                        .withMessageAttributes(mapMessageAttributes)
                        .withAttributes(mapAttributes)
                        .withBody("MessageBody");

    SQSTextMessage msg = spy(new SQSTextMessage(acknowledger, QUEUE_URL, message));

    when(amazonSQSClient.sendMessage(any(SendMessageRequest.class)))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID_1));

    producer.sendInternal(destination, msg);

    List<String> messagesBody = Arrays.asList("MessageBody");
    verify(amazonSQSClient).sendMessage(argThat(new sendMessageRequestMatcher(QUEUE_URL, messagesBody, mapMessageAttributes)));
    verify(msg).setJMSDestination(destination);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID_1);
    verify(msg).setSQSMessageId(MESSAGE_ID_1);
}
 
Example 10
Source File: SQSMessageProducerTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Test propertyToMessageAttribute with empty messages of different type
 */
@Test
public void testPropertyToMessageAttributeWithEmpty() throws JMSException {

    /*
     * Test Empty text message default attribute
     */
    SQSMessage sqsText = new SQSTextMessage();
    Map<String, MessageAttributeValue> messageAttributeText = producer.propertyToMessageAttribute(sqsText);

    assertEquals(0, messageAttributeText.size());

    /*
     * Test Empty object message default attribute
     */
    SQSMessage sqsObject = new SQSObjectMessage();
    Map<String, MessageAttributeValue> messageAttributeObject = producer.propertyToMessageAttribute(sqsObject);

    assertEquals(0, messageAttributeObject.size());

    /*
     * Test Empty byte message default attribute
     */
    MessageAttributeValue messageAttributeValueByte = new MessageAttributeValue();
    messageAttributeValueByte.setDataType("String");
    messageAttributeValueByte.setStringValue("byte");

    SQSMessage sqsByte = new SQSBytesMessage();
    Map<String, MessageAttributeValue> messageAttributeByte = producer.propertyToMessageAttribute(sqsByte);

    assertEquals(0, messageAttributeObject.size());
}
 
Example 11
Source File: SQSMessageProducer.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Convenience method for adding a single string attribute.
 */
private void addStringAttribute(Map<String, MessageAttributeValue> messageAttributes,
                                String key, String value) {
    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setDataType(SQSMessagingClientConstants.STRING);
    messageAttributeValue.setStringValue(value);
    messageAttributes.put(key, messageAttributeValue);
}
 
Example 12
Source File: SQSMessageProducer.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * 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
 * "..".
 */
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;
        }

        // This property will be used as DeduplicationId argument of SendMessage call
        // On receive it is mapped back to this JMS property
        if (propertyName.equals(SQSMessagingClientConstants.JMS_SQS_DEDUPLICATION_ID)) {
            continue;
        }

        // the JMSXGroupID and JMSXGroupSeq are always stored as message
        // properties, so they are not lost between send and receive
        // even though SQS Classic does not respect those values when returning messages
        // and SQS FIFO has a different understanding of message groups

        JMSMessagePropertyValue propertyObject = message.getJMSMessagePropertyValue(propertyName);
        MessageAttributeValue messageAttributeValue = new MessageAttributeValue();

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

        messageAttributes.put(propertyName, messageAttributeValue);
    }
    return messageAttributes;
}
 
Example 13
Source File: AmazonSQSExtendedClient.java    From amazon-sqs-java-extended-client-lib with Apache License 2.0 5 votes vote down vote up
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);

		// 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;
	}
 
Example 14
Source File: AmazonSQSExtendedClient.java    From amazon-sqs-java-extended-client-lib with Apache License 2.0 5 votes vote down vote up
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);

		// 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;
	}
 
Example 15
Source File: SQSMessageProducerTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 4 votes vote down vote up
/**
 * Test sendInternal input with SQSObjectMessage
 */
@Test
public void testSendInternalSQSObjectMessageFromReceivedMessage() throws JMSException, IOException {

    /*
     * Set up non JMS sqs message
     */
    Map<String,MessageAttributeValue> mapMessageAttributes = new HashMap<String, MessageAttributeValue>();

    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setStringValue(SQSMessage.OBJECT_MESSAGE_TYPE);
    messageAttributeValue.setDataType(SQSMessagingClientConstants.STRING);
    mapMessageAttributes.put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);

    Map<String, String> mapAttributes = new HashMap<String, String>();
    mapAttributes.put(SQSMessagingClientConstants.APPROXIMATE_RECEIVE_COUNT, "1");

    // Encode an object to byte array
    Integer integer = new Integer("10");
    ByteArrayOutputStream array = new ByteArrayOutputStream(10);
    ObjectOutputStream oStream = new ObjectOutputStream(array);
    oStream.writeObject(integer);
    oStream.close();

    String messageBody = Base64.encodeAsString(array.toByteArray());
    com.amazonaws.services.sqs.model.Message message =
            new com.amazonaws.services.sqs.model.Message()
                    .withMessageAttributes(mapMessageAttributes)
                    .withAttributes(mapAttributes)
                    .withBody(messageBody);

    SQSObjectMessage msg = spy(new SQSObjectMessage(acknowledger, QUEUE_URL, message));

    Map<String, MessageAttributeValue> messageAttributes = createMessageAttribute("object");

    when(amazonSQSClient.sendMessage(any(SendMessageRequest.class)))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID_1))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID_2));

    producer.sendInternal(destination, msg);

    verify(amazonSQSClient).sendMessage(argThat(new sendMessageRequestMatcher(QUEUE_URL, Arrays.asList(messageBody),
            messageAttributes)));
    verify(msg).setJMSDestination(destination);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID_1);
    verify(msg).setSQSMessageId(MESSAGE_ID_1);
}
 
Example 16
Source File: PutSQS.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    FlowFile flowFile = session.get();
    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);
}
 
Example 17
Source File: SQSMessageProducerFifoTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 4 votes vote down vote up
public void internalTestPropertyToMessageAttribute(SQSMessage sqsText) throws JMSException {

        /*
         * Setup JMS message property
         */
        String booleanProperty = "BooleanProperty";
        String byteProperty = "ByteProperty";
        String shortProperty = "ShortProperty";
        String intProperty = "IntProperty";
        String longProperty = "LongProperty";
        String floatProperty = "FloatProperty";
        String doubleProperty = "DoubleProperty";
        String stringProperty = "StringProperty";
        String objectProperty = "ObjectProperty";

        sqsText.setBooleanProperty(booleanProperty, true);
        sqsText.setByteProperty(byteProperty, (byte)1);
        sqsText.setShortProperty(shortProperty, (short) 2);
        sqsText.setIntProperty(intProperty, 3);
        sqsText.setLongProperty(longProperty, 4L);
        sqsText.setFloatProperty(floatProperty, (float)5.0);
        sqsText.setDoubleProperty(doubleProperty, 6.0);
        sqsText.setStringProperty(stringProperty, "seven");
        sqsText.setObjectProperty(objectProperty, new Integer(8));

        MessageAttributeValue messageAttributeValueBoolean = new MessageAttributeValue();
        messageAttributeValueBoolean.setDataType("Number.Boolean");
        messageAttributeValueBoolean.setStringValue("1");

        MessageAttributeValue messageAttributeValueByte = new MessageAttributeValue();
        messageAttributeValueByte.setDataType("Number.byte");
        messageAttributeValueByte.setStringValue("1");

        MessageAttributeValue messageAttributeValueShort = new MessageAttributeValue();
        messageAttributeValueShort.setDataType("Number.short");
        messageAttributeValueShort.setStringValue("2");

        MessageAttributeValue messageAttributeValueInt = new MessageAttributeValue();
        messageAttributeValueInt.setDataType("Number.int");
        messageAttributeValueInt.setStringValue("3");

        MessageAttributeValue messageAttributeValueLong = new MessageAttributeValue();
        messageAttributeValueLong.setDataType("Number.long");
        messageAttributeValueLong.setStringValue("4");

        MessageAttributeValue messageAttributeValueFloat = new MessageAttributeValue();
        messageAttributeValueFloat.setDataType("Number.float");
        messageAttributeValueFloat.setStringValue("5.0");

        MessageAttributeValue messageAttributeValueDouble = new MessageAttributeValue();
        messageAttributeValueDouble.setDataType("Number.double");
        messageAttributeValueDouble.setStringValue("6.0");

        MessageAttributeValue messageAttributeValueString = new MessageAttributeValue();
        messageAttributeValueString.setDataType("String");
        messageAttributeValueString.setStringValue("seven");

        MessageAttributeValue messageAttributeValueObject = new MessageAttributeValue();
        messageAttributeValueObject.setDataType("Number.int");
        messageAttributeValueObject.setStringValue("8");

        /*
         * Convert property to sqs message attribute
         */
        Map<String, MessageAttributeValue> messageAttribute = producer.propertyToMessageAttribute(sqsText);

        /*
         * Verify results
         */
        assertEquals(messageAttributeValueBoolean, messageAttribute.get(booleanProperty));
        assertEquals(messageAttributeValueByte, messageAttribute.get(byteProperty));
        assertEquals(messageAttributeValueShort, messageAttribute.get(shortProperty));
        assertEquals(messageAttributeValueInt, messageAttribute.get(intProperty));
        assertEquals(messageAttributeValueLong, messageAttribute.get(longProperty));
        assertEquals(messageAttributeValueFloat, messageAttribute.get(floatProperty));
        assertEquals(messageAttributeValueDouble, messageAttribute.get(doubleProperty));
        assertEquals(messageAttributeValueString, messageAttribute.get(stringProperty));
        assertEquals(messageAttributeValueObject, messageAttribute.get(objectProperty));

    }
 
Example 18
Source File: SQSMessageProducerTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 4 votes vote down vote up
public void internalTestPropertyToMessageAttribute(SQSMessage sqsText) throws JMSException {

        /*
         * Setup JMS message property
         */
        String booleanProperty = "BooleanProperty";
        String byteProperty = "ByteProperty";
        String shortProperty = "ShortProperty";
        String intProperty = "IntProperty";
        String longProperty = "LongProperty";
        String floatProperty = "FloatProperty";
        String doubleProperty = "DoubleProperty";
        String stringProperty = "StringProperty";
        String objectProperty = "ObjectProperty";

        sqsText.setBooleanProperty(booleanProperty, true);
        sqsText.setByteProperty(byteProperty, (byte)1);
        sqsText.setShortProperty(shortProperty, (short) 2);
        sqsText.setIntProperty(intProperty, 3);
        sqsText.setLongProperty(longProperty, 4L);
        sqsText.setFloatProperty(floatProperty, (float)5.0);
        sqsText.setDoubleProperty(doubleProperty, 6.0);
        sqsText.setStringProperty(stringProperty, "seven");
        sqsText.setObjectProperty(objectProperty, new Integer(8));

        MessageAttributeValue messageAttributeValueBoolean = new MessageAttributeValue();
        messageAttributeValueBoolean.setDataType("Number.Boolean");
        messageAttributeValueBoolean.setStringValue("1");

        MessageAttributeValue messageAttributeValueByte = new MessageAttributeValue();
        messageAttributeValueByte.setDataType("Number.byte");
        messageAttributeValueByte.setStringValue("1");

        MessageAttributeValue messageAttributeValueShort = new MessageAttributeValue();
        messageAttributeValueShort.setDataType("Number.short");
        messageAttributeValueShort.setStringValue("2");

        MessageAttributeValue messageAttributeValueInt = new MessageAttributeValue();
        messageAttributeValueInt.setDataType("Number.int");
        messageAttributeValueInt.setStringValue("3");

        MessageAttributeValue messageAttributeValueLong = new MessageAttributeValue();
        messageAttributeValueLong.setDataType("Number.long");
        messageAttributeValueLong.setStringValue("4");

        MessageAttributeValue messageAttributeValueFloat = new MessageAttributeValue();
        messageAttributeValueFloat.setDataType("Number.float");
        messageAttributeValueFloat.setStringValue("5.0");

        MessageAttributeValue messageAttributeValueDouble = new MessageAttributeValue();
        messageAttributeValueDouble.setDataType("Number.double");
        messageAttributeValueDouble.setStringValue("6.0");

        MessageAttributeValue messageAttributeValueString = new MessageAttributeValue();
        messageAttributeValueString.setDataType("String");
        messageAttributeValueString.setStringValue("seven");

        MessageAttributeValue messageAttributeValueObject = new MessageAttributeValue();
        messageAttributeValueObject.setDataType("Number.int");
        messageAttributeValueObject.setStringValue("8");

        MessageAttributeValue messageAttributeValueJMSSQSMessageType = new MessageAttributeValue();
        messageAttributeValueJMSSQSMessageType.setDataType("String");
        messageAttributeValueJMSSQSMessageType.setStringValue("text");

        /*
         * Convert property to sqs message attribute
         */
        Map<String, MessageAttributeValue> messageAttribute = producer.propertyToMessageAttribute(sqsText);

        /*
         * Verify results
         */
        assertEquals(messageAttributeValueBoolean, messageAttribute.get(booleanProperty));
        assertEquals(messageAttributeValueByte, messageAttribute.get(byteProperty));
        assertEquals(messageAttributeValueShort, messageAttribute.get(shortProperty));
        assertEquals(messageAttributeValueInt, messageAttribute.get(intProperty));
        assertEquals(messageAttributeValueLong, messageAttribute.get(longProperty));
        assertEquals(messageAttributeValueFloat, messageAttribute.get(floatProperty));
        assertEquals(messageAttributeValueDouble, messageAttribute.get(doubleProperty));
        assertEquals(messageAttributeValueString, messageAttribute.get(stringProperty));
        assertEquals(messageAttributeValueObject, messageAttribute.get(objectProperty));

    }
 
Example 19
Source File: SQSMessageProducerFifoTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 4 votes vote down vote up
/**
 * Test sendInternal input with SQSObjectMessage
 */
@Test
public void testSendInternalSQSObjectMessageFromReceivedMessage() throws JMSException, IOException {

    /*
     * Set up non JMS sqs message
     */
    Map<String,MessageAttributeValue> mapMessageAttributes = new HashMap<String, MessageAttributeValue>();

    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setStringValue(SQSMessage.OBJECT_MESSAGE_TYPE);
    messageAttributeValue.setDataType(SQSMessagingClientConstants.STRING);
    mapMessageAttributes.put(SQSMessage.JMS_SQS_MESSAGE_TYPE, messageAttributeValue);

    Map<String, String> mapAttributes = new HashMap<String, String>();
    mapAttributes.put(SQSMessagingClientConstants.APPROXIMATE_RECEIVE_COUNT, "1");
    mapAttributes.put(SQSMessagingClientConstants.MESSAGE_GROUP_ID, GROUP_ID);
    mapAttributes.put(SQSMessagingClientConstants.MESSAGE_DEDUPLICATION_ID, DEDUP_ID);
    mapAttributes.put(SQSMessagingClientConstants.SEQUENCE_NUMBER, SEQ_NUMBER);

    // Encode an object to byte array
    Integer integer = new Integer("10");
    ByteArrayOutputStream array = new ByteArrayOutputStream(10);
    ObjectOutputStream oStream = new ObjectOutputStream(array);
    oStream.writeObject(integer);
    oStream.close();

    String messageBody = Base64.encodeAsString(array.toByteArray());
    com.amazonaws.services.sqs.model.Message message =
            new com.amazonaws.services.sqs.model.Message()
                    .withMessageAttributes(mapMessageAttributes)
                    .withAttributes(mapAttributes)
                    .withBody(messageBody);

    SQSObjectMessage msg = spy(new SQSObjectMessage(acknowledger, QUEUE_URL, message));

    when(amazonSQSClient.sendMessage(any(SendMessageRequest.class)))
            .thenReturn(new SendMessageResult().withMessageId(MESSAGE_ID).withSequenceNumber(SEQ_NUMBER_2));

    producer.sendInternal(destination, msg);

    verify(amazonSQSClient).sendMessage(argThat(new sendMessageRequestMatcher(QUEUE_URL, messageBody, SQSMessage.OBJECT_MESSAGE_TYPE, GROUP_ID, DEDUP_ID)));
    verify(msg).setJMSDestination(destination);
    verify(msg).setJMSMessageID("ID:" + MESSAGE_ID);
    verify(msg).setSQSMessageId(MESSAGE_ID);
    verify(msg).setSequenceNumber(SEQ_NUMBER_2);
}
 
Example 20
Source File: PutSQS.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    FlowFile flowFile = session.get();
    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 {
        SendMessageBatchResult response = client.sendMessageBatch(request);

        // check for errors
        if (!response.getFailed().isEmpty()) {
            throw new ProcessException(response.getFailed().get(0).toString());
        }
    } 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);
}