HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / JMS / JMS Point-To-Point Message Example

JMS Point-To-Point Message Example

In JMS tutorial, you read about JMS messaging domains Point to Point Domain and Publish Subscribe Domain. In this example, we will go through one such example of point to point messaging domain. In point to point message the sender delivers the message to the queue and a single receiver takes out the message from the queue. The receiver does not need to be listening to the queue at the time the message is sent.

Point to point JMS Messaging
Point to point JMS Messaging

Point-To-Point Messaging Publisher Application Flow

A typical point-to-point messaging example for the sender application is given below. The following steps for the application are –

  1. Firstly, we will obtain the Initial Context object for the JMS server.
  2. After that use the initial context object for lookup a queue object.
  3. Again we will use the initial context object for lookup the queue connection factory.
  4. Then use the queue connection factory to create the queue connection as it represents the physical connection of the JMS server.
  5. After creating the queue connection factory we will create the queue session where the first parameter will decide whether the session is transacted or not. But we will use a non-transacted session.
  6. After this create a queue sender for queue and then create a message.
  7. Then send the message such as “Hello World” to the queue object.
  8. After that close the queue connection as you close the queue connection it will automatically close the session and queue sender.

Let’s look at example below:

package pointToPoint;                                                                      
import javax.naming.InitialContext;
                                                                           
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.QueueSender;
import javax.jms.DeliveryMode;
import javax.jms.QueueSession;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
                                                                           
public class Sender
{
    public static void main(String[] args) throws Exception
    {
       // get the initial context
       InitialContext context = new InitialContext();
                                                              
       // lookup the queue object
       Queue queue = (Queue) context.lookup("queue/queue0");
                                                                          
       // lookup the queue connection factory
       QueueConnectionFactory conFactory = (QueueConnectionFactory) context.lookup ("queue/connectionFactory");
                                                                          
       // create a queue connection
       QueueConnection queConn = conFactory.createQueueConnection();
                                                                          
       // create a queue session
       QueueSession queSession = queConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
                                                                          
       // create a queue sender
       QueueSender queSender = queSession.createSender(queue);
       queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
                                                                          
       // create a simple message to say "Hello World"
       TextMessage message = queSession.createTextMessage("Hello World");
                                                    
       // send the message
       queSender.send(message);
                                                                         
       // print what we did
       System.out.println("Message Sent: " + message.getText());
                                                                          
       // close the queue connection
       queConn.close();
    }
}

Point-To-Point Messaging Subscriber Application Flow

Most of the steps for the receiver side are same as for the sender application – except it will listen the message rather than sending the JMS message.

package pointToPoint;
import javax.naming.InitialContext;                                                                        
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.QueueSession;
import javax.jms.QueueReceiver;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
                                                                           
public class Receiver
{
    public static void main(String[] args) throws Exception
    {
       // get the initial context
       InitialContext context = new InitialContext();
                                                                          
      // lookup the queue object
       Queue queue = (Queue) context.lookup("queue/queue0");
                                                                          
       // lookup the queue connection factory
       QueueConnectionFactory conFactory = (QueueConnectionFactory) context.lookup ("queue/connectionFactory");
                                                                          
       // create a queue connection
       QueueConnection queConn = conFactory.createQueueConnection();
                                                                          
       // create a queue session
       QueueSession queSession = queConn.createQueueSession(false,   
       Session.AUTO_ACKNOWLEDGE);

       // create a queue receiver
       QueueReceiver queReceiver = queSession.createReceiver(queue);
                                                                          
       // start the connection
       queConn.start();
                                                                          
       // receive a message
       TextMessage message = (TextMessage) queueReceiver.receive();
                                                                          
       // print the message
       System.out.println("Message Received: " + message.getText());
                                                                          
       // close the queue connection
       queConn.close();
    }
}

In the above code the receiver will receive the message from the sender and print it i.e. Hello World. But if the connection is not started then the method receiver will be blocked till the time any other thread starts the connection.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. gkm

    March 30, 2020

    how to execute these programs?

  2. VICKY KUMAR JAISWAL

    November 13, 2016

    javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

    this types of error is comimng what to do.

  3. Vivek Hingorani

    May 1, 2016

    What infrastructure are u using to create queues or topics?

Comments are closed on this article!

Search Tutorials

JMS Tutorial

  • JMS – Introduction
  • JMS – Point-To-Point Message
  • JMS – Publish/Subscribe Message

HornetQ Tutorial

  • HornetQ – JMS Example
  • HornetQ – Maven Example
  • Hornetq – Spring Integration

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces