Before starting developing JMS programs, first we will discuss about the following two concepts in this posts:
- Steps to develop a JMS Producer
- Steps to develop a JMS Consumer
Post Brief TOC
- Introduction
- JMS API 1.1 Producer
- JMS API 1.1 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.1.1 Example” section for simple JMS 1.1 working example.
JMS API 1.1 Producer
Steps to develop JMS V.1.1 Producer:
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 4 |
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 Connection object
1 2 3 |
Connection connection = cf.createConnection (); |
Use Connection object and create Session object
1 2 3 |
Session session = connection.createSession (false, Session.AUTO_ACKNOWLEDGE); |
Use Session and Destination objects and create Producer object
1 2 3 |
MessageProducer producer = session.createProducer(queue); |
Use Session and create sample TextMessage
1 2 3 4 |
TextMessage txtMsg = session.createTextMessage ("Sample P2P Queue TextMessage"); |
Use Producer object to send TextMessage to the Destination
1 2 3 |
producer.send (message); |
Closed created Connection object to release all resources.
1 2 3 |
connection.close(); |
JMS API 1.1 Consumer
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.
-
- 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 4 |
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 Connection object
1 2 3 |
Connection connection = cf.createConnection (); |
Use Connection object and create Session object
1 2 3 |
Session session = connection.createSession (false, Session.AUTO_ACKNOWLEDGE); |
Use Session and Destination objects and create Producer object
1 2 3 |
MessageConsumerconsumer = session.createConsumer(queue); |
Start the connection to consume messages from Destination.
1 2 3 |
connection.start(); |
NOTE:-
- Making a connection.start() method call is required at Consumer end, but not required at Producer end.
- When Producer make a call to send() method, it automatically start connection between Producer and Messaging Provider(Like JBoss).
- Consumers should make a call to connection.start() method because Consumers should inform to Messaging Provider saying that they are ready to receive or consume messages.
- Message delivery from JMS Provider to Consumer does not begin until consumer starts the connection by calling its start() method.
- Forgetting making a Connection.start() method call is one of the major JMS Programming errors.
Use Consumer object to receive Message (Ex:- TextMessage sent by the Producer.) from the Destination
1 2 3 |
TextMessage message = (TextMessage) consumer.receive(); |
Closed created Connection object to release all resources.
1 2 3 |
connection.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 1.1 Classic API uses only one set of API to develop both Producer and Consumer programs.
That’s it all about JMS API Overview. We will discuss JMS 2.0 Producer and Consumer development steps in my coming posts.
Please drop me a comment if you like my post or have any issues/suggestions.