Skip to content

Instantly share code, notes, and snippets.

@chandra-goka
Created Aug 12, 2021
Embed
What would you like to do?
package com.otp;
import java.util.*;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.*;
public class SQS_Example {
public static void main(String[] args) {
String queueName = "My-Sample-Standard-Queue";
SqsClient sqsClient = SqsClient.builder()
.region(Region.US_WEST_2)
.build();
// String queueUrl= createStandardQueue(sqsClient, queueName);
// System.out.println("Queue URL : "+queueUrl);
//
// String fifoQueueUrl= createFIFOQueue(sqsClient, "My-FIFO-Queue.fifo");
// System.out.println("fifoQueueUrl : "+fifoQueueUrl);
//sendSingleMessage(sqsClient, "https://sqs.us-west-2.amazonaws.com/816569674899/My-FIFO-Queue.fifo");
//sendSingleFifoMessage(sqsClient, "https://sqs.us-west-2.amazonaws.com/816569674899/My-FIFO-Queue.fifo");
//sendBatchMessages(sqsClient, "https://sqs.us-west-2.amazonaws.com/816569674899/My-Sample-Standard-Queue");
receiveMessages(sqsClient, "https://sqs.us-west-2.amazonaws.com/816569674899/My-FIFO-Queue.fifo");
listAllQueues(sqsClient);
sqsClient.close();
}
public static String createStandardQueue(SqsClient sqsClient,String queueName) {
try {
CreateQueueRequest cqr = CreateQueueRequest.builder()
.queueName(queueName)
.build();
sqsClient.createQueue(cqr);
GetQueueUrlResponse getQueueUrlResponse =
sqsClient.getQueueUrl(GetQueueUrlRequest.builder().queueName(queueName).build());
return getQueueUrlResponse.queueUrl();
} catch (SqsException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
return "";
}
public static String createFIFOQueue(SqsClient sqsClient,String queueName) {
Map<QueueAttributeName, String> queueAttributes = new HashMap<>();
queueAttributes.put(QueueAttributeName.FIFO_QUEUE, "true");
try {
CreateQueueRequest cqr = CreateQueueRequest.builder()
.queueName(queueName)
.attributes(queueAttributes)
.build();
sqsClient.createQueue(cqr);
GetQueueUrlResponse getQueueUrlResponse =
sqsClient.getQueueUrl(GetQueueUrlRequest.builder().queueName(queueName).build());
return getQueueUrlResponse.queueUrl();
} catch (SqsException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
return "";
}
public static void listAllQueues(SqsClient sqsClient) {
try {
ListQueuesRequest listQueuesRequest = ListQueuesRequest.builder().build();
ListQueuesResponse listQueuesResponse = sqsClient.listQueues(listQueuesRequest);
listQueuesResponse.queueUrls().forEach(System.out::println);
} catch (SqsException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
public static void sendSingleMessage(SqsClient sqsClient, String queueUrl) {
try {
sqsClient.sendMessage(SendMessageRequest.builder()
.queueUrl(queueUrl)
.messageBody("Hello world from Java!")
.delaySeconds(10)
.build());
System.out.println("Message has been sent successfully");
} catch (SqsException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
public static void sendSingleFifoMessage(SqsClient sqsClient, String queueUrl) {
try {
sqsClient.sendMessage(SendMessageRequest.builder()
.queueUrl(queueUrl)
.messageBody("Hello world from Java!")
.messageGroupId("12345")
.messageDeduplicationId("123")
.build());
System.out.println("Message has been sent successfully");
} catch (SqsException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
public static void sendBatchMessages(SqsClient sqsClient, String queueUrl) {
try {
Collection<SendMessageBatchRequestEntry> messages = Arrays.asList(
SendMessageBatchRequestEntry.builder().id("id1").messageBody("Hello from msg 1").build(),
SendMessageBatchRequestEntry.builder().id("id2").messageBody("msg 2").delaySeconds(10).build()
);
SendMessageBatchRequest sendMessageBatchRequest = SendMessageBatchRequest.builder()
.queueUrl(queueUrl)
.entries(messages)
.build();
sqsClient.sendMessageBatch(sendMessageBatchRequest);
} catch (SqsException e) {
System.err.println(e.awsErrorDetails().errorMessage());
retry(sqsClient, queueUrl);
}
}
static int count = 3;
public static void retry(SqsClient sqsClient, String queueUrl){
while(count == 0) {
sendBatchMessages(sqsClient, queueUrl);
count -= count;
}
}
public static void receiveMessages(SqsClient sqsClient, String queueUrl) {
try {
ReceiveMessageRequest receiveMessageRequest = ReceiveMessageRequest.builder()
.queueUrl(queueUrl)
.maxNumberOfMessages(5)
.build();
List<Message> messages = sqsClient.receiveMessage(receiveMessageRequest).messages();
messages.forEach(message -> System.out.println(message.body()));
} catch (SqsException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
public static void deleteMessages(SqsClient sqsClient, String queueUrl, List<Message> messages) {
try {
for (Message message : messages) {
System.out.println("message Body:: "+message.body());
DeleteMessageRequest deleteMessageRequest = DeleteMessageRequest.builder()
.queueUrl(queueUrl)
.receiptHandle(message.receiptHandle())
.build();
sqsClient.deleteMessage(deleteMessageRequest);
}
} catch (SqsException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment