{"id":64507,"date":"2024-09-09T10:16:13","date_gmt":"2024-09-09T04:46:13","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=64507"},"modified":"2024-09-09T16:20:21","modified_gmt":"2024-09-09T10:50:21","slug":"integrating-sqs-with-spring-boot-a-comprehensive-guide","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/integrating-sqs-with-spring-boot-a-comprehensive-guide\/","title":{"rendered":"Integrating SQS with Spring Boot : Quick Start"},"content":{"rendered":"<h2><strong>What is Amazon SQS?<\/strong><\/h2>\n<ul>\n<li>Amazon SQS(Simple Queue Service) is a fully managed message queuing service that allows us to communicate asynchronously between the different components of our application.<\/li>\n<li>We can send, store, and receive messages between software components. It can handle any amount of data, dynamically scaling to match the throughput of our application.<\/li>\n<li>Using SQS, we pay only for what you use. There are no upfront costs or minimum fees, and we are charged based on the number of requests and the amount of data transferred.<\/li>\n<\/ul>\n<h2><strong>How SQS works?<\/strong><\/h2>\n<p>Simple Queue Service basically involves three main components: producers, consumers, and queue<\/p>\n<ul>\n<li><strong>Producers<\/strong>: These are the components that send messages to the queue. This can be any part of your application that needs to communicate with another component.<\/li>\n<li><strong>Queues<\/strong>: The queue is the temporary storage for messages. Producers send messages to the queue, where they are stored until they are processed by consumers.<\/li>\n<li><strong>Consumers<\/strong>: These are the components that receive and process messages from the queue. Consumers check the queue for new messages and process them as they become available.<\/li>\n<\/ul>\n<p>Integrating Simple Queue Service with Spring Boot<\/p>\n<h3><strong>\u00a01. <span style=\"text-decoration: underline;\">Creating an SQS Queue :<\/span><\/strong><\/h3>\n<p>Firstly, we need to create a queue by logging in to the AWS Management Console configure the queue settings there and create the queue.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-64561 size-large aligncenter\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-from-2024-08-02-14-23-01-1-1024x527.png\" alt=\"ff\" width=\"625\" height=\"322\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-from-2024-08-02-14-23-01-1-1024x527.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-from-2024-08-02-14-23-01-1-300x154.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-from-2024-08-02-14-23-01-1-768x395.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-from-2024-08-02-14-23-01-1-1536x790.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-from-2024-08-02-14-23-01-1-624x321.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-from-2024-08-02-14-23-01-1.png 1917w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-64560 size-large aligncenter\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-from-2024-08-02-14-23-24-1024x544.png\" alt=\"gg\" width=\"625\" height=\"332\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-from-2024-08-02-14-23-24-1024x544.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-from-2024-08-02-14-23-24-300x159.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-from-2024-08-02-14-23-24-768x408.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-from-2024-08-02-14-23-24-1536x816.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-from-2024-08-02-14-23-24-624x332.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/08\/Screenshot-from-2024-08-02-14-23-24.png 1910w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<h3><span style=\"text-decoration: underline;\"><strong>2. Setting up Spring Boot project :<\/strong><\/span><\/h3>\n<p>Create a new Spring Boot project using IDE or Spring Initializr and then add the necessary dependencies to the project<\/p>\n<p>Using Gradle:<\/p>\n<pre style=\"text-align: justify;\">dependencies {\r\nimplementation 'org.springframework:spring-messaging:5.3.27'\r\nimplementation 'software.amazon.awssdk:sqs:2.26.8'\r\nimplementation 'io.awspring.cloud:spring-cloud-aws-starter-sqs'\r\n}<\/pre>\n<h4>\u00a03. <span style=\"text-decoration: underline;\">Add Configuration of your AWS Credentials:<\/span><\/h4>\n<p>Ensure your AWS credentials are configured. Add your credentials in application.properties or use the AWS CLI configuration.<\/p>\n<pre>aws.access.key.id=your-access-key\r\naws.secret.access.key=your-secret-key\r\naws.region=your-region\r\n<\/pre>\n<h3><strong><span style=\"text-decoration: underline;\">4. Create Configuration Class :<\/span><\/strong><\/h3>\n<p>Create a configuration class to set up the SQS client and the queue.<\/p>\n<pre>@Configuration\r\npublic class SQSConfig {\r\n\r\n@Bean\r\npublic SqsClient createSqsClient() {\r\nreturn SqsClient.builder()\r\n.credentialsProvider(DefaultCredentialsProvider.create())\r\n.region(Region.of(\"your_region\"))\r\n.build();\r\n}\r\n}<\/pre>\n<h3>5. <strong><span style=\"text-decoration: underline;\">Send Messages to SQS<\/span><\/strong><\/h3>\n<p>Create a service class to send messages to the SQS queue.<\/p>\n<pre>@Service\r\npublic class SqsService {\r\n\r\nprivate final String queueUrl; (\/\/ Specify your queue URL in the config file)\r\n\r\npublic void sendMessageToQueue(String messageBody) {\r\nlog.info(\"Sending msg to SQS queue\");\r\ntry {\r\nSqsClient sqsClient = sqsConfig.createSqsClient();\r\nString jsonMessage;\r\ntry {\r\njsonMessage = objectMapper.writeValueAsString(messageBody);\r\n} catch (JsonProcessingException e) {\r\nlog.error(\"Error converting message to JSON: {}\", e.getMessage());\r\nreturn;\r\n}\r\nSendMessageRequest sendMessageRequest = SendMessageRequest.builder()\r\n.queueUrl(queueUrl)\r\n.messageBody(jsonMessage)\r\n.build();\r\nsqsClient.sendMessage(sendMessageRequest);\r\nlog.info(\"Message sent to SQS queue\");\r\n} catch (Exception e) {\r\nlog.error(\"Error sending message to SQS queue: {}\", e.getMessage());\r\n}\r\n}\r\n}<\/pre>\n<h4><span style=\"text-decoration: underline;\"><strong>6. Receive Messages from SQS:<\/strong><\/span><\/h4>\n<p>Create a service class to receive messages from the SQS queue.<\/p>\n<pre>@Component(\"sqsConsumer\")\r\n@RequiredArgsConstructor\r\npublic class SQSConsumer {\r\n\r\n@SqsListener(\"your_queue_name\")\r\npublic void consumeQueueMessages(String message) {\r\nlog.info(\"Received message from SQS queue: {}\", message);\r\nprocessMessage(message);\r\n}\r\n\r\nprivate void processMessage(String messageBody) {\r\ntry {\r\nlog.info(\"Started processing queue message.\");\r\n\/\/ You can specify your logic for processing the message according to requirements\r\n} catch (Exception e) {\r\nlog.error(\"Error processing message from SQS queue: {}\", e.getMessage(), e);\r\n}\r\n}\r\n}<\/pre>\n<h3><span style=\"text-decoration: underline;\"><strong>7. Application Properties<\/strong><\/span><\/h3>\n<p>You can add the queue URL to your application.properties or application.yml file<\/p>\n<pre>sqs.queue.url=\"your_queue_url\"<\/pre>\n<h2>\nAmazon SQS vs. Other Messaging Systems: Quick Reference<\/h2>\n<ul>\n<li><strong>Polling and Long Polling<\/strong> : Amazon SQS uses a polling model to retrieve messages. By default, it uses short polling, which returns immediately even if no messages are available. This reduces the number of empty responses and helps lower costs by reducing the number of requests made to SQS, on the other hand, RabbitMQ and Kafka require management of the broker, clustering, and scaling.<\/li>\n<li><strong>Message Delivery<\/strong> : SQS provides at least-once delivery for Standard queues and exactly once delivery for FIFO queues. while Kafka supports both at-least-once and exactly-once delivery semantics depending on the configuration.<\/li>\n<li><strong>Scalability<\/strong> : SQS scales automatically with the volume of messages, while RabbitMQ and Kafka require manual scaling to add nodes or configure clusters.<\/li>\n<li><strong>Durability<\/strong> : In SQS messages are stored across multiple AWS Availability Zones, while in\u00a0 RabbitMQ and Kafka messages and queues can be configured to be durable to withstand broker failures.<\/li>\n<\/ul>\n<h2><strong><br \/>\nConclusion<\/strong><\/h2>\n<p>By following the above steps, you can effectively integrate SQS with your Spring Boot application, enabling reliable and scalable messaging capabilities. Also, ensure that messages are deleted from the queue after successful processing. Moreover, you can implement proper error handling for message processing and retries.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Amazon SQS? Amazon SQS(Simple Queue Service) is a fully managed message queuing service that allows us to communicate asynchronously between the different components of our application. We can send, store, and receive messages between software components. It can handle any amount of data, dynamically scaling to match the throughput of our application. Using [&hellip;]<\/p>\n","protected":false},"author":1685,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":81},"categories":[446],"tags":[6336,2072,6335,6334],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/64507"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1685"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=64507"}],"version-history":[{"count":14,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/64507\/revisions"}],"predecessor-version":[{"id":65553,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/64507\/revisions\/65553"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=64507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=64507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=64507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}