Getting Started with JMS Messaging- ActiveMQ Hello World Tutorial
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.

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

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");
}
}
}
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-

Now go to http://localhost:8161/admin/queues.jsp.

Download Source Code
Download it - ActiveMQ TutorialSee 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