5

I want to convert one of my synchronous API into asynchronous. And I believe queue are one way to do this. Like a publisher will push(synchronously) the message into queue which will be consumed by consumer API from the queue.

I was curious to know what is the right way of consuming AWS SimpleQueueService messages. Can queue call an API to deliver the message to it or the only way to do is to poll the queue. But I believe that polling will make our system busy waiting so it is best the queue deliver the message to API.

What is possible way to do this?

Kuldeep Yadav
  • 1,664
  • 5
  • 23
  • 41

2 Answers2

7

If you want to consume from SQS you have the following methods:

If you intend to retrieve to get responses back you can also take advantage of virtual queues.

Chris Williams
  • 32,215
  • 4
  • 30
  • 68
0

In application.yml

sqs:
region: ap-south-1
accessKeyId: arunsinghgujjar
secretAccessKey: jainpurwalearunsingh/saharanpursepauchepuna
cloud:
aws:
end-point:
uri: https://arun-learningsubway-1.amazonaws.com/9876974864/learningsubway_SQS.fifo
queue:
max-poll-time: 20
max-messages: 10
fetch-wait-on-error: 60
enabled: true
content: sqs

Write SQS client

public String sendMessage(MessageDistributionEvent messageDistributionEvent) {
SendMessageResponse sendMessage = null;
try {
Map<String, MessageAttributeValue> attributes = new HashMap<>();

String recepList = "";
for (Integer myInt : messageDistributionEvent.getRecipients()) {
recepList = recepList + "_" + myInt;
}
SendMessageRequest sendMsgRequest = SendMessageRequest.builder()
.queueUrl(url)
.messageBody(messageDistributionEvent.getChannelId() + "_" + messageDistributionEvent.getMessageId() + "" + recepList)
.messageGroupId("1")
.messageAttributes(attributes)
.build();
sendMessage = sqsClient.sendMessage(sendMsgRequest);
} catch (Exception ex) {
log.info("failed to send message :" + ex);
}
return sendMessage.sequenceNumber();
}

Read Message from Queue

ReceiveMessageRequest receiveMessageRequest = ReceiveMessageRequest.builder()
.queueUrl(url)
.waitTimeSeconds(maxPollTime)
.maxNumberOfMessages(maxMessages)
.messageAttributeNames("MessageLabel")
.build();
List<Message> sqsMessages = sqsClient.receiveMessage(receiveMessageRequest).messages();

Reference https://learningsubway.com/read-write-data-into-aws-sqs-using-java/

Arun
  • 1,011
  • 11
  • 13