1
votes

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?

1
Have you tried your own suggestions? - SimonC
@Simon first one i tried but about second one i am not sure if it is right approach? - user3198603

1 Answers

0
votes

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.