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-400x450

    • 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

Use InitialContext object to lookup ConnectionFactory object from JNDI Repository

Use InitialContext object to lookup Destination object from JNDI Repository

Use ConnectionFactory object and create Connection object

Use Connection object and create Session object

Use Session and Destination objects and create Producer object

Use Session and create sample TextMessage

Use Producer object to send TextMessage to the Destination

Closed created Connection object to release all resources.

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

Use InitialContext object to lookup ConnectionFactory object from JNDI Repository

Use InitialContext object to lookup Destination object from JNDI Repository

Use ConnectionFactory object and create Connection object

Use Connection object and create Session object

Use Session and Destination objects and create Producer object

Start the connection to consume messages from Destination.

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

Closed created Connection object to release all resources.

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.

By admin

Leave a Reply

%d bloggers like this: