- Introduction to Java Message Service
- JMS Architecture
- JMS Messages
- Messaging Domain Models
- The JMS Programming Model
- JMS Integration with EJBs
- Summary
The JMS Programming Model
Building a JMS application requires understanding of the following six building blocks:
- administered objects
- connection
- session
- message producer
- message consumer
- message
The JMS APIs are available under java.jms.*. We would have to import the following JMS packages to implement the example: TopicConnectionFactory, QueueConnectionFactory, Topic, Queue, QueReceiver, Session , and TextMessage.
Before a JMS producer can send messages, it must first create a session and then a connection. To create a connection, the JMS client must figure out the mechanism for creating a connection and find a destination for the message. This function is enabled using administered objects. Administered objects consist of ConnectionFactory and destination objects. The JNDI is used to locate and download these administered objects. A ConnectionFactory object encapsulates a set of vendor-specific messaging server configuration parameters defined by the administrator during deployment. JMS clients use the ConnectionFactory object to create a connection to the JMS provider. A code snippet showing the use of JNDI and the connection factory object follows:
......... Context cntx = new InitialContext(env); //in the case of Queue messaging model QueueConnectionFactory queConnFactory = (QueueConnectionFactory) cntx.lookup("java:comp/env/jms/QueueConnectionFactory"); ............ //in the case of Topic messaging model TopicConnectionFactory facConnFactory = (TopicConnectionFactory) cntx.lookup("java:comp/env/jms/TopicConnectionFactory"); ....................
A destination object encapsulates vendor-proprietary destination information that message producers and consumers must use to send messages. JNDI is also used to locate and download destination objects. It's also used to locate and download destination objects from java:comp/env/jms, which is a commonly used JMS root context for administered objects. A destination is called a queue in the P2P messaging model and a topic in the pub/sub model, as illustrated here:
............. Topic hotDealTopic = (Topic) cntx.lookup("java:comp/env/jms/HotDeal"); Queue supportQue = (Queue) cntx.lookup("java:comp/env/jms/TechSupport"); .............
Administered objects are created by an administrator and made available for clients to use during executionboth support concurrent access. Administered objects encapsulate the JMS provider-specific logic and hides it from the JMS client code, thus enhancing application portability. Administered objects are created and configured by an administrator using a vendor-provided deployment tool at deployment time. Connection objects encapsulate an open connection to the JMS provider. Connection objects use the user name and password authentication to open a connection to the JMS provider daemon for a queue and topic destination and must close the connection when done in the following way:
QueueConnection queConnection = queConnFactory.createQueueConnec-tion( username, password) TopicConnection topicConnection = topicConnFactory.createTopicConnec-tion( username, password)
The session object is a factory for producing and consuming messages and is single threaded and transactional as shown here:
QueueSession queSession = queConnection.createQueueSession(false, Ses-sion. AUTO_ACKNOWLEDGE)TopicSession topicSession = topicConnection.cre-ateTopicSession( false, AUTO_ACKNOWLEDGE)
The message producer (MessageProducer) object is created by a session object and is used by producer clients to send messages to a destination. It's a superinterface for both the TopicPublisher and QueueSender objects. With P2P, use the send() method of the QueueSender interface to send messages to a destination and use the publish() method of the TopicPublisher interface for the pub/sub model. For example, let's assume we want to connect to a queue destination called "HotNews" and a topic destination called "HotBargains" that was preconfigured by the administrator. First, look up the destinations and then create the sender and publisher objects as follows:
Queue hotNews = (Queue) jndi.lookup("java:comp/env/jms/HotNews") Topic hotBargains = (Topic) jndi.lookup("java:comp/emv/jms/HotBargains") QueueSender queSender = queSession.createSender(hotNews); queSender.send(msgHotDeal) TopicPublisher topicPublisher = topicSession.createPublisher(hotBar-gains)
The message consumer (MessageConsumer) object is also created by a session object and is used by consumer clients to receive messages from a destination. A MessageConsumer is a superinterface to both the QueueReceiver and TopicSubscriber interfaces. There are several variations for creating a message consumer client. A message consumer client can be created with a message selector, which enables developers to selectively filter messages the client will receive based on the selector SQL syntax. Developers also can select between synchronous and asynchronous communication modes when creating the consumer client. When selecting synchronous mode, the pull method is being selected and the receive() method is used to get messages. If the synchronous or push mode is preferred, the MessageListener interface is implemented and the JMS provider uses the onMessage() method to deliver messages. The start() method in QueueConnection and TopicConnection objects must be invoked before messages can arrive as illustrated here:
QueueReceiver queReceiver = queSession.createQueueReceiver("HotNews"); queConection.start(); Message msg = queReceiver.receive()
Or, asynchronous mode requires more work. First, developers must define a QueueListener class that implements the MessageListener interface and then registers the queue listener as follows:
QueueListener queListener = new QueueListener(); queReceiver.setMessageListener(queListener);
Message objects are created by the JMS producer and can be any one of five format types discussed earlier. Refer back to Table 12.2 for the different types of JMS message formats. Table 12.3 summarizes these objects, which are the building blocks for JMS applications (for both the P2P and pub/sub messaging models). Notice the symmetry of the object names and semantics.
Table 12-3 JMS API and Message Types
Object |
Point-To-Point Messages |
Publish/Subscribe Messages |
Comment |
Destination |
Queue, TemporaryQueue |
Topic, TemporaryTopic |
Administered object |
ConnectionFactory |
QueueConnection Factory |
TopicConnection Factory |
Administered object |
Connection |
QueueConnection |
TopicConnection |
|
Session |
QueueSession |
TopicSession |
|
MessageProducer |
QueueSender |
TopicPublisher |
|
MessageConsumer |
QueueReceiver |
TopicSubscriber |
|
Message |
TextMessage, MapMessage, ByteMessage, StreamMessage, ObjectMessage |
TextMessage, MapMessage, ByteMessage, StreamMessage, ObjectMessage |
Message types are common to both models |