JMS API Programming Model

JMS Programming Model

ConnectionFactory

  • an administered object that encapsulates a set of connection configuration parameters.
    • A client uses it to create a Connection to a JMS provider.
    • JMS clients find administered objects by looking them up in a JNDI namespace.
    • Connection factories come in two forms implementing either QueueConnectionFactory or TopicConnectionFactory interfaces.

  • At the beginning of a JMS client program, you usually perform a JNDI API lookup of the connection factory.
  • For example, the following code fragment obtains an InitialContext object and uses it to look up the QueueConnectionFactory and the TopicConnectionFactory by name. You can choose any of the below three options based on your requirement.
Context context = new InitialContext();
//1. ConnectionFactory
ConnectionFactory connectionFactory = 
  (ConnectionFactory) context.lookup("ConnectionFactory");
//2. QueueConnectionFactory
QueueConnectionFactory queueConnectionFactory = 
  (QueueConnectionFactory) context.lookup("QueueConnectionFactory");
//3. TopicConnectionFactory
TopicConnectionFactory topicConnectionFactory = 
  (TopicConnectionFactory) context.lookup("TopicConnectionFactory");

Connection

  • encapsulates a virtual connection with a JMS provider.
  • Like connection factories, connections come in two forms, implementing either the QueueConnection or the TopicConnection interface.

//1. Connection
Connection connection = connectionFactory.createConnection();
//2. QueueConnection
QueueConnection queueConnection = 
		queueConnectionFactory.createQueueConnection();
//3. TopicConnection
TopicConnection topicConnection = 
		topicConnectionFactory.createTopicConnection();
  • A JMS client usually creates a connection, one or more sessions, and a number of message producers.
  • When an application completes, you need to close any connections that you have created. Closing a connection also closes its sessions and their message producers and message consumers.
connection.close();
queueConnection.close();
topicConnection.close();

Destination

  • A Destination object is a JMS administered object that encapsulates a provider-specific address for storing and retrieving messages.
  • Destination, like session, comes in two forms, implementing either the Queue or the Topic interface.

  • We use Session object to create a destination
  • Client uses JNDI API to look up destination. The following line of code looks up a queue and a topic.
Destination destination = (Queue)context.lookup(“queue/MyQueue”);
Queue queue = (Queue)context.lookup(“queue/MyQueue”);
Topic topic = (Topic)context.lookup(“topic/MyTopic”);

Session

  • Session is a lightweight JMS object which acts as a single-threaded context for producing and consuming messages. Session creates,
    • Message producers,
    • Message consumers,
    • Messages,
    • Message Destination (Queue or Topic)
  • Sessions, like connections, come in two forms, implementing either the QueueSession or the TopicSession interface.

  • We use Connection object to create a session.
//1. Session
Session session = connection.createSession(false, 
				Session. DUPS_OK_ACKNOWLEDGE);
//2. QueueSession
QueueSession queueSession = queueConnection.createQueueSession(false, 
				Session.AUTO_ACKNOWLEDGE);
//3. TopicSession
TopicSession topicSession =  topicConnection.createTopicSession(true, 0);

Message

  • JMS application produces and consumes message.
  • The Message interface is the root interface of all JMS messages. There are five types of message: BytesMessage, TextMessage, ObjectMessage, StreamMessage and MapMessage.

  • We use Session object to create a message
TextMessage message = session.createTextMessage();
message.setText("Hello EJB3 MDB!!!");

BytesMessage byteMessage = session.createBytesMessage();
MapMessage mapMessage = session.createMapMessage();
StreamMessage streamMessage = session.createStreamMessage();
ObjectMessage objMsg = session.createObjectMessage();

MessageProducer

  • A message producer is an object created by a session and is used for sending messages to a destination.
  • MessageProducer comes in two forms implementing either the QueueSender or the TopicPublisher interface.

  • We use Session object to create a message producer.
  • The following snippet shows how to create a message producer, queue sender and a topic publisher.
MessageProducer producer  = session.createProducer(destination)
QueueSender queueSender = queueSession.createSender(queue);
TopicPublisher topicPublisher = topicSession.createPublisher(topic);

Once a message producer is created, you can use it to send messages.

queueSender.send(message);
topicPublisher.publish(message);

MessageConsumer

  • an object created by a Session that is used for receiving messages sent to a destination.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.