1

My JMS consumer produces any number(Say n) of messages on JMS queue during the day. First I am evaluating the synchronous processing of message

Say at 23.0 clock, now I want to consume all messages. Here is main method

Here's how to do it sequentially(not concurrently) :-

Do I need to call consumer.receive() method n times (till returns consumer.receive() return null )on single consumer?

            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
             // Create a Connection
            Connection connection = connectionFactory.createConnection();
            connection.start();

            // Create a Session
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // Create the destination (Topic or Queue)
            Destination destination = session.createQueue("TEST.FOO");

            // Create a MessageConsumer from the Session to the Topic or Queue
            MessageConsumer consumer = session.createConsumer(destination);

            // Wait for a message
            Message message = consumer.receive();

How to do it concurrently :- I want to process the 20 messages concurrently

Do I need to create 20 thread where each thread creates its own consumer and the receive the message?

lreeder
  • 12,047
  • 2
  • 56
  • 65
user3198603
  • 5,528
  • 13
  • 65
  • 125

1 Answers1

0

To process 20 messages sequentially, and you know you will receive at least 20 messages, put the MessageConsumer.receive() call into a loop 20 times. Note that MessageConsumer.receive() without a timeout argument will not return null if there are no messages on the queue. It will block until it receives a message, or until close() is called. If you use MessageConsumer.receive(longTimeoutValue), it will wait for longTimeoutValue to receive a message, and will return null if no message is received by then.

For concurrent message processing, the ActiveMQ docs provide a sample of how to use multiple consumers here: http://activemq.apache.org/hello-world.html, which you can modify for your purposes. The sample creates a new connection per thread, but according to http://activemq.apache.org/multiple-consumers-on-a-queue.html, you are only required to create a session and consumer per thread.

lreeder
  • 12,047
  • 2
  • 56
  • 65
  • @Ireeder As i said in my post that "each thread creates its own consumer " not a single consumer. So looks like i am not right path? – user3198603 Apr 06 '14 at 03:54
  • Each thread should create its own consumer, but it should also create it a separate session for each consumer. – lreeder Apr 06 '14 at 03:55
  • @Ireeder what about my first question where i am doing it sequentially. In that case i can use same consumer , just need to call receive multiple times. Right? – user3198603 Apr 06 '14 at 04:14
  • I missed that part of your question. My answer has been updated, but yes, basically just call receive multiple times as you expect. – lreeder Apr 06 '14 at 04:21
  • @ireeder. Cam you provide insight on this http://stackoverflow.com/questions/22890086/how-to-concurrently-process-the-asynchronous-jms-queue-message. Thanks in advance. – user3198603 Apr 06 '14 at 04:45