JMS with ActiveMQ

JMS with ActiveMQ 

JMS short for Java Message Service provides a mechanism for integrating applications in a loosely coupled, flexible manner. JMS delivers data asynchronously across applications on a store and forward basis. Applications communicate through MOM(Message Oriented Middleware) which acts as an intermediary without communicating directly.

JMS Architecture

Main components of JMS are:

  • JMS Provider: A messaging system that implements the JMS interfaces and provides administrative and control features
  • Clients: Java applications that send or receive JMS messages. A message sender is called the Producer, and the recipient is called a Consumer
  • Messages: Objects that communicate information between JMS clients
  • Administered objects: Preconfigured JMS objects created by an administrator for the use of clients.

There are several JMS providers available like Apache ActiveMQ and OpenMQ. Here I have used Apache ActiveMQ.

Installing and starting Apache ActiveMQ on windows

  1. Download ActiveMQ windows binary distribution
  2. Extract the it to a desired location
  3. Using the command prompt change the directory to the bin folder inside ActiveMQ installation folder and run the following command to start ActiveMQ
  4. activemq

After starting ActiveMQ you can visit the admin console using http://localhost:8161/admin/ and do the administrative tasks

JMS Messaging Models

JMS has two messaging models, point to point messaging model and publisher subscriber messaging model.

Point to point messaging model

Producer sends the message to a specified queue within JMS provider and the only one of the consumers who listening to that queue receives that message.

Example 1 and example 2 are almost similar the only difference is example 1 creates queues within the program and the example 2 uses jndi.properties file for naming look ups and creating queues.

Example 1

package com.eviac.blog.jms;

import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.BasicConfigurator;

public class Producer {

 public Producer() throws JMSException, NamingException {

  // Obtain a JNDI connection
  InitialContext jndi = new InitialContext();

  // Look up a JMS connection factory
  ConnectionFactory conFactory = (ConnectionFactory) jndi
    .lookup('connectionFactory');
  Connection connection;

  // Getting JMS connection from the server and starting it
  connection = conFactory.createConnection();
  try {
   connection.start();

   // JMS messages are sent and received using a Session. We will
   // create here a non-transactional session object. If you want
   // to use transactions you should set the first parameter to 'true'
   Session session = connection.createSession(false,
     Session.AUTO_ACKNOWLEDGE);

   Destination destination = (Destination) jndi.lookup('MyQueue');

   // MessageProducer is used for sending messages (as opposed
   // to MessageConsumer which is used for receiving them)
   MessageProducer producer = session.createProducer(destination);

   // We will send a small text message saying 'Hello World!'
   TextMessage message = session.createTextMessage('Hello World!');

   // Here we are sending the message!
   producer.send(message);
   System.out.println('Sent message '' + message.getText() + ''');
  } finally {
   connection.close();
  }
 }

 public static void main(String[] args) throws JMSException {
  try {
   BasicConfigurator.configure();
   new Producer();
  } catch (NamingException e) {
   e.printStackTrace();
  }

 }
}
package com.eviac.blog.jms;

import javax.jms.*;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.BasicConfigurator;

public class Consumer {
 // URL of the JMS server
 private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

 // Name of the queue we will receive messages from
 private static String subject = 'MYQUEUE';

 public static void main(String[] args) throws JMSException {
  BasicConfigurator.configure();
  // Getting JMS connection from the server
  ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
  Connection connection = connectionFactory.createConnection();
  connection.start();

  // Creating session for seding messages
  Session session = connection.createSession(false,
    Session.AUTO_ACKNOWLEDGE);

  // Getting the queue
  Destination destination = session.createQueue(subject);

  // MessageConsumer is used for receiving (consuming) messages
  MessageConsumer consumer = session.createConsumer(destination);

  // Here we receive the message.
  // By default this call is blocking, which means it will wait
  // for a message to arrive on the queue.
  Message message = consumer.receive();

  // There are many types of Message and TextMessage
  // is just one of them. Producer sent us a TextMessage
  // so we must cast to it to get access to its .getText()
  // method.
  if (message instanceof TextMessage) {
   TextMessage textMessage = (TextMessage) message;
   System.out.println('Received message '' + textMessage.getText()
     + ''');
  }
  connection.close();
 }
}

jndi.properties

# START SNIPPET: jndi

java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory

# use the following property to configure the default connector
java.naming.provider.url = vm://localhost

# use the following property to specify the JNDI name the connection factory
# should appear as. 
#connectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactry

# register some queues in JNDI using the form
# queue.[jndiName] = [physicalName]
queue.MyQueue = example.MyQueue


# register some topics in JNDI using the form
# topic.[jndiName] = [physicalName]
topic.MyTopic = example.MyTopic

# END SNIPPET: jndi
package com.eviac.blog.jms;

import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.BasicConfigurator;

public class Producer {

 public Producer() throws JMSException, NamingException {

  // Obtain a JNDI connection
  InitialContext jndi = new InitialContext();

  // Look up a JMS connection factory
  ConnectionFactory conFactory = (ConnectionFactory) jndi
    .lookup('connectionFactory');
  Connection connection;

  // Getting JMS connection from the server and starting it
  connection = conFactory.createConnection();
  try {
   connection.start();

   // JMS messages are sent and received using a Session. We will
   // create here a non-transactional session object. If you want
   // to use transactions you should set the first parameter to 'true'
   Session session = connection.createSession(false,
     Session.AUTO_ACKNOWLEDGE);

   Destination destination = (Destination) jndi.lookup('MyQueue');

   // MessageProducer is used for sending messages (as opposed
   // to MessageConsumer which is used for receiving them)
   MessageProducer producer = session.createProducer(destination);

   // We will send a small text message saying 'Hello World!'
   TextMessage message = session.createTextMessage('Hello World!');

   // Here we are sending the message!
   producer.send(message);
   System.out.println('Sent message '' + message.getText() + ''');
  } finally {
   connection.close();
  }
 }

 public static void main(String[] args) throws JMSException {
  try {
   BasicConfigurator.configure();
   new Producer();
  } catch (NamingException e) {
   e.printStackTrace();
  }

 }
}
package com.eviac.blog.jms;

import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.BasicConfigurator;

public class Consumer {
 public Consumer() throws NamingException, JMSException {
  Connection connection;
  
  // Obtain a JNDI connection
  InitialContext jndi = new InitialContext();

  // Look up a JMS connection factory
  ConnectionFactory conFactory = (ConnectionFactory) jndi
    .lookup('connectionFactory');
  // Getting JMS connection from the server and starting it
  // ConnectionFactory connectionFactory = new
  // ActiveMQConnectionFactory(url);
  connection = conFactory.createConnection();

  // // Getting JMS connection from the server
  // ConnectionFactory connectionFactory = new
  // ActiveMQConnectionFactory(url);
  // Connection connection = connectionFactory.createConnection();
  try {
   connection.start();

   // Creating session for seding messages
   Session session = connection.createSession(false,
     Session.AUTO_ACKNOWLEDGE);

   // Getting the queue
   Destination destination = (Destination) jndi.lookup('MyQueue');

   // MessageConsumer is used for receiving (consuming) messages
   MessageConsumer consumer = session.createConsumer(destination);

   // Here we receive the message.
   // By default this call is blocking, which means it will wait
   // for a message to arrive on the queue.
   Message message = consumer.receive();

   // There are many types of Message and TextMessage
   // is just one of them. Producer sent us a TextMessage
   // so we must cast to it to get access to its .getText()
   // method.
   if (message instanceof TextMessage) {
    TextMessage textMessage = (TextMessage) message;
    System.out.println('Received message '' + textMessage.getText()
      + ''');
   }
  } finally {
   connection.close();
  }
 }

 public static void main(String[] args) throws JMSException {
  BasicConfigurator.configure();
  try {
   new Consumer();
  } catch (NamingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }
}

Publisher Subscriber Model

Publisher publishes the message to a specified topic within JMS provider and all the subscribers who subscribed for that topic receive the message. Note that only the active subscribers receive the message.

Point to Point Model Example

package com.eviac.blog.jms;

import javax.jms.*;
import javax.naming.*;

import org.apache.log4j.BasicConfigurator;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class DemoPublisherSubscriberModel implements javax.jms.MessageListener {
 private TopicSession pubSession;
 private TopicPublisher publisher;
 private TopicConnection connection;

 /* Establish JMS publisher and subscriber */
 public DemoPublisherSubscriberModel(String topicName, String username,
   String password) throws Exception {
  // Obtain a JNDI connection
  InitialContext jndi = new InitialContext();

  // Look up a JMS connection factory
  TopicConnectionFactory conFactory = (TopicConnectionFactory) jndi
    .lookup('topicConnectionFactry');

  // Create a JMS connection
  connection = conFactory.createTopicConnection(username, password);

  // Create JMS session objects for publisher and subscriber
  pubSession = connection.createTopicSession(false,
    Session.AUTO_ACKNOWLEDGE);
  TopicSession subSession = connection.createTopicSession(false,
    Session.AUTO_ACKNOWLEDGE);

  // Look up a JMS topic
  Topic chatTopic = (Topic) jndi.lookup(topicName);

  // Create a JMS publisher and subscriber
  publisher = pubSession.createPublisher(chatTopic);
  TopicSubscriber subscriber = subSession.createSubscriber(chatTopic);

  // Set a JMS message listener
  subscriber.setMessageListener(this);

  // Start the JMS connection; allows messages to be delivered
  connection.start();

  // Create and send message using topic publisher
  TextMessage message = pubSession.createTextMessage();
  message.setText(username + ': Howdy Friends!');
  publisher.publish(message);

 }

 /*
  * A client can register a message listener with a consumer. A message
  * listener is similar to an event listener. Whenever a message arrives at
  * the destination, the JMS provider delivers the message by calling the
  * listener's onMessage method, which acts on the contents of the message.
  */
 public void onMessage(Message message) {
  try {
   TextMessage textMessage = (TextMessage) message;
   String text = textMessage.getText();
   System.out.println(text);
  } catch (JMSException jmse) {
   jmse.printStackTrace();
  }
 }

 public static void main(String[] args) {
  BasicConfigurator.configure();
  try {
   if (args.length != 3)
    System.out
      .println('Please Provide the topic name,username,password!');

   DemoPublisherSubscriberModel demo = new DemoPublisherSubscriberModel(
     args[0], args[1], args[2]);

   BufferedReader commandLine = new java.io.BufferedReader(
     new InputStreamReader(System.in));

   // closes the connection and exit the system when 'exit' enters in
   // the command line
   while (true) {
    String s = commandLine.readLine();
    if (s.equalsIgnoreCase('exit')) {
     demo.connection.close();
     System.exit(0);

    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

Reference: JMS with ActiveMQ from our JCG partner Pavithra Siriwardena at the EVIAC blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


6 × = forty eight



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books