On 01/15/2015 09:16 PM, artnaseef wrote:
How about the following instead?  As Tim pointed out, the consumer close
won't return until no more calls to onMessage are guaranteed.  I believe
that also means it won't return while onMessage() is actively being called.

@Tim - can you confirm?

It should be the case that onMessage must always complete prior to close() returning, unless you do something silly like call close from onMessage. There are two locks at play here that prevent things from going wrong, one on the listener and one on the dispatch queue. This is why it is also safe to just call setMessageListener(null) on the consumer as it will also not return until no onMessage calls are in progress. During the time there is no set message listener the messages will pile up in the prefetch queue and dispatch would start again once a new listener is added.


If that's correct, the following should do the job effectively:

ON MESSAGE
     LOCK semaphore
     IF shutdown started
     THEN
        ABORT
     END IF

     SET processing active
     UNLOCK semaphore

     DO process message

     LOCK semaphore
       SET processing NOT active
     UNLOCK semaphore


ON LISTENER SHUTDOWN
     LOCK semaphore
       SET shutdown started
     UNLOCK semaphore

     DO close consumer

     SET not done
     WHILE not done
       LOCK semaphore
         IF processing NOT active
         THEN
           SET done
         END IF
       UNLOCK semaphore

       IF NOT done
       THEN
         DELAY // short sleep; note there are methods to eliminate the sleep
and use a notification; I don't recall them for C++ right now
       END IF
     END WHILE

     DO delete message listener


That last part about the delay can be solved with another semaphore as well.
Also, if the part up to "ABORT" in the listener can be done safely even
after the listener is deleted, then this works even if the consumer close
can return while the call to onMessage() is still active.



--
View this message in context: 
http://activemq.2283324.n4.nabble.com/how-to-safely-delete-a-cms-MessageListener-object-tp4689939p4690003.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.



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

Reply via email to