JMS API 1.1 Producer and Consumer

Filed Under: JMS

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.

jms1.1_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

    
    Context context = new InitialContext ();
    

    Use InitialContext object to lookup ConnectionFactory object from JNDI Repository

    
    ConnectionFactory cf = (ConnectionFactory) 
      context.lookup ("jms/SampleConnectionFactory");
    

    Use InitialContext object to lookup Destination object from JNDI Repository

    
    Destination queue = (Destination) context.lookup ("jms/SampleQueue");
    

    Use ConnectionFactory object and create Connection object

    
    Connection connection = cf.createConnection ();
    

    Use Connection object and create Session object

    
    Session session = connection.createSession (false, Session.AUTO_ACKNOWLEDGE);
    

    Use Session and Destination objects and create Producer object

    
    MessageProducer producer = session.createProducer(queue);
    

    Use Session and create sample TextMessage

    
    TextMessage txtMsg = 
       session.createTextMessage ("Sample P2P Queue TextMessage");
    

    Use Producer object to send TextMessage to the Destination

    
    producer.send (message);
    

    Closed created Connection object to release all resources.

    
    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:

jms1.1_consumer_steps

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

    
    Context context = new InitialContext ();
    

    Use InitialContext object to lookup ConnectionFactory object from JNDI Repository

    
    ConnectionFactory cf = ConnectionFactory) 
        context.lookup ("jms/SampleConnectionFactory");
    

    Use InitialContext object to lookup Destination object from JNDI Repository

    
    Destination queue = (Destination) context.lookup ("jms/SampleQueue");
    

    Use ConnectionFactory object and create Connection object

    
    Connection connection = cf.createConnection ();
    

    Use Connection object and create Session object

    
    Session session = connection.createSession (false, Session.AUTO_ACKNOWLEDGE);
    

    Use Session and Destination objects and create Producer object

    
    MessageConsumerconsumer = session.createConsumer(queue);
    

    Start the connection to consume messages from Destination.

    
    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

    
    TextMessage message = (TextMessage) consumer.receive();
    

    Closed created Connection object to release all resources.

    
    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.

Comments

  1. Younes says:

    Hi,
    Thanks for the Post.
    I can t create connection, the method cf.createConnection() is undefined, what should I do?

    Thanks in advance

  2. Narendra Khator says:

    Hi Rambabu,

    Nice article,

    Please correct the line in case of consumer because this line creates some doubts.

    Use Producer object to send TextMessage to the Destination

    TextMessage message = (TextMessage) consumer.receive();

    I hope it would be like this.

    Use Consumer object to get TextMessage from the Destination.

    Thakns

    1. Rambabu says:

      Yes good catch on my typo error. Updated that sentence.

  3. JOHNYBASHA SHAIK says:

    Good article. I have a doubt. Why producer doesn’t have connection.start()?

    1. Rambabu says:

      Yes, its really good question. I thought of delivering that info in my coming examples post. Anyhow, will update the post with that info soon. Please re-read this post after sometime.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages