Make delicious recipes!

JMS (Java Messaging Service)

JMS (Java Messaging Service) provides a messaging server to which JMS clients can send and receive messages.
Having a messaging server reduces the coupling between different clients and allows the system to work even if some nodes go down.

JMS has two modes:
  1. Queue (Point-to-Point) model: Two JMS clients send and receive messages through the JMS server.

  2. Publish-Subscribe model: Several JMS clients (subscribers) can receive messages from a single JMS client (Publisher).

Queue Model Topic Model

// Command line to start a Queue in the JMS server
j2eeadmin -addJmsFactory <jndi_name> queue
j2eeadmin -addJmsDestination <queue_name> queue

// Java Code to create a 'Queue' message sender
Context ctx = new InitialContext();

QueueConnectionFactory fctry = 
  (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");   

Queue myQueue = 
  (Queue) ctx.lookup("MyQueue");

// Create connection and session from factory
QueueConnection conn = fctry.createQueueConnection();
QueueSession session = conn.createQueueSession(true, 0);

QueueSender queueSender = session.createSender(myQueue);
TextMessage message = session.createTextMessage();


// Java Code to create a 'Queue' message receiver
QueueReceiver queueReceiver = session.createReceiver(myQueue);
Message m = queueReceiver.receive();
if (m instanceof TextMessage) {
    TextMessage message = (TextMessage) m;
    System.out.println("Reading message: " + message.getText());

// Command line to start a Topic in the JMS server
j2eeadmin -addJmsFactory <jndi_name> topic
j2eeadmin -addJmsDestination <topic_name> topic

// Java Code to create a 'Topic' message sender
Context ctx = new InitialContext();

TopicConnectionFactory fctry = 
  (TopicConnectionFactory) ctx.lookup("TopicConnectionFactory");

Topic myTopic = 
  (Topic) ctx.lookup("MyTopic");

// Create connection and session from factory
TopicConnection conn = fctry.createTopicConnection();
TopicSession session = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

TopicPublisher topicPublisher = session.createPublisher(myTopic);
TextMessage message = session.createTextMessage();


// Java Code to create a 'Topic' message receiver
TopicSubscriber topicSubscriber = session.createSubscriber(myTopic);
Message m = topicSubscriber.receive(1000); // time out after a second

The receive() method used above comes in 3 flavours:
  1. receive(): Blocking method.
  2. receive(long ms): Blocking method with a timeout.
  3. receiveNoWait(): Non-blocking method.

Apart from that, JMS also provides clients to register as listeners on a Queue or a Topic.
This way, clients do not have to keep polling the JMS server to check the presence of a new message.

TopicListener topicListener = new TopicListener();

Like us on Facebook to remain in touch
with the latest in technology and tutorials!

Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Email: (Your email is not shared with anybody)

Facebook comments:

Site Owner: Sachin Goyal