Search in sources :

Example 6 with ReceiveMessageRequest

use of com.amazonaws.services.sqs.model.ReceiveMessageRequest in project krypton-android by kryptco.

the class SQSTransport method receiveMessages.

public static List<byte[]> receiveMessages(final Pairing pairing) throws TransportException {
    final AmazonSQSClient client = getClient();
    ReceiveMessageRequest request = new ReceiveMessageRequest(sendQueueURL(pairing));
    request.setWaitTimeSeconds(10);
    request.setMaxNumberOfMessages(10);
    ReceiveMessageResult result = client.receiveMessage(request);
    final List<DeleteMessageBatchRequestEntry> deleteEntries = new ArrayList<>();
    ArrayList<byte[]> messages = new ArrayList<byte[]>();
    for (Message m : result.getMessages()) {
        deleteEntries.add(new DeleteMessageBatchRequestEntry(m.getMessageId(), m.getReceiptHandle()));
        try {
            messages.add(Base64.decode(m.getBody()));
        } catch (Exception e) {
            Log.e(TAG, "failed to decode message: " + e.getMessage());
        }
    }
    if (!deleteEntries.isEmpty()) {
        deleteThreadPool.submit(() -> {
            try {
                DeleteMessageBatchRequest deleteRequest = new DeleteMessageBatchRequest(sendQueueURL(pairing)).withEntries(deleteEntries);
                client.deleteMessageBatch(deleteRequest);
            } catch (Exception e) {
                Log.e(TAG, "failed to delete messages: " + e.getMessage());
            }
        });
    }
    return messages;
}
Also used : DeleteMessageBatchRequest(com.amazonaws.services.sqs.model.DeleteMessageBatchRequest) ReceiveMessageRequest(com.amazonaws.services.sqs.model.ReceiveMessageRequest) Message(com.amazonaws.services.sqs.model.Message) NetworkMessage(co.krypt.krypton.protocol.NetworkMessage) ArrayList(java.util.ArrayList) AmazonSQSClient(com.amazonaws.services.sqs.AmazonSQSClient) DeleteMessageBatchRequestEntry(com.amazonaws.services.sqs.model.DeleteMessageBatchRequestEntry) TransportException(co.krypt.krypton.exception.TransportException) ReceiveMessageResult(com.amazonaws.services.sqs.model.ReceiveMessageResult)

Example 7 with ReceiveMessageRequest

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

the class SqsUnboundedReader method pull.

/**
 * BLOCKING Fetch another batch of messages from SQS.
 */
private void pull() {
    if (inFlight.size() >= MAX_IN_FLIGHT) {
        // messages faster than we can checkpoint them.
        return;
    }
    long requestTimeMsSinceEpoch = now();
    long deadlineMsSinceEpoch = requestTimeMsSinceEpoch + visibilityTimeoutMs;
    final ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(source.getRead().queueUrl());
    receiveMessageRequest.setMaxNumberOfMessages(MAX_NUMBER_OF_MESSAGES);
    receiveMessageRequest.setAttributeNames(Arrays.asList(MessageSystemAttributeName.SentTimestamp.toString()));
    final ReceiveMessageResult receiveMessageResult = sqsClient.receiveMessage(receiveMessageRequest);
    final List<Message> messages = receiveMessageResult.getMessages();
    if (messages == null || messages.isEmpty()) {
        numEmptyReceives.add(requestTimeMsSinceEpoch, 1L);
        return;
    }
    lastReceivedMsSinceEpoch = requestTimeMsSinceEpoch;
    // Capture the received messages.
    for (Message message : messages) {
        // Keep request time as message attribute for later usage
        MessageAttributeValue reqTime = new MessageAttributeValue().withStringValue(Long.toString(requestTimeMsSinceEpoch));
        message.setMessageAttributes(ImmutableMap.of(REQUEST_TIME, reqTime));
        messagesNotYetRead.add(message);
        notYetReadBytes += message.getBody().getBytes(UTF_8).length;
        inFlight.put(message.getMessageId(), new InFlightState(message.getReceiptHandle(), requestTimeMsSinceEpoch, deadlineMsSinceEpoch));
        numReceived++;
        numReceivedRecently.add(requestTimeMsSinceEpoch, 1L);
        long timestampMillis = getTimestamp(message).getMillis();
        minReceivedTimestampMsSinceEpoch.add(requestTimeMsSinceEpoch, timestampMillis);
        maxReceivedTimestampMsSinceEpoch.add(requestTimeMsSinceEpoch, timestampMillis);
        minUnreadTimestampMsSinceEpoch.add(requestTimeMsSinceEpoch, timestampMillis);
    }
}
Also used : ReceiveMessageRequest(com.amazonaws.services.sqs.model.ReceiveMessageRequest) Message(com.amazonaws.services.sqs.model.Message) ReceiveMessageResult(com.amazonaws.services.sqs.model.ReceiveMessageResult) MessageAttributeValue(com.amazonaws.services.sqs.model.MessageAttributeValue)

Example 8 with ReceiveMessageRequest

use of com.amazonaws.services.sqs.model.ReceiveMessageRequest in project aws-doc-sdk-examples by awsdocs.

the class LongPolling method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply the name of a queue to create and\n" + "queue url of an existing queue.\n\n" + "Ex: LongPolling <unique-queue-name> <existing-queue-url>\n";
    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String queue_name = args[0];
    String queue_url = args[1];
    final AmazonSQS sqs = AmazonSQSClientBuilder.defaultClient();
    // Enable long polling when creating a queue
    CreateQueueRequest create_request = new CreateQueueRequest().withQueueName(queue_name).addAttributesEntry("ReceiveMessageWaitTimeSeconds", "20");
    try {
        sqs.createQueue(create_request);
    } catch (AmazonSQSException e) {
        if (!e.getErrorCode().equals("QueueAlreadyExists")) {
            throw e;
        }
    }
    // Enable long polling on an existing queue
    SetQueueAttributesRequest set_attrs_request = new SetQueueAttributesRequest().withQueueUrl(queue_url).addAttributesEntry("ReceiveMessageWaitTimeSeconds", "20");
    sqs.setQueueAttributes(set_attrs_request);
    // Enable long polling on a message receipt
    ReceiveMessageRequest receive_request = new ReceiveMessageRequest().withQueueUrl(queue_url).withWaitTimeSeconds(20);
    sqs.receiveMessage(receive_request);
}
Also used : ReceiveMessageRequest(com.amazonaws.services.sqs.model.ReceiveMessageRequest) CreateQueueRequest(com.amazonaws.services.sqs.model.CreateQueueRequest) AmazonSQS(com.amazonaws.services.sqs.AmazonSQS) SetQueueAttributesRequest(com.amazonaws.services.sqs.model.SetQueueAttributesRequest) AmazonSQSException(com.amazonaws.services.sqs.model.AmazonSQSException)

Example 9 with ReceiveMessageRequest

use of com.amazonaws.services.sqs.model.ReceiveMessageRequest in project java by wavefrontHQ.

the class SQSSubmissionQueue method peek.

@Override
public T peek() {
    try {
        if (this.head != null)
            return head;
        ReceiveMessageRequest receiveRequest = new ReceiveMessageRequest(this.queueUrl);
        receiveRequest.setMaxNumberOfMessages(1);
        receiveRequest.setWaitTimeSeconds(1);
        ReceiveMessageResult result = sqsClient.receiveMessage(receiveRequest);
        List<Message> messages = result.getMessages();
        if (messages.size() <= 0) {
            return null;
        }
        Message message = messages.get(0);
        byte[] messageBytes = parseBase64Binary(message.getBody());
        messageHandle = message.getReceiptHandle();
        head = converter.fromBytes(messageBytes);
        return head;
    } catch (IOException e) {
        throw Utils.<Error>throwAny(e);
    } catch (AmazonClientException e) {
        throw Utils.<Error>throwAny(new IOException("AmazonClientException while trying to peek the queues, ", e));
    }
}
Also used : ReceiveMessageRequest(com.amazonaws.services.sqs.model.ReceiveMessageRequest) Message(com.amazonaws.services.sqs.model.Message) AmazonClientException(com.amazonaws.AmazonClientException) IOException(java.io.IOException) ReceiveMessageResult(com.amazonaws.services.sqs.model.ReceiveMessageResult)

Example 10 with ReceiveMessageRequest

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

the class TestGetSQS method testGetMessageNoAutoDelete.

@Test
public void testGetMessageNoAutoDelete() {
    runner.setProperty(GetSQS.QUEUE_URL, "https://sqs.us-west-2.amazonaws.com/123456789012/test-queue-000000000");
    runner.setProperty(GetSQS.AUTO_DELETE, "false");
    Message message1 = new Message();
    message1.setBody("TestMessage1");
    message1.addAttributesEntry("attrib-key-1", "attrib-value-1");
    MessageAttributeValue messageAttributeValue = new MessageAttributeValue();
    messageAttributeValue.setStringValue("msg-attrib-value-1");
    message1.addMessageAttributesEntry("msg-attrib-key-1", messageAttributeValue);
    message1.setMD5OfBody("test-md5-hash-1");
    message1.setMessageId("test-message-id-1");
    message1.setReceiptHandle("test-receipt-handle-1");
    ReceiveMessageResult receiveMessageResult = new ReceiveMessageResult().withMessages(message1);
    Mockito.when(mockSQSClient.receiveMessage(Mockito.any(ReceiveMessageRequest.class))).thenReturn(receiveMessageResult);
    runner.run(1);
    ArgumentCaptor<ReceiveMessageRequest> captureRequest = ArgumentCaptor.forClass(ReceiveMessageRequest.class);
    Mockito.verify(mockSQSClient, Mockito.times(1)).receiveMessage(captureRequest.capture());
    ReceiveMessageRequest request = captureRequest.getValue();
    assertEquals("https://sqs.us-west-2.amazonaws.com/123456789012/test-queue-000000000", request.getQueueUrl());
    Mockito.verify(mockSQSClient, Mockito.never()).deleteMessageBatch(Mockito.any(DeleteMessageBatchRequest.class));
    runner.assertAllFlowFilesTransferred(GetSQS.REL_SUCCESS, 1);
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(GetSQS.REL_SUCCESS);
    MockFlowFile ff0 = flowFiles.get(0);
    ff0.assertAttributeEquals("sqs.attrib-key-1", "attrib-value-1");
    ff0.assertAttributeEquals("sqs.msg-attrib-key-1", "msg-attrib-value-1");
    ff0.assertAttributeEquals("hash.value", "test-md5-hash-1");
    ff0.assertAttributeEquals("hash.algorithm", "md5");
    ff0.assertAttributeEquals("sqs.message.id", "test-message-id-1");
    ff0.assertAttributeEquals("sqs.receipt.handle", "test-receipt-handle-1");
}
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) MessageAttributeValue(com.amazonaws.services.sqs.model.MessageAttributeValue) ReceiveMessageResult(com.amazonaws.services.sqs.model.ReceiveMessageResult) Test(org.junit.Test)

Aggregations

ReceiveMessageRequest (com.amazonaws.services.sqs.model.ReceiveMessageRequest)13 ReceiveMessageResult (com.amazonaws.services.sqs.model.ReceiveMessageResult)11 Message (com.amazonaws.services.sqs.model.Message)8 Test (org.junit.Test)6 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 IOException (java.io.IOException)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 AmazonClientException (com.amazonaws.AmazonClientException)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 SendMessageRequest (com.amazonaws.services.sqs.model.SendMessageRequest)1