JMS - Programming Model



Description

The JMS API contains following building blocks −

  • Administered Objects
  • Connections
  • Sessions
  • Message Producers
  • Message Consumers
  • Messages
JMS Programming Model

Administered Objects

Administered objects are pre-built objects, which can be used by the JMS clients. Administrator uses the Java Naming and Directory Interface (JNDI: It is a Java API, used to find the data with particular name) API namespace to build administered objects. There are two types of administered objects −

  • Connection Factory − This object provides the connection between JMS client and Service provider.

  • Destination − This object defines the JMS clients, to target the messages and receive messages from the destination.

Connections

Connection uses some JMS providers such as WebSphere, Active MQ, Open MQ etc to create connection with client and defines these provider resources virtually outide the Java Virtual Machine (JVM). It creates connection between client and provider.

It will use the Connection interface along with ConnectionFactory object to create a connection as shown below −

Connection connection = connectionFactory.createConnection();

The connection can be closed by using below line −

connection.close();

Sessions

It is a light weight JMS object, used for producing and consuming messages. You can create message producers, message consumers, and messages by using sessions.

You can create a session by using Connection object and Session interface as shown below −

//The AUTO_ACKNOWLEDGE field automatically gives the client's receipt messages, when they have been received successfully
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

Message Producers

The message producer is generated by a session, which sends messages to the destination by implementing MessageProducer interface. You can establish a MessageProducer for the destination or queue object as shown below −

MessageProducer producer = session.createProducer(destination);
MessageProducer producer = session.createProducer(queue);

Use the send() method to send the messages after creating message producer.

producer.send(message);

Message Consumer

The message consumer is generated by a session, which receives messages from the destination by implementing the MessageConsumer interface. You can establish a MessageConsumer for the destination or queue object as shown below −

MessageConsumer producer = session.createConsumer(destination);
MessageConsumer producer = session.createConsumer(queue);

Messages

JMS Messages includes the data, which will be exchanged between JMS clients to the design of a JMS application. Messages are highly flexible, that create messages with the matching formats. For more information, refer this chapter.

Advertisements