Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (5.6k points)
I'm trying to work with queues, I successfully posted messages and receive them synchronously. However, I'm trying to async.

I went through some links by SQS that suggest using the JMS client wrapper and it also mentions using it if you have a code that is integrated into a JMS client. But as I'm starting fresh, so any code example would be appreciated.

1 Answer

0 votes
by (12.4k points)

Providing the sample code using JMS with Amazon SQS that show how to asynchronously receive messages using JMS.

First, you need 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.

            if (message != null) {

                System.out.println("Received: " + ((TextMessage) message).getText());

            }

        } catch (JMSException e) {

            e.printStackTrace();

        }

    }

}

Then you need to set it as the MessageListener for a MessageConsumer:

// Create a consumer for the 'TestQueue'.

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 will be invoked when a message is received.

Thread.sleep(1000);

Do you want to master AWS? Then do check out the AWS Certified Solutions Architect Professional certification by Intellipaat.

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer
asked Nov 22, 2020 in AWS by devin (5.6k points)

Browse Categories

...