sqs

Bismillah learn SQS
https://www.baeldung.com/aws-queues-java

SQS(Simple Queue Service )

AmazonSQS sqs = AmazonSQSClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(Regions.US_EAST_1)
.build();

--
2) creating a queue

CreateQueueRequest createStandardQueueRequest =
new CreateQueueREquest("mi-queue");

String standardQueueUrl = sqs.createqueue(createStandardRequest)
.getQueueUrl()
---
3) Creating a FIFO Queue
Map queueAttributes = new HashMap<>();
queueAttributes.put("FifoQueue", "true");
queueAttributes.put("ContentBasedDeduplication", true);
CreateQueueRequest createFifoQueueRequest = new CreateQueueRequest("mi-queue")
.withAttributes(queueAttributes);
sqs.createQueue(createQueueRequest)
.getQueueUrl();

---
4) Posting message to Queues
Map messageAttributes = new HashMap<>();
messageAttributes.put("AttributeOne", new MessageAttributeValue()
. withStringValue("This is an attribute")
.withDataType("String");

SendMessageRequest sendMessageStandardQueue = new SendMessageRequest()
.withQueueUrl(standardQueueUrl)
.withMessageBody("A simple message.")
.withDelaySeconds(30)
.withMessageBody("A simple message")
.withDelaySeconds(30)
.withMessageAttributes(messageAttributes);

sqs.sendMessage(sendMessageStandardQueue);

---
5) posting a message to a fifo queue
SendMessageRequest sendMessageFifoQeuue = new SendMessageRequest()
.withQueueUrl(fifoQueueUrl)
.withMessageBody("Another simple message.")
.withMessageGroupId("mi-group-1")
.withMessageAttributes(messageAttributes)

6) Posting multiple Messagea to a queue
List 
messageEntries = new ArrayList<>90;
messageEntries.add(new SendMessageBatchRequestEntry()
.withId("id-1")
.withMessageBody("batch-1")
.withMessageGroupId("mi-group-1"));

mesageEntries.add(new SendMessageBatchRequestEntry()
.withId("id-2")
.withMessageBody("batch-2")
.withMessageGroupId("mi-group-1"));

SendMessageBatchRequest sendMessageBatchRequest
 = new SendMessageBatchRequest(fifoQueueUrl, messageEntries);
 sqs.sendMessageBatch(sendMessageBatchRequest);

 ---
 5) Reading Message from Queues
 ReceiveMesageRequest receiveMessageRequest = new ReceiveMessageRequest(fifoQueueUrl)
 .withWaitTimeSeconds(10)
 .withMaxNumberOfMessages(10);

 List sqsMessages = sqs.receiveMessage(receiveMessageRequest).getMessages();
 ---
 wati -> long pilling

---
sqsMessags.get(0).getAttributes();
sqsMessags.get(0).getBody();
---
6) deleting a message from a queue
sqs.deleteMessage(new DeleteMessageRequest().
withQueueUrl(fifoQueueUrl)
.withReceiptHandle(sqsMessages.get(0).getReceiptHandle()))

No comments:

Post a Comment