Search Tutorials


ActiveMQ Tutorial | JavaInUse



Getting Started with JMS Messaging- ActiveMQ Hello World Tutorial

JMS is a messaging standard that allows Java EE applications to create, send, receive, and consume messages in a loosely coupled, reliable, and asynchronous way.Its a messaging system that implements the JMS interfaces and provides administrative and control features. It is an API that provides the facility to create, send and read messages.It provides loosely coupled, reliable and asynchronous communication. In this example we use ActiveMQ to explain messaging service.

Lets Begin

Download ActiveMQ from http://activemq.apache.org/download.html. Extract it. In the bin folder click on activemq.bat and start ActiveMQ as follows.
camel7_1
and goto http://localhost:8161/admin/queues.jsp, we will see all no queues on this page.
camel7_2

We will now write java code to create a queue named Queue and produce and consume message from it.
Create the eclipse project as follows
misc2_ec

As shown above include the following jars(available in activemq/lib) to the classpath.
  • geronimo-j2ee-management_1.1_spec-1.0.1.jar
  • geronimo-jms_1.1_spec-1.1.1.jar
  • slf4j-api-1.7.13.jar
  • activemq-client-5.13.0.jar

Create Producer.java-
package com.javainuse.producer;

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

public class Producer implements Runnable {

    public void run() {
        try { // Create a connection factory.
            ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");

            //Create connection.
            Connection connection = factory.createConnection();

            // Start the connection
            connection.start();

            // Create a session which is non transactional
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // Create Destination queue
            Destination queue = session.createQueue("Queue");

            // Create a producer
            MessageProducer producer = session.createProducer(queue);

            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            String msg = "Hello World";

            // insert message
            TextMessage message = session.createTextMessage(msg);
            System.out.println("Producer Sent: " + msg);
            producer.send(message);

            session.close();
            connection.close();
        }
        catch (Exception ex) {
            System.out.println("Exception Occured");
        }
    }
}
Create Consumer.java
package com.javainuse.consumer;

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

public class Consumer implements Runnable {

    @Override
    public void run() {
        try {
            ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");

            //Create Connection
            Connection connection = factory.createConnection();

            // Start the connection
            connection.start();

            // Create Session
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            //Create queue
            Destination queue = session.createQueue("Queue");

            MessageConsumer consumer = session.createConsumer(queue);

            Message message = consumer.receive(1000);

            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                String text = textMessage.getText();
                System.out.println("Consumer Received: " + text);
            }

            session.close();
            connection.close();
        }
        catch (Exception ex) {
            System.out.println("Exception Occured");
        }
    }
}
Create the main class to run the Producer and Consumer Threads.
package com.javainuse.main;

import com.javainuse.consumer.Consumer;
import com.javainuse.producer.Producer;

public class TestMQ {

    public static void main(String[] args) {
        Producer producer = new Producer();
        Consumer consumer = new Consumer();
 
        Thread producerThread = new Thread(producer);
        producerThread.start();
 
        Thread consumerThread = new Thread(consumer);
        consumerThread.start();
    }
}
Run the TestMQ.java as Java Application-
misc2_run
Now go to http://localhost:8161/admin/queues.jsp.
misc2_active

Download Source Code

Download it - ActiveMQ Tutorial

See Also

Understand Java 8 Method References using Simple Example 
Java - PermGen space vs MetaSpace 
Java 8 Lambda Expression- Hello World Example 
Java 8 Features
Java Miscelleneous Topics
Java Basic Topics
Java- Main Menu