Spring Boot JMS Queues

Rishabh Khot
3 min readAug 27, 2020

--

JMS (Java Message Service) is a Java Message Oriented Middleware used to send messages between clients and works by sending messages to a message queue which are then taken when possible to execute a transaction. This post will focus on implementing JMS with Spring Boot, which is easier to understand.

JMS and message queues have certain advantages over using RESTful services such as:

  • Redundancy. A message must confirm that it has completed its transaction and that it can now be removed from the queue, but if the transaction fails it can be reprocessed. The messages can also be stored in a database allowing them to continue later on even if the server stops.
  • Asynchronous messaging. As the processing time of the message cannot be guaranteed, the client that sent the message can carry on asynchronously to the completion of the transaction. Due to this, the queue should be used to write data (POST if you're thinking in a RESTful mindset).
  • Loose coupling. The services do not interact directly and only know where the message queue is, where one service sends messages and the other receives them.

CODE SNIPPET

I’m going to explain the code from this GitHub link
Thank You @maxkahan for letting me use the code.

STEPS

  1. Import java dependencies by importing the packages.
  2. Create variables for the connection to MQ
    HOST = “localhost”; // Host name or IP address
    PORT = 1414; // Listener port for your queue manager
    CHANNEL = “DEV.APP.SVRCONN”; // Channel name
    QMGR = “QM1”; // Queue manager name
    APP_USER = “app”; // User name that application uses to connect to MQ APP_PASSWORD = “password”; // Password that the application uses to connect to MQ
    QUEUE_NAME = “DEV.QUEUE.1”; // Queue that the application uses to put and get messages to and from
  3. // Declare JMS 2.0 objects
    JMSContext // A JMSContext is the main interface in the simplified JMS API introduced for JMS 2.0. This combines in a single object the functionality of two separate objects from the JMS 1.1 API: a Connection and a Session.
    Destination // The destination will be a queue, but could also be a topic
    JMSConsumer;
    JmsConnectionFactory //This annotation may be used to specify the JNDI lookup name of a javax.jms.ConnectionFactory to be used when injecting a javax.jms.JMSContext object.
  4. Create JMS Properties as defined above by setting up in the property file
  5. Connect to the queue and start sending the messages
    context = connectionFactory.createContext(); // This is connection + session.
    The connection is started by default destination = context.createQueue(“queue:///” + QUEUE_NAME); // Set the producer and consumer destination to be the same… not true in general
    consumer = context.createConsumer(destination); // associate consumer with the queue we put messages onto
    MessageListener ml = new DemoMessageListener(); // Creates a listener object
    consumer.setMessageListener(ml); // Associates listener object with the consumer // The message listener will now listen for messages in a separate thread (see MyMessageListener.java file)
  6. Create a separate Java file to check if the message have been received or not
  7. We create a connection to the queue and create a receiver object to connect to the queue.
  8. Finally, retrieve the message and print it

This is a simple setup for the JMS message for sending and receiving messages. We can add more functions and even create a UI for easy understanding for the end-users.

--

--