Please go through my previous post at “JMS API 1.1 Producer and Consumer” to get some basics about How to develop JMS Producer and Consumer programs using JMS 1.1 API.
Before starting developing JMS programs, first we will discuss about the following two concepts in this posts:
- Steps to develop a JMS 2.0 Producer
- Steps to develop a JMS 2.0 Consumer
Post Brief TOC
- Introduction
- JMS API 2.0 Producer
- JMS API 2.0 Consumer
Introduction
In P2P or Pub/Sub messaging model, we can observe the following actors
- JMS Producer
- JMS Consumer
- JMS Message
- JMS Administered Objects
- JMS Provider
Based on our application or client needs, we can use any available JMS Provider to implement this Messaging Application. JMS Administrator configures all required Administered Objects in JMS Provider Admin console to use them in our application.
So, as a JMS Developer, we need to concentrate on 3 parts: Producer, Consumer and Message.
NOTE:-After reading this section, please refer “Simple JMS.V.2.0 Example” section for simple JMS2.0 working example.
Steps to develop JMS V.2.0 Producer:
JMS 2.0 has provided Simplified API to avoid the creation of more objects.
Responsibility of a Producer or Sender is to create and send messages to a destination. To develop a Producer application, we need to create a set of objects in the following order
-
- Install JMS Provider software
- Use Admin console and create Administered objects in JMS provider
ConnectionFactory JNDI Name: jms/SampleConnectionFactory Destination (Queue) JNDI Name: jms/SampleQueue
-
- Develop JMS Producer application
Create InitialContext
1 2 3 |
Context context = new InitialContext (); |
Use InitialContext object to lookup ConnectionFactory object from JNDI Repository
1 2 3 |
ConnectionFactory cf = (ConnectionFactory) context.lookup ("jms/SampleConnectionFactory"); |
Use InitialContext object to lookup Destination object from JNDI Repository
1 2 3 |
Destination queue = (Destination) context.lookup ("jms/SampleQueue"); |
Use ConnectionFactory object and create JMSContext object
1 2 3 |
JMSContext jmsContext = cf.createContext(); |
Use JMSContext object to create JMSProducer and send message to destination.
1 2 3 |
jmsContext.createProducer().send(queue, " JD Sample Text Message"); |
Closed created JMSContext object to release all resources.
1 2 3 |
jmsContext.close(); |
Steps to develop JMS V.2.0 Receiver:
Responsibility of a Consumer or Receiver is to consumer receive messages from a destination. To develop a Consumer application, we need to create a set of objects in the following order
If we observe this sequence of objects creation to consume a message, we are following almost similar kind of Producer steps.
1 2 3 |
Context context = new InitialContext (); |
Use InitialContext object to lookup ConnectionFactory object from JNDI Repository
1 2 3 |
ConnectionFactory cf = ConnectionFactory) context.lookup ("jms/SampleConnectionFactory"); |
Use InitialContext object to lookup Destination object from JNDI Repository
1 2 3 |
Destination queue = (Destination) context.lookup ("jms/SampleQueue"); |
Use ConnectionFactory object and create JMSContext object
1 2 3 |
JMSContext jmsContext = cf.createContext(); |
Use JMSContext object to create JMSConsumer and send message to destination.
1 2 3 |
String message = jmsContext.createConsumer(queue).receiveBody(String.class); |
Closed created JMSContext object to release all resources.
1 2 3 |
jmsContext.close(); |
NOTE:-
- As we are using Queue as Destination, these two Producer and Consumer components are part of P2P Messaging model.
- To develop same kind of application for Pub/Sub Messaging model, then JMS Administrator will create Topic in JMS Provider and Developer should use that Topic JNDI name in both Producer and Consumer programs. Because, JMS 2.0 Classic API uses only one set of API to develop both Producer and Consumer programs.
- In JMS 1.1 P2P Messaging model, to consume messages by a JMS Consumer, we should start connection manually. Otherwise, connection is created in CLOSED state. Where as in JMS 2.0 P2P Model, JMSContext will take care of this and no manual start is required.
That’s it all about JMS 2.0 Producer and Consumer API Overview. We will start developing JMS Applications using one IDE like Eclipse in coming posts.
Please drop me a comment if you like my post or have any issues/suggestions.