Searching...
Monday, 5 August 2013

JMS with ActiveMQ Example

11:25
In the previous example we tested JMS Message producer and Message listener in Jboss application server. In this example i am going to use Apache ActiveMQ for messaging.
Goals
  1. Setup ActiveMQ
  2. Create a JMS Message Producer
  3. Create a JMS Message Consumer
Setup ActiveMQ
In this example we are using windows so please follow the below steps to start ActiveMQ messaging system.
1. Download activemq from http://activemq.apache.org/download-archives.html
2. Extract activemq zip file and start ActiveMQ (Click on activemq.bat file)
3. Check activemq console using http://localhost:8161/admin  (Use default credentials admin/admin to login)
image

2. Create a Message Producer
Add below dependency in your pom.xml
<dependency>
            <groupId>javax.jms</groupId>
            <artifactId>javax.jms-api</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.7.0</version>
        </dependency>
Message Producer
package com.vinod.jms;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class MessageProducerExample {
    public static void main(String[] args) {
        try {
            // Create a ConnectionFactory
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin",
                    ActiveMQConnection.DEFAULT_BROKER_URL);
            // Create a Connection
            Connection connection = connectionFactory.createConnection();
            connection.start();
            // Create a Session
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            // Create the destination
            Destination destination = session.createQueue("testQ");
            // Create a MessageProducer from the Session to the Queue
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            // Create a messages
            TextMessage message = session.createTextMessage("Helloworld..by vinod");
            producer.send(message);
            session.close();
            connection.close();
            System.out.println("Message sent");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
  
3. Create a Message Consumer
package com.vinod.jms;

import javax.jms.ConnectionFactory;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.Message;

public class MessageConsumerExample {
    public static void main(String[] args) {
        try {
            // Create a ConnectionFactory
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "admin",
                    ActiveMQConnection.DEFAULT_BROKER_URL);
            // Create a Connection
            Connection connection = connectionFactory.createConnection();
            connection.start();
            // Create a Session
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            // Create the destination
            Destination destination = session.createQueue("testQ");
            // Create a MessageConsumer from the Session to the Queue
            MessageConsumer consumer = session.createConsumer(destination);
            // Wait for a message
            Message message = consumer.receive(1000);
            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                String text = textMessage.getText();
                System.out.println("Text Message is " + text);
            } else {
                System.out.println(message);
            }
            consumer.close();
            session.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
4. Run it
Once activemq started, then start Message producer first and push the messages in the queue after that run the consumer..
image
Console output after running Message consumer
Text Message is Helloworld..by vinod
5. Download example
https://github.com/kkvinodkumaran/myrepository/tree/master/vinod-jms

0 comments: