Skip to content
Advertisement

Better way to wait to receive Async messages in ActiveMQ

I have used ActiveMQ to Send messages and Receive them Asynchronously.

There, I’m having a problem with deciding the best way to waiting in the for messages. Sleeping thread in a loop is one option. But it feels doesn’t look good for me.

Can anyone suggest a better way for this.

AsyncReceiver.java

JavaScript

Sender.java

JavaScript

Advertisement

Answer

There are two ways to process/consume messages in the Queue.

  1. Periodically check the queue for new messages – This is suitable if you run your program periodically. You can do this by implementing a loop with some thread sleeps. Ex. twice a day, once a day etc.

  2. Register consumers (use MessageListener) with the queue. You can do this as below example.

Consumer.java

JavaScript

YourClass.java

JavaScript

}

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement