Data is one of the essential requirements for every business to grow in this competitive environment. Access to high-quality volumes of data gives an edge to companies.

  • Modern applications generate huge volumes of data that need to be fed to other software components or user end.
  • To satisfy the application’s requirements and ensure a smooth experience. companies use a messaging queue for delivering messages efficiently.
  • AWS SQS is a fully managed messaging queue service offered by Amazon to help companies scale up their application or microservices.
  • It also helps them decouple various software components to independently run them.
  • AWS SQS can handle high throughput messages of any volume.
  • Most applications are built using Java. Developers use AWS SQS Java to build messaging queue services for their applications because it is secure, durable, and scalable.

What is AWS SQS?

Amazon SQS Logo
  • AWS SQS (Simple Queue Service) is a durable, scalable, and fully managed messaging queue that allows Developers to decouple and scale microservices, serverless applications, and distributed systems.
  • It can programmatically send messages through web service applications as a way to communicate over the internet. Using AWS SQS, users can easily eliminate the complexity and overhead bound with managing, organizing, and operating middleware software designed for message communication.
  • AWS SQS helps users to send, receive and store messages software components at any volume making the overall application decoupled.
  • Users don’t need to use any other software for managing messages without losing messages. AWS SQS comes with two types of message queues.

Key Features of AWS SQS

Some of the main features of AWS SQS are listed below:

  • Scalability: AWS SQS enables companies to scale their applications as it can process all the buffered message requests of any volume independently. Moreover, it can automatically scale up the resources to handle the sudden load spikes without the need for any prior instructions.
  • Durability: AWS SQS uses multiple servers to store your messages in a safe place. With Standard Queues and FIFO Queues, AWS SQS guarantees at least one delivery of messages.
  • Availability: AWS SQS offers a high performance by leveraging redundant infrastructure to deliver high-speed concurrent messaging. This way, it ensures that you experience an increased availability of messages for both production and consumption.
  • Queue Sharing: AWS SQS allows users to securely share queues anonymously or using an AWS account. Queue sharing can also be restricted by IP address and time of day.

What is Java?

Java Logo
  • Java is a class-based, high-level, and object-oriented programming language.
  • It was first released publicly by Sun Microsystems in 1996 but later acquired by Oracle Corporation. Java programs are compiled to bytecode that runs on JVM (Java Virtual Machine) allowing programmers to run the same piece of code on any platform making it platform-independent. 

Key Features of Java

Some of the main features of Java are listed below:

  • Multithreaded: Java can execute multiple tasks simultaneously making the applications smoother and fast.
  • Platform Independent: Java compiles into byte code that is platform-independent and then runs on JVM instead of a platform-specific machine that allows users to run single code on any platform.
  • Easy to Use: Java follows OOPs concepts and logic making it simple and easy-to-learn programming.

Creating an AWS SQS Java Messaging Library

  • Now that you have a brief knowledge of AWS SQS. In this section, you will learn about the steps to create AWS SQS Java Messaging Service Connection and how to send and receive messages using AWS SQS Java.
  • In this tutorial to AWS SQS Java, you create a wrapped SQS client object included in the AWS SQS Java Messaging Library that checks if a queue exists or not. if the AWS SQS Queue doesn’t exist then the client will create it.
  • The following steps to create an AWS SQS Java Messaging Library are listed below:

Step 1: Creating a Java Messaging Service connection

First, you need to create a connection factory and call the createConnection method. The following code is given below:

// Create a new connection factory with all defaults (credentials and region) set automatically
SQSConnectionFactory connectionFactory = new SQSConnectionFactory(
        new ProviderConfiguration(),
        AmazonSQSClientBuilder.defaultClient()
        );
 
// Create the connection.
SQSConnection connection = connectionFactory.createConnection();
Java to SQS Connection Successful

In the above code, the SQSConnection class extends the javax.jms.Connection. AWS SQS java Messaging Service Standard connection methods, SQSConnection offers getAmazonSQSClient and getWrappedAmazonSQSClient. Both the operations allow users to perform administrative operations such as creating new queues. 

If you have existing code that expects AWS SQS java Messaging Service exceptions then use should consider using the getWrappedAmazonSQSClient method.

Step 2: Creating an Amazon SQS queue

In this, we are going to create a wrapped client object that will check if the queue exists. If an AWS SQS queue doesn’t exist, the client will create a new queue and if the queue does exist, the function doesn’t return anything. 

The code to create a Standard queue is given below:

// Get the wrapped client
AmazonSQSMessagingClientWrapper client = connection.getWrappedAmazonSQSClient();
 
// Create an SQS queue named MyQueue, if it doesn't already exist
if (!client.queueExists("MyQueue")) {
    client.createQueue("MyQueue");
}
Queue Creation Successfull

The code to create a FIFO queue is given below:

// Get the wrapped client
AmazonSQSMessagingClientWrapper client = connection.getWrappedAmazonSQSClient();

// Create an Amazon SQS FIFO queue named MyQueue.fifo, if it doesn't already exist
if (!client.queueExists("MyQueue.fifo")) {
    Map<String, String> attributes = new HashMap<String, String>();
    attributes.put("FifoQueue", "true");
    attributes.put("ContentBasedDeduplication", "true");
    client.createQueue(new CreateQueueRequest().withQueueName("MyQueue.fifo").withAttributes(attributes));
}

Step 3: Sending Messages Synchronously

Once the AWS SQS java Messaging Service connection and the AWS SQS queue are ready. You can create a non transacted AWS SQS Java Messaging Service session with AUTO_ACKNOWLEDGE mode.

// Create the nontransacted session with AUTO_ACKNOWLEDGE mode
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

Now let’s create an AWS SQS java Messaging Service queue identity and a message producer for sending a text message to the queue. The following code for sending a message is given below:

// Create a queue identity and specify the queue name to the session
Queue queue = session.createQueue("MyQueue");
 
// Create a producer for the 'MyQueue'
MessageProducer producer = session.createProducer(queue);

Now let’s code a section where we will send a text message to the queue. For sending a message to a Standard queue, you don’t need any additional parameters.

// Create the text message
TextMessage message = session.createTextMessage("Hello World!");
 
// Send the message
producer.send(message);
System.out.println("AWS SQS java Messaging Service Message " + message.getJMSMessageID());

For sending a message to an AWS SQS FIFO queue, you have to set the message group ID. You can also set a message deduplication ID. The following code for sending a message to a FIFO queue is given below.

// Create the text message
TextMessage message = session.createTextMessage("Hello World!");

// Set the message group ID
message.setStringProperty("JMSXGroupID", "Default");

// You can also set a custom message deduplication ID
// message.setStringProperty("JMS_SQS_DeduplicationId", "hello");
// Here, it's not needed because content-based deduplication is enabled for the queue

// Send the message
producer.send(message);
System.out.println("AWS SQS java Messaging Service Message " + message.getJMSMessageID());
System.out.println("AWS SQS java Messaging Service Message Sequence Number " + message.getStringProperty("JMS_SQS_SequenceNumber"));
Sending message through SQS Successfull

Step 4: Receiving Message Synchronously

Let’s code for receiving messages sent by AWS SQS Java Messaging Service. For this, you need to create a consumer for the same queue. You can do this by invoking the start method. 

First, you have to call the start method on the connection at any time. But the consumer will not start receiving messages until you call it. The following code is given below.

// Create a consumer for the 'MyQueue'
MessageConsumer consumer = session.createConsumer(queue);
// Start receiving incoming messages
connection.start();

Once you started the connection, you can call the receive method with a timeout set to 1 second or more according to your preference. After that, you can print the content of the message that you received.

If you receive a message from a Standard queue, you can access the contents of the message using the following code given below.

// Receive a message from 'MyQueue' and wait up to 1 second
Message receivedMessage = consumer.receive(1000);
 
// Cast the received message as TextMessage and display the text
if (receivedMessage != null) {
    System.out.println("Received: " + ((TextMessage) receivedMessage).getText());
}

As FIFO queue contains other details such as message group ID, message deduplication ID, and sequence number other than the message. You can receive a message from the FIFO queue and access its contents using the following code given below.

// Receive a message from 'MyQueue' and wait up to 1 second
Message receivedMessage = consumer.receive(1000);

// Cast the received message as TextMessage and display the text
if (receivedMessage != null) {
    System.out.println("Received: " + ((TextMessage) receivedMessage).getText());
    System.out.println("Group id: " + receivedMessage.getStringProperty("JMSXGroupID"));
    System.out.println("Message deduplication id: " + receivedMessage.getStringProperty("JMS_SQS_DeduplicationId"));
    System.out.println("Message sequence number: " + receivedMessage.getStringProperty("JMS_SQS_SequenceNumber"));
}
Java received message successfully

Once you received a message, you can close the connection and the session using the following code given below.

// Close the connection (and the session).
connection.close();

The sample output of the received message will look similarly as shown below.

JMS Message ID:8example-588b-44e5-bbcf-d816example2
Received: Hello World!

Step 5: Receiving Message Asynchronously

Now let’s also have look at how to receive a message from AWS SQS Java asynchronously. Create a listener through which AWS SQS Java will receive a message. The following code is given below to implement the MessageListener interface.

class MyListener implements MessageListener {
 
    @Override
    public void onMessage(Message message) {
        try {
            // Cast the received message as TextMessage and print the text to screen.
            System.out.println("Received: " + ((TextMessage) message).getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

The onMessage method of the MessageListener interface is called when you will receive a message from the AWS SQS Java queue. 

You can set the listener of the consumer to an instance of the MyListener implementation instead of explicitly calling the receive method on the consumer. The main thread will wait for one second. The following code is given below.

// Create a consumer for the 'MyQueue'.
MessageConsumer consumer = session.createConsumer(queue);
 
// Instantiate and set the message listener for the consumer.
consumer.setMessageListener(new MyListener());
 
// Start receiving incoming messages.
connection.start();
 
// Wait for 1 second. The listener onMessage() method is invoked when a message is received.
Thread.sleep(1000);

The rest steps are similar to synchronous messaging.

Conclusion

In this article, you learned about AWS SQS and how to design an AWS SQS Java Messaging Service. You also read how to send messages synchronously and receive messages synchronously and asynchronously via the AWS SQS Java queue. AWS SQS is widely used by developers for scaling up their applications and decoupling them. You can use any other language than Java for designing and managing AWS SQS. 

Amazon Redshift and Amazon S3 store data from multiple sources and every source follow a different schema. Instead of manually writing scripts for every source to perform the ETL (Extract Transform Load) process, one can automate the whole process.

Want to take Hevo for a spin? Sign Up here for a 14-day free trial and experience the feature-rich Hevo suite first hand.

Share your experience of learning about AWS SQS Java in the comments section below!

Aditya Jadon
Research Analyst, Hevo Data

Aditya Jadon is a data science enthusiast with a passion for decoding the complexities of data. He leverages his B. Tech degree, expertise in software architecture, and strong technical writing skills to craft informative and engaging content. Aditya has authored over 100 articles on data science, demonstrating his deep understanding of the field and his commitment to sharing knowledge with others.