Wednesday, February 22, 2012

Java Message Service (JMS) using ActiveMQ

Java Message Service(JMS) is an API for standardizing Messaging services. we can send and receive the messages using JMS via Message-Oriented Middleware(MOM) like ActiveMQ.
ActiveMQ is provided by Apache software foundation, one of the popular JMS Provider.

Download the Apache ActiveMQ from Here
Set CLASSPATH = C:\Downloads\apache-activemq-5.5.1\lib
Set PATH = C:\Downloads\apache-activemq-5.5.1\bin

The home directory look like this:
 Start ActiveMQ Server:
  
Testing the Installation
If ActiveMQ is up and running without problems, the Window's console window or the Unix command shell will display information similar to the following log line:
INFO  ActiveMQ JMS Message Broker (ID:apple-s-Computer.local-51222-1140729837569-0:0) has started


ActiveMQ's default port is 61616. From another window run netstat and search for port 61616.
From a Windows console, type:
netstat -an|find "61616" 
 

Monitoring ActiveMQ

http://localhost:8161/admin/

Here we see a sample Producer/consumer application -


 Here is the code of the program sending (producing) the messages:
/**
 * Producer
 */
package com;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

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


/**
 * @author thanooj
 *
 */
public class Producer {

    // URL of the JMS server. DEFAULT_BROKER_URL will just mean
    // that JMS server is on localhost
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

    // Name of the queue we will be sending messages to
    private static String subject = "TESTQUEUE";

    public static void main(String[] args) throws JMSException {
        // Getting JMS connection from the server and starting it
        ConnectionFactory connectionFactory =
            new ActiveMQConnectionFactory(url);
        Connection connection = connectionFactory.createConnection();
        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 represents here our queue 'TESTQUEUE' on the
        // JMS server. You don't have to do anything special on the
        // server to create it, it will be created automatically.
        Destination destination = session.createQueue(subject);

        // 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 ActiveMQ World!'
        TextMessage message = session.createTextMessage("Hello ActiveMQ World!");

        // Here we are sending the message!
        producer.send(message);
        System.out.println("Sent message '" + message.getText() + "'");

        connection.close();
    }

}

if you run the Producer, you will get output like :

Sent message 'Hello ActiveMQ World!'

If you see something similar to the output above (especially the ‘Sent message’ part) then it means that the message was successfully sent and is now inside the TESTQUEUE queue. You can enter the Queues section in the ActiveMQ’s admin console http://localhost:8161/admin/queues.jsp and see that there is one message sitting in TESTQUEUE:






------------------------------------


/**
 * Consumer
 */
package com;

import javax.jms.*;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
 * @author thanooj
 *
 */
public class Consumer {

    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

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

    public static void main(String[] args) throws JMSException {
        // 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 'TESTQUEUE'
        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();
    }

}
In order to receive that message run now the Consumer program:

output:
Received message 'Hello ActiveMQ World!'
If you are getting above input (or something similar) everything went ok. The message was successfully received.

No comments: