You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by mrh <mr...@gmail.com> on 2007/10/25 22:43:24 UTC

Stop Thread / Stop Listening

With the threading capabilities provided by ActiveMQ-CPP, is it possible to
stop a thread?  For example, if one thread starts listening for messages, is
it possible for a second thread, say a command prompt to stop and shutdown
the listener thread when the user types "quit" or "exit"?

Thanks,
mrh
-- 
View this message in context: http://www.nabble.com/Stop-Thread---Stop-Listening-tf4693571s2354.html#a13415697
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Stop Thread / Stop Listening

Posted by Timothy Bish <ti...@sensis.com>.
All you need to do is close the consuer, i.e. myConsumer.close() and it
won't receive any more messages.  That is if you only want to stop that
consumer.  Otherwise if you are shutting down the app then close and
delete everything.

Regards
Tim.

On Fri, 2007-10-26 at 07:51 -0700, mrh wrote:
> In other words, if the asynchronous listener is listening "forever", is it
> possible to neatly stop that listener by closing the connection, the
> session, the consumer, etc?
> 
> --mrh
> 
> 
> mrh wrote:
> > 
> > Thank you for the reply, Tim.
> > 
> > Yes, this is my primary question:
> > 
> > 
> > tabish121 wrote:
> >> 
> >> make that it
> >> doesn't wait forever for messages and it stops as soon as it gets all
> >> the messages we told it to.  
> >> 
> > 
> > I would like for it to listen continuously for messages: no countdown, no
> > wait... just listen in the background while allowing the user to do other
> > things, until the user wants to exit the entire application.  Is the only
> > way to do this with threads?
> > 
> > Thanks again,
> > mrh
> > 
> > 
> > 
> > On Fri, 2007-10-26 at 06:49 -0700, mrh wrote:
> >> In the activemq-cpp example code, there is a countdown "latch".  If I
> >> understand it correctly, you can pass in a number of milliseconds that it
> >> is
> >> to wait for a message or a number that it will count down to as messages
> >> are
> >> received.  With this structure, is it possible to asychronously receive
> >> messages (using the onMessage function to process them) but stop the
> >> listener from the outside? Say, from an outside thread?
> >> 
> >> Thanks,
> >> mrh
> > 
> > 
> 
> 
> 

Re: Stop Thread / Stop Listening

Posted by mrh <mr...@gmail.com>.
In other words, if the asynchronous listener is listening "forever", is it
possible to neatly stop that listener by closing the connection, the
session, the consumer, etc?

--mrh


mrh wrote:
> 
> Thank you for the reply, Tim.
> 
> Yes, this is my primary question:
> 
> 
> tabish121 wrote:
>> 
>> make that it
>> doesn't wait forever for messages and it stops as soon as it gets all
>> the messages we told it to.  
>> 
> 
> I would like for it to listen continuously for messages: no countdown, no
> wait... just listen in the background while allowing the user to do other
> things, until the user wants to exit the entire application.  Is the only
> way to do this with threads?
> 
> Thanks again,
> mrh
> 
> 
> 
> On Fri, 2007-10-26 at 06:49 -0700, mrh wrote:
>> In the activemq-cpp example code, there is a countdown "latch".  If I
>> understand it correctly, you can pass in a number of milliseconds that it
>> is
>> to wait for a message or a number that it will count down to as messages
>> are
>> received.  With this structure, is it possible to asychronously receive
>> messages (using the onMessage function to process them) but stop the
>> listener from the outside? Say, from an outside thread?
>> 
>> Thanks,
>> mrh
> 
> 



-- 
View this message in context: http://www.nabble.com/Stop-Thread---Stop-Listening-tf4693571s2354.html#a13428693
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Stop Thread / Stop Listening

Posted by Timothy Bish <ti...@sensis.com>.
You need to get then out of SVN.

See this page for accessing the code.
http://activemq.apache.org/cms/source.html

The code is in these folders
https://svn.apache.org/repos/asf/activemq/activemq-cpp/trunk/src/examples/consumers/
https://svn.apache.org/repos/asf/activemq/activemq-cpp/trunk/src/examples/producers/

On Fri, 2007-10-26 at 11:25 -0700, mrh wrote:
> I can't seem to find the link to the examples... would you mind posting it
> here?
> 
> Thanks,
> mrh

Re: Stop Thread / Stop Listening

Posted by mrh <mr...@gmail.com>.
I can't seem to find the link to the examples... would you mind posting it
here?

Thanks,
mrh
-- 
View this message in context: http://www.nabble.com/Stop-Thread---Stop-Listening-tf4693571s2354.html#a13432796
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Stop Thread / Stop Listening

Posted by Timothy Bish <ti...@sensis.com>.
Sure thing.

I've added a SimpleAsyncConsumer and SimplProducer examples to the trunk
of ActiveMQ-CPP.  You can look at these to see example of each in
separate apps. 

Regards
Tim

On Fri, 2007-10-26 at 09:11 -0700, mrh wrote:
> That is exactly what I needed to know.  I misunderstood what the
> donelatch.await() line of code did in the activemq-example project.  I could
> not figure out why the entire program kept exiting when that line of code
> wasn't in there.  Now I see that if no other processing is taking place, the
> program exits without stopping the consumer correctly, which throws an
> exception.  Now I'm starting to understand.  Thanks again for all of the
> assistance!
> 
> mrh
> 
> 
> 
> tabish121 wrote:
> > 
> > When you use a MessageListener that listener is notified in a thread
> > context other than the main thread.  So in you client app you can set a
> > MessageListener and then it will receive messages until you kill the
> > app, so you can just have the main waiting on a cin.get() and it will
> > process message until you hit a key.
> > 
> > Regards.
> > Tim.
> > 
> > On Fri, 2007-10-26 at 07:36 -0700, mrh wrote:
> >> Thank you for the reply, Tim.
> >> 
> >> Yes, this is my primary question:
> >> 
> >> 
> >> tabish121 wrote:
> >> > 
> >> > make that it
> >> > doesn't wait forever for messages and it stops as soon as it gets all
> >> > the messages we told it to.  
> >> > 
> >> 
> >> I would like for it to listen continuously for messages: no countdown, no
> >> wait... just listen in the background while allowing the user to do other
> >> things, until the user wants to exit the entire application.  Is the only
> >> way to do this with threads?
> >> 
> >> Thanks again,
> >> mrh
> >> 
> >> 
> >> 
> >> On Fri, 2007-10-26 at 06:49 -0700, mrh wrote:
> >> > In the activemq-cpp example code, there is a countdown "latch".  If I
> >> > understand it correctly, you can pass in a number of milliseconds that
> >> it
> >> > is
> >> > to wait for a message or a number that it will count down to as
> >> messages
> >> > are
> >> > received.  With this structure, is it possible to asychronously receive
> >> > messages (using the onMessage function to process them) but stop the
> >> > listener from the outside? Say, from an outside thread?
> >> > 
> >> > Thanks,
> >> > mrh
> >> 
> >> 
> >> 
> > 
> > 
> 

Re: Stop Thread / Stop Listening

Posted by mrh <mr...@gmail.com>.
That is exactly what I needed to know.  I misunderstood what the
donelatch.await() line of code did in the activemq-example project.  I could
not figure out why the entire program kept exiting when that line of code
wasn't in there.  Now I see that if no other processing is taking place, the
program exits without stopping the consumer correctly, which throws an
exception.  Now I'm starting to understand.  Thanks again for all of the
assistance!

mrh



tabish121 wrote:
> 
> When you use a MessageListener that listener is notified in a thread
> context other than the main thread.  So in you client app you can set a
> MessageListener and then it will receive messages until you kill the
> app, so you can just have the main waiting on a cin.get() and it will
> process message until you hit a key.
> 
> Regards.
> Tim.
> 
> On Fri, 2007-10-26 at 07:36 -0700, mrh wrote:
>> Thank you for the reply, Tim.
>> 
>> Yes, this is my primary question:
>> 
>> 
>> tabish121 wrote:
>> > 
>> > make that it
>> > doesn't wait forever for messages and it stops as soon as it gets all
>> > the messages we told it to.  
>> > 
>> 
>> I would like for it to listen continuously for messages: no countdown, no
>> wait... just listen in the background while allowing the user to do other
>> things, until the user wants to exit the entire application.  Is the only
>> way to do this with threads?
>> 
>> Thanks again,
>> mrh
>> 
>> 
>> 
>> On Fri, 2007-10-26 at 06:49 -0700, mrh wrote:
>> > In the activemq-cpp example code, there is a countdown "latch".  If I
>> > understand it correctly, you can pass in a number of milliseconds that
>> it
>> > is
>> > to wait for a message or a number that it will count down to as
>> messages
>> > are
>> > received.  With this structure, is it possible to asychronously receive
>> > messages (using the onMessage function to process them) but stop the
>> > listener from the outside? Say, from an outside thread?
>> > 
>> > Thanks,
>> > mrh
>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Stop-Thread---Stop-Listening-tf4693571s2354.html#a13430289
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Stop Thread / Stop Listening

Posted by Timothy Bish <ti...@sensis.com>.
When you use a MessageListener that listener is notified in a thread
context other than the main thread.  So in you client app you can set a
MessageListener and then it will receive messages until you kill the
app, so you can just have the main waiting on a cin.get() and it will
process message until you hit a key.

Regards.
Tim.

On Fri, 2007-10-26 at 07:36 -0700, mrh wrote:
> Thank you for the reply, Tim.
> 
> Yes, this is my primary question:
> 
> 
> tabish121 wrote:
> > 
> > make that it
> > doesn't wait forever for messages and it stops as soon as it gets all
> > the messages we told it to.  
> > 
> 
> I would like for it to listen continuously for messages: no countdown, no
> wait... just listen in the background while allowing the user to do other
> things, until the user wants to exit the entire application.  Is the only
> way to do this with threads?
> 
> Thanks again,
> mrh
> 
> 
> 
> On Fri, 2007-10-26 at 06:49 -0700, mrh wrote:
> > In the activemq-cpp example code, there is a countdown "latch".  If I
> > understand it correctly, you can pass in a number of milliseconds that it
> > is
> > to wait for a message or a number that it will count down to as messages
> > are
> > received.  With this structure, is it possible to asychronously receive
> > messages (using the onMessage function to process them) but stop the
> > listener from the outside? Say, from an outside thread?
> > 
> > Thanks,
> > mrh
> 
> 
> 

Re: Stop Thread / Stop Listening

Posted by mrh <mr...@gmail.com>.
Thank you for the reply, Tim.

Yes, this is my primary question:


tabish121 wrote:
> 
> make that it
> doesn't wait forever for messages and it stops as soon as it gets all
> the messages we told it to.  
> 

I would like for it to listen continuously for messages: no countdown, no
wait... just listen in the background while allowing the user to do other
things, until the user wants to exit the entire application.  Is the only
way to do this with threads?

Thanks again,
mrh



On Fri, 2007-10-26 at 06:49 -0700, mrh wrote:
> In the activemq-cpp example code, there is a countdown "latch".  If I
> understand it correctly, you can pass in a number of milliseconds that it
> is
> to wait for a message or a number that it will count down to as messages
> are
> received.  With this structure, is it possible to asychronously receive
> messages (using the onMessage function to process them) but stop the
> listener from the outside? Say, from an outside thread?
> 
> Thanks,
> mrh



-- 
View this message in context: http://www.nabble.com/Stop-Thread---Stop-Listening-tf4693571s2354.html#a13428588
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Stop Thread / Stop Listening

Posted by Timothy Bish <ti...@sensis.com>.
I'm not really sure what you are asking.  The CountDownLatch is used
specifically in our example to control the consumer and make that it
doesn't wait forever for messages and it stops as soon as it gets all
the messages we told it to.  This happens when the run method in the
consumer class finishes waiting on the latch.

If you are trying to stop a consumer receiving then you can just close
the consumer and recreate it, or call stop on the connection and then
start it again when you are ready.

Regards
Tim.


On Fri, 2007-10-26 at 06:49 -0700, mrh wrote:
> In the activemq-cpp example code, there is a countdown "latch".  If I
> understand it correctly, you can pass in a number of milliseconds that it is
> to wait for a message or a number that it will count down to as messages are
> received.  With this structure, is it possible to asychronously receive
> messages (using the onMessage function to process them) but stop the
> listener from the outside? Say, from an outside thread?
> 
> Thanks,
> mrh

Re: Stop Thread / Stop Listening

Posted by mrh <mr...@gmail.com>.
In the activemq-cpp example code, there is a countdown "latch".  If I
understand it correctly, you can pass in a number of milliseconds that it is
to wait for a message or a number that it will count down to as messages are
received.  With this structure, is it possible to asychronously receive
messages (using the onMessage function to process them) but stop the
listener from the outside? Say, from an outside thread?

Thanks,
mrh
-- 
View this message in context: http://www.nabble.com/Stop-Thread---Stop-Listening-tf4693571s2354.html#a13427708
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Stop Thread / Stop Listening

Posted by Nathan Mittler <na...@gmail.com>.
Not sure I understand your question exactly.  Are you looking to simply use
the threading classes in your own program or are you asking  about how to
shutdown a connection?

If the latter, you simply have to do a connection.close().

If you're looking to use ActiveMQ-CPP's threading classes in your program,
this should give you an idea of what you can do
http://activemq.apache.org/cms/api_docs/activemqcpp-2.0.1/classactivemq_1_1concurrent_1_1_thread.html.


We basically took a Java approach.  Stopping threads is an
application-specific operation, so you have to write that logic yourself (
e.g. setting a flag/event).

BTW, we also have a synchronized macro that simulates the Java synchronized
block (
http://activemq.apache.org/cms/api_docs/activemqcpp-2.0.1/_concurrent_8h.html).
This lets you do things like this:

synchronized(this){
    ...
}

In addition, all Synchronizable objects (
http://activemq.apache.org/cms/api_docs/activemqcpp-2.0.1/classactivemq_1_1concurrent_1_1_synchronizable.html)
allow you to do Java-style wait and notify as well.  Of course the object
has to first be locked before you can wait/notify on it.  If you have a
class that implements Synchronizable, you can just delegate all of the
Synchronizable methods to a member variable of type Mutex (
http://activemq.apache.org/cms/api_docs/activemqcpp-2.0.1/classactivemq_1_1concurrent_1_1_mutex.html).
That makes it really easy to make any class Synchronizable.

Hope that helps :)

Regards,
Nate

On 10/25/07, mrh <mr...@gmail.com> wrote:
>
>
> With the threading capabilities provided by ActiveMQ-CPP, is it possible
> to
> stop a thread?  For example, if one thread starts listening for messages,
> is
> it possible for a second thread, say a command prompt to stop and shutdown
> the listener thread when the user types "quit" or "exit"?
>
> Thanks,
> mrh
> --
> View this message in context:
> http://www.nabble.com/Stop-Thread---Stop-Listening-tf4693571s2354.html#a13415697
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>
>