Search in sources :

Example 1 with ReceiveMessageRequest

use of com.amazonaws.services.sqs.model.ReceiveMessageRequest in project camel by apache.

the class SqsConsumer method poll.

@Override
protected int poll() throws Exception {
    // must reset for each poll
    shutdownRunningTask = null;
    pendingExchanges = 0;
    ReceiveMessageRequest request = new ReceiveMessageRequest(getQueueUrl());
    request.setMaxNumberOfMessages(getMaxMessagesPerPoll() > 0 ? getMaxMessagesPerPoll() : null);
    request.setVisibilityTimeout(getConfiguration().getVisibilityTimeout() != null ? getConfiguration().getVisibilityTimeout() : null);
    request.setWaitTimeSeconds(getConfiguration().getWaitTimeSeconds() != null ? getConfiguration().getWaitTimeSeconds() : null);
    if (attributeNames != null) {
        request.setAttributeNames(attributeNames);
    }
    if (messageAttributeNames != null) {
        request.setMessageAttributeNames(messageAttributeNames);
    }
    LOG.trace("Receiving messages with request [{}]...", request);
    ReceiveMessageResult messageResult = null;
    try {
        messageResult = getClient().receiveMessage(request);
    } catch (QueueDoesNotExistException e) {
        LOG.info("Queue does not exist....recreating now...");
        reConnectToQueue();
        messageResult = getClient().receiveMessage(request);
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("Received {} messages", messageResult.getMessages().size());
    }
    Queue<Exchange> exchanges = createExchanges(messageResult.getMessages());
    return processBatch(CastUtils.cast(exchanges));
}
Also used : QueueDoesNotExistException(com.amazonaws.services.sqs.model.QueueDoesNotExistException) Exchange(org.apache.camel.Exchange) ReceiveMessageRequest(com.amazonaws.services.sqs.model.ReceiveMessageRequest) ReceiveMessageResult(com.amazonaws.services.sqs.model.ReceiveMessageResult)

Example 2 with ReceiveMessageRequest

use of com.amazonaws.services.sqs.model.ReceiveMessageRequest in project nifi by apache.

the class TestGetSQS method testGetMessageAndAutoDelete.

@Test
public void testGetMessageAndAutoDelete() {
    runner.setProperty(GetSQS.QUEUE_URL, "https://sqs.us-west-2.amazonaws.com/123456789012/test-queue-000000000");
    runner.setProperty(GetSQS.AUTO_DELETE, "true");
    Message message1 = new Message();
    message1.setBody("TestMessage1");
    message1.setMessageId("test-message-id-1");
    message1.setReceiptHandle("test-receipt-handle-1");
    Message message2 = new Message();
    message2.setBody("TestMessage2");
    message2.setMessageId("test-message-id-2");
    message2.setReceiptHandle("test-receipt-handle-2");
    ReceiveMessageResult receiveMessageResult = new ReceiveMessageResult().withMessages(message1, message2);
    Mockito.when(mockSQSClient.receiveMessage(Mockito.any(ReceiveMessageRequest.class))).thenReturn(receiveMessageResult);
    runner.run(1);
    ArgumentCaptor<ReceiveMessageRequest> captureReceiveRequest = ArgumentCaptor.forClass(ReceiveMessageRequest.class);
    Mockito.verify(mockSQSClient, Mockito.times(1)).receiveMessage(captureReceiveRequest.capture());
    ReceiveMessageRequest receiveRequest = captureReceiveRequest.getValue();
    assertEquals("https://sqs.us-west-2.amazonaws.com/123456789012/test-queue-000000000", receiveRequest.getQueueUrl());
    ArgumentCaptor<DeleteMessageBatchRequest> captureDeleteRequest = ArgumentCaptor.forClass(DeleteMessageBatchRequest.class);
    Mockito.verify(mockSQSClient, Mockito.times(1)).deleteMessageBatch(captureDeleteRequest.capture());
    DeleteMessageBatchRequest deleteRequest = captureDeleteRequest.getValue();
    assertEquals("https://sqs.us-west-2.amazonaws.com/123456789012/test-queue-000000000", deleteRequest.getQueueUrl());
    assertEquals("test-message-id-1", deleteRequest.getEntries().get(0).getId());
    assertEquals("test-message-id-2", deleteRequest.getEntries().get(1).getId());
    runner.assertAllFlowFilesTransferred(GetSQS.REL_SUCCESS, 2);
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(GetSQS.REL_SUCCESS);
    MockFlowFile ff0 = flowFiles.get(0);
    ff0.assertAttributeEquals("sqs.message.id", "test-message-id-1");
    MockFlowFile ff1 = flowFiles.get(1);
    ff1.assertAttributeEquals("sqs.message.id", "test-message-id-2");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) DeleteMessageBatchRequest(com.amazonaws.services.sqs.model.DeleteMessageBatchRequest) Message(com.amazonaws.services.sqs.model.Message) ReceiveMessageRequest(com.amazonaws.services.sqs.model.ReceiveMessageRequest) ReceiveMessageResult(com.amazonaws.services.sqs.model.ReceiveMessageResult) Test(org.junit.Test)

Example 3 with ReceiveMessageRequest

use of com.amazonaws.services.sqs.model.ReceiveMessageRequest in project nifi by apache.

the class GetSQS method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    final String queueUrl = context.getProperty(DYNAMIC_QUEUE_URL).evaluateAttributeExpressions().getValue();
    final AmazonSQSClient client = getClient();
    final ReceiveMessageRequest request = new ReceiveMessageRequest();
    request.setAttributeNames(Collections.singleton("All"));
    request.setMessageAttributeNames(Collections.singleton("All"));
    request.setMaxNumberOfMessages(context.getProperty(BATCH_SIZE).asInteger());
    request.setVisibilityTimeout(context.getProperty(VISIBILITY_TIMEOUT).asTimePeriod(TimeUnit.SECONDS).intValue());
    request.setQueueUrl(queueUrl);
    request.setWaitTimeSeconds(context.getProperty(RECEIVE_MSG_WAIT_TIME).asTimePeriod(TimeUnit.SECONDS).intValue());
    final Charset charset = Charset.forName(context.getProperty(CHARSET).getValue());
    final ReceiveMessageResult result;
    try {
        result = client.receiveMessage(request);
    } catch (final Exception e) {
        getLogger().error("Failed to receive messages from Amazon SQS due to {}", new Object[] { e });
        context.yield();
        return;
    }
    final List<Message> messages = result.getMessages();
    if (messages.isEmpty()) {
        context.yield();
        return;
    }
    final boolean autoDelete = context.getProperty(AUTO_DELETE).asBoolean();
    for (final Message message : messages) {
        FlowFile flowFile = session.create();
        final Map<String, String> attributes = new HashMap<>();
        for (final Map.Entry<String, String> entry : message.getAttributes().entrySet()) {
            attributes.put("sqs." + entry.getKey(), entry.getValue());
        }
        for (final Map.Entry<String, MessageAttributeValue> entry : message.getMessageAttributes().entrySet()) {
            attributes.put("sqs." + entry.getKey(), entry.getValue().getStringValue());
        }
        attributes.put("hash.value", message.getMD5OfBody());
        attributes.put("hash.algorithm", "md5");
        attributes.put("sqs.message.id", message.getMessageId());
        attributes.put("sqs.receipt.handle", message.getReceiptHandle());
        flowFile = session.putAllAttributes(flowFile, attributes);
        flowFile = session.write(flowFile, new OutputStreamCallback() {

            @Override
            public void process(final OutputStream out) throws IOException {
                out.write(message.getBody().getBytes(charset));
            }
        });
        session.transfer(flowFile, REL_SUCCESS);
        session.getProvenanceReporter().receive(flowFile, queueUrl);
        getLogger().info("Successfully received {} from Amazon SQS", new Object[] { flowFile });
    }
    if (autoDelete) {
        // If we want to auto-delete messages, we must fist commit the session to ensure that the data
        // is persisted in NiFi's repositories.
        session.commit();
        final DeleteMessageBatchRequest deleteRequest = new DeleteMessageBatchRequest();
        deleteRequest.setQueueUrl(queueUrl);
        final List<DeleteMessageBatchRequestEntry> deleteRequestEntries = new ArrayList<>();
        for (final Message message : messages) {
            final DeleteMessageBatchRequestEntry entry = new DeleteMessageBatchRequestEntry();
            entry.setId(message.getMessageId());
            entry.setReceiptHandle(message.getReceiptHandle());
            deleteRequestEntries.add(entry);
        }
        deleteRequest.setEntries(deleteRequestEntries);
        try {
            client.deleteMessageBatch(deleteRequest);
        } catch (final Exception e) {
            getLogger().error("Received {} messages from Amazon SQS but failed to delete the messages; these messages" + " may be duplicated. Reason for deletion failure: {}", new Object[] { messages.size(), e });
        }
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) DeleteMessageBatchRequest(com.amazonaws.services.sqs.model.DeleteMessageBatchRequest) ReceiveMessageRequest(com.amazonaws.services.sqs.model.ReceiveMessageRequest) Message(com.amazonaws.services.sqs.model.Message) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) AmazonSQSClient(com.amazonaws.services.sqs.AmazonSQSClient) IOException(java.io.IOException) OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback) DeleteMessageBatchRequestEntry(com.amazonaws.services.sqs.model.DeleteMessageBatchRequestEntry) HashMap(java.util.HashMap) Map(java.util.Map) ReceiveMessageResult(com.amazonaws.services.sqs.model.ReceiveMessageResult) MessageAttributeValue(com.amazonaws.services.sqs.model.MessageAttributeValue)

Example 4 with ReceiveMessageRequest

use of com.amazonaws.services.sqs.model.ReceiveMessageRequest in project tutorials by eugenp.

the class SpringCloudSQSIntegrationTest method whenConvertedMessageSentAndVerified_thenSuccess.

@Test
public void whenConvertedMessageSentAndVerified_thenSuccess() throws InterruptedException, IOException {
    Greeting message = new Greeting("Hello", "World");
    springCloudSQS.send(sendQueueName, message);
    AmazonSQS amazonSQS = SpringCloudAwsTestUtil.amazonSQS();
    ReceiveMessageRequest request = new ReceiveMessageRequest(sendQueueURl);
    request.setMaxNumberOfMessages(1);
    ReceiveMessageResult result = null;
    do {
        result = amazonSQS.receiveMessage(request);
        if (result.getMessages().size() == 0) {
            logger.info("Message not received at first time, waiting for 1 second");
        }
    } while (result.getMessages().size() == 0);
    assertThat(new ObjectMapper().readValue(result.getMessages().get(0).getBody(), Greeting.class)).isEqualTo(message);
    // Delete message so that it doen't interfere with other test
    amazonSQS.deleteMessage(sendQueueURl, result.getMessages().get(0).getReceiptHandle());
}
Also used : ReceiveMessageRequest(com.amazonaws.services.sqs.model.ReceiveMessageRequest) AmazonSQS(com.amazonaws.services.sqs.AmazonSQS) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ReceiveMessageResult(com.amazonaws.services.sqs.model.ReceiveMessageResult) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with ReceiveMessageRequest

use of com.amazonaws.services.sqs.model.ReceiveMessageRequest in project tutorials by eugenp.

the class SpringCloudSQSIntegrationTest method whenMessageSentAndVerified_thenSuccess.

@Test
public void whenMessageSentAndVerified_thenSuccess() throws InterruptedException {
    String message = "Hello World";
    springCloudSQS.send(sendQueueName, message);
    AmazonSQS amazonSQS = SpringCloudAwsTestUtil.amazonSQS();
    ReceiveMessageRequest request = new ReceiveMessageRequest(sendQueueURl);
    request.setMaxNumberOfMessages(1);
    ReceiveMessageResult result = null;
    do {
        result = amazonSQS.receiveMessage(request);
        if (result.getMessages().size() == 0) {
            logger.info("Message not received at first time, waiting for 1 second");
        }
    } while (result.getMessages().size() == 0);
    assertThat(result.getMessages().get(0).getBody()).isEqualTo(message);
    // Delete message so that it doen't interfere with other test
    amazonSQS.deleteMessage(sendQueueURl, result.getMessages().get(0).getReceiptHandle());
}
Also used : ReceiveMessageRequest(com.amazonaws.services.sqs.model.ReceiveMessageRequest) AmazonSQS(com.amazonaws.services.sqs.AmazonSQS) ReceiveMessageResult(com.amazonaws.services.sqs.model.ReceiveMessageResult) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ReceiveMessageRequest (com.amazonaws.services.sqs.model.ReceiveMessageRequest)11 ReceiveMessageResult (com.amazonaws.services.sqs.model.ReceiveMessageResult)9 Message (com.amazonaws.services.sqs.model.Message)6 Test (org.junit.Test)5 DeleteMessageBatchRequest (com.amazonaws.services.sqs.model.DeleteMessageBatchRequest)4 AmazonSQS (com.amazonaws.services.sqs.AmazonSQS)3 MessageAttributeValue (com.amazonaws.services.sqs.model.MessageAttributeValue)3 AmazonSQSClient (com.amazonaws.services.sqs.AmazonSQSClient)2 DeleteMessageBatchRequestEntry (com.amazonaws.services.sqs.model.DeleteMessageBatchRequestEntry)2 ArrayList (java.util.ArrayList)2 MockFlowFile (org.apache.nifi.util.MockFlowFile)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 TransportException (co.krypt.krypton.exception.TransportException)1 NetworkMessage (co.krypt.krypton.protocol.NetworkMessage)1 AmazonSQSException (com.amazonaws.services.sqs.model.AmazonSQSException)1 CreateQueueRequest (com.amazonaws.services.sqs.model.CreateQueueRequest)1 QueueDoesNotExistException (com.amazonaws.services.sqs.model.QueueDoesNotExistException)1 SetQueueAttributesRequest (com.amazonaws.services.sqs.model.SetQueueAttributesRequest)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1