This post shows how to use Amazon Simple Queue Service (SQS) in Java with Eclipse using the AWS Toolkit Plugin for the IDE. It also shows how to create queues and send and receive messages from them.
Java, Amazon Simple Queue Service, And Other Requirements
We used the following items for this post.
- An AWS Account
- Fast Internet Connection
- Eclipse Mars.2 Release (4.5.2)
- The AWS Toolkit Plugin for Eclipse
- Java 8 – JDK
- AWS SDK for Java
- Version: 1.11.205
Step 1 – Create A Queue In Amazon Simple Queue Service
Before we create a queue in Amazon Simple Queue Service (SQS), we need an AWS account. Then, proceed with the queue creation either using the AWS Management Console, Eclipse (using the AWS Toolkit Plugin), or via Java codes.
To create via the AWS Management Console, sign in to the AWS account and proceed to the Simple Queue Service (SQS). Then, click the Get Started Now button.
Alternatively, we can create a queue in Amazon Simple Queue Service (SQS) in Eclipse for Java using the AWS Toolkit Plugin. However, the plugin may require providing a credential to an AWS account.
Then, proceed to the AWS Explorer tab, choose Amazon SQS, and click the Create New Queue option.
On the other hand, we create a queue in the Amazon Simple Queue Service using Java codes. Consider the following sample codes. However, this assumes we are using a dependency on the AWS SDK for Java.
1 2 3 4 5 6 7 | // NOTE: Using software.amazon.awssdk / sqs version 2.0.0-preview-1 String queueName = "TURRETA-QUE002"; SQSClient sqsClient = SQSClient.builder().region(Region.AP_SOUTHEAST_1).build(); // Create the queue CreateQueueRequest createQueueRequest = CreateQueueRequest.builder().queueName(queueName).build(); sqsClient.createQueue(createQueueRequest); |
Step 2 – Create an AWS Project
Next, we create an AWS project in Eclipse.
Then, choose AWS Java Project and click Next.
Then, provide the Project name, Group ID, and Artifact ID. Optionally, we can ignore the checkboxes in the AWS SDK for Java Samples. Finally, click Finish.
Wait until the creation process completes.
Step 3 – Create simple Java Codes
Next, we craft some Java codes to work with the Amazon Simple Queue Service. Consider the following codes that send messages to a queue.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package com.turreta.aws.sqs; import com.amazonaws.regions.Regions; import com.amazonaws.services.sqs.*; import com.amazonaws.services.sqs.model.SendMessageRequest; public class DemoSQS { public static void main(String[] args) { AmazonSQSClientBuilder f = AmazonSQSClient.builder(); f.setRegion(Regions.AP_SOUTHEAST_1.getName()); // Build the SQS Client AmazonSQS amazonSQS = f.build(); // Create a message request SendMessageRequest req = new SendMessageRequest(); // Specify the URL of our queue req.setQueueUrl("https://sqs.ap-southeast-1.amazonaws.com/854968627112/TURRETA-QUE001"); // Specify the message content req.setMessageBody("Hello Turreta.com!"); req.setDelaySeconds(10); // Send the message amazonSQS.sendMessage(req); System.out.println("Message sent!"); } } |
We get the following output on the IDE console when we run the codes.
Then, we can check if the Amazon Simple Queue Service received our message.
Java Codes to read Messages from a Queue
The following are simple Java codes that retrieve the messages from our SQQ queue in the cloud.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package com.turreta.aws.sqs; import java.util.List; import com.amazonaws.regions.Regions; import com.amazonaws.services.sqs.*; import com.amazonaws.services.sqs.model.ChangeMessageVisibilityRequest; import com.amazonaws.services.sqs.model.DeleteMessageRequest; import com.amazonaws.services.sqs.model.Message; import com.amazonaws.services.sqs.model.ReceiveMessageRequest; public class DemoSQS_ReadQueue { public static void main(String[] args) { String queueUrl = "https://sqs.ap-southeast-1.amazonaws.com/854968627112/TURRETA-QUE001"; AmazonSQSClientBuilder f = AmazonSQSClient.builder(); f.setRegion(Regions.AP_SOUTHEAST_1.getName()); // Build the SQS Client AmazonSQS amazonSQS = f.build(); System.out.println("Receive messages from queue:"); ReceiveMessageRequest rcvMsgReq = new ReceiveMessageRequest(); rcvMsgReq.setQueueUrl(queueUrl); rcvMsgReq.setMaxNumberOfMessages(5); List<Message> messages= amazonSQS.receiveMessage(rcvMsgReq).getMessages(); for(Message msg: messages) { System.out.println(msg.getBody()); ChangeMessageVisibilityRequest cmvReq = new ChangeMessageVisibilityRequest(); cmvReq.setQueueUrl(queueUrl); cmvReq.setReceiptHandle(msg.getReceiptHandle()); cmvReq.setVisibilityTimeout(10); amazonSQS.changeMessageVisibility(cmvReq); // Delete the current message DeleteMessageRequest dmReq = new DeleteMessageRequest(); dmReq.setQueueUrl(queueUrl); dmReq.setReceiptHandle(msg.getReceiptHandle()); amazonSQS.deleteMessage(dmReq); } } } |
This outputs:
Our queue will be empty.
Download the codes
For more information, please download the Java codes that send a message to Amazon Simple Queue Service on GitHub. Also, check out the following links for additional references.