To receive messages for a particular subject, you simply call one of the two
methods that the
class provides. One version of the method delivers messages synchronously, while the other delivers messages asynchronously.
Notice that as I've described the
listen methods, I haven't mentioned topics or queues. This is because, as discussed above, these details of message delivery don't need to be exposed at this level.
Asynchronous Message Delivery
To receive messages asynchronously, call the version of the
listen method that requires both a subject name and a callback reference. This call returns right away, and your code is free to do whatever processing it needs to do.
When a message arrives for the specified subject, a method is called on the provided callback reference. The callback reference must be an object that implements the
javax.jms.MessageListener interface. The only method defined by this interface is
onMessage, which provides the message itself through a parameter defined as
The callback reference is continuously called for each message that arrives until your application instructs the JMS Facade to stop. To stop listening to messages on a subject, simply call the
stop method and provide the applicable subject name as the parameter.
You don't have to create multiple instances of the
JMSManager class to asynchronously listen to more than one subject at a time. One instance will do the job. However, your code will need to call
listen once for each subject.
Synchronous Message Delivery
If the code you're writing has no other purpose but to receive messages and perform processing only when they arrive, you may chose to use the synchronous version of the
listen method. You call this version of
listen by providing only the subject name to which the method will listen. The call will not return until a message arrives for the subject. When a message does arrive, it is returned from the call in the form of a
With synchronous message delivery, your code must call
listen again after a message arrives in order to receive additional messages for the specified subject. To stop receiving messages for a subject, you simply do not call
listen after a message arrives. Therefore, with the synchronous version of
listen, you never need to call the
stop method of the JMS Facade.
Sending MessagesA simple text message in the form of a
To provide flexibility, the JMSManager class defines three
send methods. The difference between them is the type of message payload each sends:
A serializable Java object
Besides the message payload type, each
send method works the same way, requiring a subject name to send the message to.
As with receiving messages, topics or queues aren't mentioned. Only a subject is specified. Again, the details of message delivery are hidden at this level. However, it is normally important for a message sender to specify the message-delivery paradigm. The sender may want the message broadcast to all listeners (publish/subscribe) or only to one listener, one time (point-to-point).
Still, you can't specify this option at the time a message is sent. The message-delivery model is a characteristic of the subject itself. To control it, your code can specify the model by creating the subject before sending or receiving messages for the applicable subject (see the previous section, Subject Creation).
You shouldn't need more than one instance of
JMSManager, even if your code needs to send messages to more than one subject. You may call the
send method as many times as you need, specifying the same or different subject names as required each time.
JMS Made Easy
The Facade class provides a good starting point for making JMS easier to work with in your code. To see how truly easy it makes working with JMS, download my JMS Fa