JMS API 2.0 Producer and Consumer

Filed Under: JMS

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

jms2.0_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 JMSContext object

    
    JMSContext jmsContext = cf.createContext();
    

    Use JMSContext object to create JMSProducer and send message to destination.

    
    jmsContext.createProducer().send(queue, " JD Sample Text Message");
    

    Closed created JMSContext object to release all resources.

    
    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

    jms2.0_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 Consumer 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 JMSContext object

      
      JMSContext jmsContext = cf.createContext();
      

      Use JMSContext object to create JMSConsumer and send message to destination.

      
      String message = jmsContext.createConsumer(queue).receiveBody(String.class);
      

      Closed created JMSContext object to release all resources.

      
      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.

Comments

  1. David Obber says:

    Nice article. I found it looking for a way to get a Destination (Queue or Topic) in a small program I wrote to use Oracle advanced queues. In this test I didn’t find how to replace session.getQueue() using JMSContext. Is there a way?

    public static void enqueueMessage(String messageContent) throws JMSException, SQLException {
    QueueConnectionFactory connectionFactory = AQjmsFactory.getQueueConnectionFactory(getOracleDataSource());
    try (QueueConnection connection = connectionFactory.createQueueConnection();
    QueueSession session = connection.createQueueSession(true, Session.CLIENT_ACKNOWLEDGE)) {
    connection.start();
    Queue queue = ((AQjmsSession) session).getQueue(queueOwner, queueName);

    TextMessage message = session.createTextMessage(messageContent);
    MessageProducer producer = session.createProducer(queue);;
    producer.send(message);
    session.commit();
    };
    }

    public static void enqueueMessageJMS2(String messageContent) throws JMSException, SQLException {
    QueueConnectionFactory connectionFactory = AQjmsFactory.getQueueConnectionFactory(getOracleDataSource());

    try (JMSContext context = connectionFactory.createContext(JMSContext.CLIENT_ACKNOWLEDGE);) {
    Queue queue = null; //?????
    TextMessage message = context.createTextMessage(messageContent);
    context.createProducer().send(queue, message);
    context.commit();
    };
    }

  2. Suresh says:

    Nice tutorial. Please provide one example on JMS 2.0 with NetBeans and GlassFish server. I need this requirement in my project. Thax for nice tutorial.

  3. Hafeez says:

    Nice Posts. It would be great if you could add one complete working example.

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