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;
}
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);
}
}
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);
}
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));
}
}
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");
}
Aggregations