commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Strachan <>
Subject [messenger] Re: some questions
Date Thu, 24 Apr 2003 07:00:47 GMT

On Wednesday, April 23, 2003, at 09:44  pm, Louis Gates wrote:

> Hi James.  I hope it's ok that I email you directly.

No worries. I've cc'd my response to the Commons User list in case this 
answer helps anyone else...

> I have a question or two about the messenger.  First I want to say, 
> I've
> been using it and it's great!  Thanks for all of your hard work.  It's 
> saved
> us alot of time.

Thanks, thats nice to hear.

> Basically my questions are about multi threading a messenger object.  I
> would like to be able to share a messenger object throughout my 
> application
> and have it send messages asynchronously.  I'm assuming that in order 
> to do
> this multiple jms sessions are necessary.  If you could at least shed 
> some
> light on how I would achieve this it would be greatly appreciated.

The JMS API has a synchronous API for sending messages, a synchronous 
API for receiving messages (pull) and an asynchronous API for receiving 
messages (push). However it doesn't have an asynchronous API for 
sending messages. So if you want to asynchronously send messages, you 
need to use some kind of thread pool. There's a little experimental 
thread pool in the commons sandbox right now so you can do things 

final Messenger messenger = ...;
final Destination destination = ...;

ThreadPool threadPool = ...;
	new Runnable() {
		public void run() {
			try {
				Message message = messenger.createTextMessage( "some text" );
				messenger.send( destination, message );
			catch (JMSException e) {
				// ...

For receiving messages synchronously, you can use the receive() methods 
(which pull messages), or to asynchronously receive messages you can 
use the addListener() method.

In all of the above use cases, you can happily share a Messenger object 
across threads. Each Messenger object is designed to be a facade on top 
of a JMS Connection and a Session per thread. So the Messenger object 
takes care of the session objects for you.


View raw message