Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Simplify JMS with the Facade Design Pattern : Page 4

JMS provides a multitude of classes, but you need to extend or instantiate them before they're of any use. Although the complexity of this process can be daunting, applying the Facade design pattern makes JMS much simpler to use.


advertisement
Receiving Messages
To receive messages for a particular subject, you simply call one of the two listen methods that the JMSManager 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 javax.jms.Message.

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 javax.jms.Message object.

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 Messages
To provide flexibility, the JMSManager class defines three send methods. The difference between them is the type of message payload each sends:

  • A simple text message in the form of a String
  • A serializable Java object
  • A javax.jms.Message 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



    Eric Bruno is a development analyst with Reuters in New York. He has worked extensively with Java, C++, and COM through the years, developing real-time trading systems, news systems, and financial applications. Reach him by e-mail at eric_j_bruno@yahoo.com.
    Comment and Contribute

     

     

     

     

     


    (Maximum characters: 1200). You have 1200 characters left.

     

     

    Sitemap