You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by Don Santillan <do...@gmail.com> on 2011/05/02 11:50:31 UTC

Question on consumers

Hello,

I have a basic question about message consumers that I really need to 
confirm that my understanding is correct.

Do message consumers run in different threads?

Suppose I have 5 consumers assigned to a queue. Are these consumers 
independent with each other and are running in different threads?

Thanks!

Re: Question on consumers

Posted by Don Santillan <do...@gmail.com>.
Thanks a lot Martin. This is very helpful.

With this, I'd like to verify if my understanding is correct through the 
following code snippet:

ActiveMQConnectionFactory connectionFactory = new 
ActiveMQConnectionFactory(brokerURI);

Connection connection = connectionFactory.createConnection();

Destination destination = connection.createSession(isTransacted, 
acknowledgeMode).createQueue(queueName);

MessageProducer producer = connection.createSession(isTransacted, 
acknowledgeMode).createProducer(destination);

MessageConsumer consumer1 = connection.createSession(isTransacted, 
acknowledgeMode).createConsumer(destination);
consumer1.setMessageListener(new ConcreteMessageListenerImpl());
MessageConsumer consumer2 = connection.createSession(isTransacted, 
acknowledgeMode).createConsumer(destination);
consumer2.setMessageListener(new ConcreteMessageListenerImpl());
MessageConsumer consumer3 = connection.createSession(isTransacted, 
acknowledgeMode).createConsumer(destination);
consumer3.setMessageListener(new ConcreteMessageListenerImpl());
MessageConsumer consumer4 = connection.createSession(isTransacted, 
acknowledgeMode).createConsumer(destination);
consumer4.setMessageListener(new ConcreteMessageListenerImpl());
MessageConsumer consumer5 = connection.createSession(isTransacted, 
acknowledgeMode).createConsumer(destination);
consumer5.setMessageListener(new ConcreteMessageListenerImpl());

Thanks!
Don

On Tuesday, 03 May, 2011 08:13 PM, Martin C. wrote:
> Hi,
>
> the JMS threading model is based around the Session. Each Session is
> to be used by one thread. If you are using asynchronous mode (you
> register a MessageListener with the Session), every Session uses one
> thread or is to be used by one thread
> (http://download.oracle.com/javaee/1.4/api/javax/jms/Session.html). If
> you attach 5 consumers to a single session, only one of them will be
> called. You need 5 Sessions with each 1 consumer in order to feed 5
> consumers in parallel in asynchronous mode.
>
> This is not strictly speaking the truth, because in theory you may use
> a Session with more than one thread, but you need to ensure proper
> synchronization. If you are using asynchronous messaging, the
> following from http://download.oracle.com/javaee/1.4/api/javax/jms/MessageListener.html
> holds:
> <quote>Each session must insure that it passes messages serially to
> the listener. This means that a listener assigned to one or more
> consumers of the same session can assume that the onMessage method is
> not called with the next message until the session has completed the
> last call.</quote>
>
> So if you want to go parallel, you need to create more Sessions and
> MessageConsumers.
>
> Best regards,
> Martin
>
> On Mon, May 2, 2011 at 11:50 AM, Don Santillan<do...@gmail.com>  wrote:
>> Hello,
>>
>> I have a basic question about message consumers that I really need to
>> confirm that my understanding is correct.
>>
>> Do message consumers run in different threads?
>>
>> Suppose I have 5 consumers assigned to a queue. Are these consumers
>> independent with each other and are running in different threads?
>>
>> Thanks!
>>

Re: Question on consumers

Posted by "Martin C." <ma...@gmx.at>.
Hi,

the JMS threading model is based around the Session. Each Session is
to be used by one thread. If you are using asynchronous mode (you
register a MessageListener with the Session), every Session uses one
thread or is to be used by one thread
(http://download.oracle.com/javaee/1.4/api/javax/jms/Session.html). If
you attach 5 consumers to a single session, only one of them will be
called. You need 5 Sessions with each 1 consumer in order to feed 5
consumers in parallel in asynchronous mode.

This is not strictly speaking the truth, because in theory you may use
a Session with more than one thread, but you need to ensure proper
synchronization. If you are using asynchronous messaging, the
following from http://download.oracle.com/javaee/1.4/api/javax/jms/MessageListener.html
holds:
<quote>Each session must insure that it passes messages serially to
the listener. This means that a listener assigned to one or more
consumers of the same session can assume that the onMessage method is
not called with the next message until the session has completed the
last call.</quote>

So if you want to go parallel, you need to create more Sessions and
MessageConsumers.

Best regards,
Martin

On Mon, May 2, 2011 at 11:50 AM, Don Santillan <do...@gmail.com> wrote:
> Hello,
>
> I have a basic question about message consumers that I really need to
> confirm that my understanding is correct.
>
> Do message consumers run in different threads?
>
> Suppose I have 5 consumers assigned to a queue. Are these consumers
> independent with each other and are running in different threads?
>
> Thanks!
>

Re: Question on consumers

Posted by Don Santillan <do...@gmail.com>.
bump

On Monday, 02 May, 2011 05:50 PM, Don Santillan wrote:
> Hello,
>
> I have a basic question about message consumers that I really need to 
> confirm that my understanding is correct.
>
> Do message consumers run in different threads?
>
> Suppose I have 5 consumers assigned to a queue. Are these consumers 
> independent with each other and are running in different threads?
>
> Thanks!