You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by Li Li <fa...@gmail.com> on 2014/01/23 03:58:13 UTC

why only one thread working?

public class Worker implements MessageListener{
    public Worker() throws Exception{
        recvConnFactory = new ActiveMQConnectionFactory(
                queueConnString);
        recvQConn = recvConnFactory.createQueueConnection();
        recvQConn.start();
        recvSession = recvQConn.createQueueSession(false,
Session.CLIENT_ACKNOWLEDGE);
        Queue queue = recvSession.createQueue("queue");
        QueueReceiver receiver = recvSession.createReceiver(queue);
        receiver.setMessageListener(this);
    }
    @Override
    public void onMessage(Message message) {
        this.doWork(message);
    }
}

public class Main{
     public static void main(String[] args){
     Worker[] workers=new Worker[10];
     for(int i=0;i<workers.length;i++){
         workers[i]=new Worker();
     }
     //sleep and wait

}

Each woker has its own connection and session.
but it seems only one worker are running.
What's wrong with it?
need I wrapper Worker in a it's own thread?

Re: why only one thread working?

Posted by Timothy Bish <ta...@gmail.com>.
On 01/22/2014 10:52 PM, Li Li wrote:
> I found the problem. I have only 160 messages and all are dispatched
> to a single worker.
> how to avoid this?

You need to set the consumer prefetch.
http://activemq.apache.org/what-is-the-prefetch-limit-for.html

>
> On Thu, Jan 23, 2014 at 11:27 AM, Li Li <fa...@gmail.com> wrote:
>> I modified it to use multithreads, but still only one work.
>> I use jstack and find other threads are blocked by Message msg =
>> receiver.receive();
>>
>> public class Worker extends Thread{
>>      public Worker() throws Exception{
>>          recvConnFactory = new ActiveMQConnectionFactory(
>>                  queueConnString);
>>          recvQConn = recvConnFactory.
>> createQueueConnection();
>>          recvQConn.start();
>>          recvSession = recvQConn.createQueueSession(false,
>> Session.CLIENT_ACKNOWLEDGE);
>>          Queue queue = recvSession.createQueue("queue");
>>          receiver = recvSession.createReceiver(queue);
>>
>>      }
>>      @Override
>>      public void run() {
>>          while (true) {
>>              try {
>>                  Message msg = receiver.receive();
>>                  if(!this.doWork(msg)){
>>                      break;
>>                  }
>>              } catch (JMSException e) {
>>                  logger.error(e.getMessage(), e);
>>              }
>>          }
>>      }
>> }
>>
>> public class Main{
>>       public static void main(String[] args){
>>       Thread[] workers=new Worker[10];
>>       for(int i=0;i<workers.length;i++){
>>           workers[i]=new Worker();
>>           workers[i].start();
>>       }
>>       //sleep and wait
>>      for(int i=0;i<workers.length;i++){
>>         workers[i].join();
>>      }
>> }
>>
>> On Thu, Jan 23, 2014 at 11:12 AM, kimmking <ki...@gmail.com> wrote:
>>> Because you have only one thread -- main thread, no more threads created.
>>>
>>>
>>>
>>>
>>> Beijing,China
>>> Kimm King
>>> skype: kimmking
>>> github.com/kimmking
>>>
>>>
>>>
>>> --
>>> View this message in context: http://activemq.2283324.n4.nabble.com/why-only-one-thread-working-tp4676688p4676689.html
>>> Sent from the ActiveMQ - User mailing list archive at Nabble.com.


-- 
Tim Bish
Sr Software Engineer | RedHat Inc.
tim.bish@redhat.com | www.fusesource.com | www.redhat.com
skype: tabish121 | twitter: @tabish121
blog: http://timbish.blogspot.com/


Re: why only one thread working?

Posted by Li Li <fa...@gmail.com>.
I found the problem. I have only 160 messages and all are dispatched
to a single worker.
how to avoid this?

On Thu, Jan 23, 2014 at 11:27 AM, Li Li <fa...@gmail.com> wrote:
> I modified it to use multithreads, but still only one work.
> I use jstack and find other threads are blocked by Message msg =
> receiver.receive();
>
> public class Worker extends Thread{
>     public Worker() throws Exception{
>         recvConnFactory = new ActiveMQConnectionFactory(
>                 queueConnString);
>         recvQConn = recvConnFactory.
> createQueueConnection();
>         recvQConn.start();
>         recvSession = recvQConn.createQueueSession(false,
> Session.CLIENT_ACKNOWLEDGE);
>         Queue queue = recvSession.createQueue("queue");
>         receiver = recvSession.createReceiver(queue);
>
>     }
>     @Override
>     public void run() {
>         while (true) {
>             try {
>                 Message msg = receiver.receive();
>                 if(!this.doWork(msg)){
>                     break;
>                 }
>             } catch (JMSException e) {
>                 logger.error(e.getMessage(), e);
>             }
>         }
>     }
> }
>
> public class Main{
>      public static void main(String[] args){
>      Thread[] workers=new Worker[10];
>      for(int i=0;i<workers.length;i++){
>          workers[i]=new Worker();
>          workers[i].start();
>      }
>      //sleep and wait
>     for(int i=0;i<workers.length;i++){
>        workers[i].join();
>     }
> }
>
> On Thu, Jan 23, 2014 at 11:12 AM, kimmking <ki...@gmail.com> wrote:
>>
>> Because you have only one thread -- main thread, no more threads created.
>>
>>
>>
>>
>> Beijing,China
>> Kimm King
>> skype: kimmking
>> github.com/kimmking
>>
>>
>>
>> --
>> View this message in context: http://activemq.2283324.n4.nabble.com/why-only-one-thread-working-tp4676688p4676689.html
>> Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: why only one thread working?

Posted by Li Li <fa...@gmail.com>.
I modified it to use multithreads, but still only one work.
I use jstack and find other threads are blocked by Message msg =
receiver.receive();

public class Worker extends Thread{
    public Worker() throws Exception{
        recvConnFactory = new ActiveMQConnectionFactory(
                queueConnString);
        recvQConn = recvConnFactory.
createQueueConnection();
        recvQConn.start();
        recvSession = recvQConn.createQueueSession(false,
Session.CLIENT_ACKNOWLEDGE);
        Queue queue = recvSession.createQueue("queue");
        receiver = recvSession.createReceiver(queue);

    }
    @Override
    public void run() {
        while (true) {
            try {
                Message msg = receiver.receive();
                if(!this.doWork(msg)){
                    break;
                }
            } catch (JMSException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
}

public class Main{
     public static void main(String[] args){
     Thread[] workers=new Worker[10];
     for(int i=0;i<workers.length;i++){
         workers[i]=new Worker();
         workers[i].start();
     }
     //sleep and wait
    for(int i=0;i<workers.length;i++){
       workers[i].join();
    }
}

On Thu, Jan 23, 2014 at 11:12 AM, kimmking <ki...@gmail.com> wrote:
>
> Because you have only one thread -- main thread, no more threads created.
>
>
>
>
> Beijing,China
> Kimm King
> skype: kimmking
> github.com/kimmking
>
>
>
> --
> View this message in context: http://activemq.2283324.n4.nabble.com/why-only-one-thread-working-tp4676688p4676689.html
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: why only one thread working?

Posted by kimmking <ki...@gmail.com>.
Because you have only one thread -- main thread, no more threads created.
 



Beijing,China 
Kimm King 
skype: kimmking
github.com/kimmking



--
View this message in context: http://activemq.2283324.n4.nabble.com/why-only-one-thread-working-tp4676688p4676689.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.