activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Li Li <fancye...@gmail.com>
Subject multithreads share a session problem
Date Wed, 22 Jan 2014 10:50:17 GMT
hi all
   I want to share a session among many threads and after do real work
the message is acknowledged:

   class Dispatcher  implements MessageListener{
        Thread[] workers;
        public void init(){
            session = connection.createQueueSession(false,
Session.CLIENT_ACKNOWLEDGE);
            Queue queue = session.createQueue("workingqueue");
            QueueReceiver receiver = session.createReceiver(queue);
            //initWorkers();

            receiver.setMessageListener(this);
        }
        public void onMessage(Message message) {
             send this message to a worker thread
        }

    }

    class Worker extends Thread{

          @Override
          public void run(){
               while(true){
                    //get a message from dispatcher
                    Message message=....
                    doWorker(message);
                    message.acknowledge();
                }
          }
    }

    After reading
http://docs.oracle.com/cd/E19717-01/819-7757/aeqeg/index.html and
http://stackoverflow.com/questions/1669746/jms-acknowledge-asynchronous-message
    I know it's wrong because Worker1 call message.acknowledge() will
also acknowledge Worker2's message--even it's not acknowledged by
worker2.

    So I have to let each Worker has its own session. But it seems not
very efficient.
    Any better solutions? Thank you.

Mime
View raw message