JMS specifications
JMS session objects are single threaded and can only be used by single thread 
that created them. An attempt to use them from another thread would result in 
an exception or some very strange errors.




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


From: Li Li [via ActiveMQ]
Date: 2014-01-22 18:51
To: kimmking
Subject: multithreads share a session problem
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. 





If you reply to this email, your message will be added to the discussion below:
http://activemq.2283324.n4.nabble.com/multithreads-share-a-session-problem-tp4676622.html
 
To unsubscribe from ActiveMQ - User, click here.
NAML 



--
View this message in context: 
http://activemq.2283324.n4.nabble.com/multithreads-share-a-session-problem-tp4676622p4676623.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Reply via email to