Browse DevX
Sign up for e-mail newsletters from DevX


Simplify JMS with the Facade Design Pattern : Page 3

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

The JMS Facade
The JMS Facade attempts to merge the publish/subscribe and point-to-point message models into one interface, hiding the model itself from the programmer. In my experience, application code written around the publish/subscribe model is very similar to that written around the point-to-point model. The difference lies in the details of how each delivers the messages themselves. This difference can be considered an implementation detail and, hence, should be hidden from developers in most cases. (For an overview of the JMS message models, read the sidebar, "The Java Message Service (JMS)".)

To use the JMS Facade, start by importing the com.JMSFacade package. Next, create an instance of the com.JMSFacade.JMSManager class, as shown in Listing 4. JMSManager does all the work.

Listing 1. The com.JMSFacade Package
This is an example of importing and creating the JMS Facade class, JMSManager:

import com.JMSFacade; import javax.jms.*; public class myClass { JMSManager jmsMgr = new JMSManager(); ... }

The JMS Facade attempts to abstract the concepts of publish/subscribe (Topic) and point-to-point (Queue) using the term "subject." Very simply, code written around the JMS Facade listens to, or sends messages to, a subject that is nothing more than a text name. It is not required to denote whether the destination is a topic or queue at this point—or ever.

Subject Creation
You don't need to create subjects explicitly, even though the JMS Facade does allow it. The Facade creates subjects automatically when you call the listen or send methods. The only reason to explicitly create a subject is to control the message-delivery model, publish/subscribe or point-to-point. The default model is publish/subscribe.

To specify the message-delivery model, your code must call the createSubject method on the JMSManager class. This method requires two parameters:

  • A unique subject name in the form of a String
  • A subject type (the message-delivery model), which is either com.JMSFacade.JMSManager.BROADCAST_SUBJECT or com.JMSFacade.JMSManager.SEND_ONCE_SUBJECT
  • A subject type specified as BROADCAST_TYPE is equivalent to a JMS Topic, which is the publish/subscribe model. A subject type of SEND_ONCE_SUBJECT is equivalent to a JMS Queue, which is the point-to-point model.

    Comment and Contribute






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



    Thanks for your registration, follow us on our social networks to keep up-to-date