use of software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest in project aws-doc-sdk-examples by awsdocs.
the class SendReceiveMessages method getMessages.
public String getMessages() {
List attr = new ArrayList<String>();
attr.add("Name");
SqsClient sqsClient = getClient();
try {
GetQueueUrlRequest getQueueRequest = GetQueueUrlRequest.builder().queueName(QUEUE_NAME).build();
String queueUrl = sqsClient.getQueueUrl(getQueueRequest).queueUrl();
// Receive messages from the queue
ReceiveMessageRequest receiveRequest = ReceiveMessageRequest.builder().queueUrl(queueUrl).maxNumberOfMessages(10).messageAttributeNames(attr).build();
List<Message> messages = sqsClient.receiveMessage(receiveRequest).messages();
com.example.Message myMessage;
List allMessages = new ArrayList<com.example.Message>();
// Push the messages to a list
for (Message m : messages) {
myMessage = new com.example.Message();
myMessage.setBody(m.body());
Map map = m.messageAttributes();
MessageAttributeValue val = (MessageAttributeValue) map.get("Name");
myMessage.setName(val.stringValue());
allMessages.add(myMessage);
}
return convertToString(toXml(allMessages));
} catch (SqsException e) {
e.getStackTrace();
}
return "";
}
use of software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest in project aws-doc-sdk-examples by awsdocs.
the class VideoDetect method getLabelJob.
public static void getLabelJob(RekognitionClient rekClient, SqsClient sqs, String queueUrl) {
List<Message> messages = null;
ReceiveMessageRequest messageRequest = ReceiveMessageRequest.builder().queueUrl(queueUrl).build();
try {
messages = sqs.receiveMessage(messageRequest).messages();
if (!messages.isEmpty()) {
for (Message message : messages) {
String notification = message.body();
// Get the status and job id from the notification
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonMessageTree = mapper.readTree(notification);
JsonNode messageBodyText = jsonMessageTree.get("Message");
ObjectMapper operationResultMapper = new ObjectMapper();
JsonNode jsonResultTree = operationResultMapper.readTree(messageBodyText.textValue());
JsonNode operationJobId = jsonResultTree.get("JobId");
JsonNode operationStatus = jsonResultTree.get("Status");
System.out.println("Job found in JSON is " + operationJobId);
DeleteMessageRequest deleteMessageRequest = DeleteMessageRequest.builder().queueUrl(queueUrl).build();
String jobId = operationJobId.textValue();
if (startJobId.compareTo(jobId) == 0) {
System.out.println("Job id: " + operationJobId);
System.out.println("Status : " + operationStatus.toString());
if (operationStatus.asText().equals("SUCCEEDED"))
GetResultsLabels(rekClient);
else
System.out.println("Video analysis failed");
sqs.deleteMessage(deleteMessageRequest);
} else {
System.out.println("Job received was not job " + startJobId);
sqs.deleteMessage(deleteMessageRequest);
}
}
}
} catch (RekognitionException e) {
e.getMessage();
System.exit(1);
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
use of software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest in project aws-doc-sdk-examples by awsdocs.
the class SendReceiveMessages method main.
public static void main(String[] args) {
SqsClient sqsClient = SqsClient.builder().region(Region.US_WEST_2).build();
try {
CreateQueueRequest request = CreateQueueRequest.builder().queueName(QUEUE_NAME).build();
CreateQueueResponse createResult = sqsClient.createQueue(request);
GetQueueUrlRequest getQueueRequest = GetQueueUrlRequest.builder().queueName(QUEUE_NAME).build();
String queueUrl = sqsClient.getQueueUrl(getQueueRequest).queueUrl();
SendMessageRequest sendMsgRequest = SendMessageRequest.builder().queueUrl(queueUrl).messageBody("hello world").delaySeconds(5).build();
sqsClient.sendMessage(sendMsgRequest);
// Send multiple messages to the queue
SendMessageBatchRequest sendBatchRequest = SendMessageBatchRequest.builder().queueUrl(queueUrl).entries(SendMessageBatchRequestEntry.builder().messageBody("Hello from message 1").id("msg_1").build(), SendMessageBatchRequestEntry.builder().messageBody("Hello from message 2").delaySeconds(10).id("msg_2").build()).build();
sqsClient.sendMessageBatch(sendBatchRequest);
// Receive messages from the queue
ReceiveMessageRequest receiveRequest = ReceiveMessageRequest.builder().queueUrl(queueUrl).build();
List<Message> messages = sqsClient.receiveMessage(receiveRequest).messages();
// Print out the messages
for (Message m : messages) {
System.out.println("\n" + m.body());
}
} catch (QueueNameExistsException e) {
throw e;
}
}
use of software.amazon.awssdk.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 = ReceiveMessageRequest.builder().maxNumberOfMessages(MAX_NUMBER_OF_MESSAGES).attributeNamesWithStrings(SENT_TIMESTAMP.toString()).queueUrl(queueUrl()).build();
final ReceiveMessageResponse receiveMessageResponse = sqsClient.receiveMessage(receiveMessageRequest);
final List<Message> messages = receiveMessageResponse.messages();
if (messages == null || messages.isEmpty()) {
return;
}
lastReceivedMsSinceEpoch = requestTimeMsSinceEpoch;
// Capture the received messages.
for (Message orgMsg : messages) {
long msgTimeStamp = Long.parseLong(orgMsg.attributes().get(SENT_TIMESTAMP));
SqsMessage message = SqsMessage.create(orgMsg.body(), orgMsg.messageId(), orgMsg.receiptHandle(), msgTimeStamp, requestTimeMsSinceEpoch);
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);
minReceivedTimestampMsSinceEpoch.add(requestTimeMsSinceEpoch, msgTimeStamp);
maxReceivedTimestampMsSinceEpoch.add(requestTimeMsSinceEpoch, msgTimeStamp);
minUnreadTimestampMsSinceEpoch.add(requestTimeMsSinceEpoch, msgTimeStamp);
}
}
Aggregations