ActiveMQ Hello World
This is simple ActiveMQ JMS example using multiple thread, concurrent, producer and consumers. Just to give idea how ActiveMQ works we will create a queue and produce message on it and consumer will consume it. Please follow steps below:
- Create maven java project as below:
- pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javahonk</groupId> <artifactId>ActiveMQHelloWorld</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>ActiveMQHelloWorld</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <repositories> <repository> <id>repository.jboss.org-public</id> <name>JBoss.org Maven repository</name> <url>https://repository.jboss.org/nexus/content/groups/public</url> </repository> </repositories> <dependencies> <dependency> <groupId>javax.jms</groupId> <artifactId>jms</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> <version>5.11.1</version> </dependency> </dependencies> </project>
- Install ActiveMQ using this tutorial
- Once installation is done start ActiveMQ server using below command
- To find where to connect, open server console and search “tcp://” you will see as below that will be the connection point to connect ActiveMQ server. In our case we will connect to “tcp://JavaHonk:61616”
- Java client ActiveMQHelloWorld.java
package com.javahonk; import javax.jms.Connection; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.ExceptionListener; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; public class ActiveMQHelloWorld { public static void main(String[] args) throws Exception { (new Thread(new ActiveMQHelloWorldProducer())).start(); (new Thread(new ActiveMQHelloWorldProducer())).start(); (new Thread(new HelloWorldConsumer())).start(); Thread.sleep(10000); (new Thread(new HelloWorldConsumer())).start(); (new Thread(new ActiveMQHelloWorldProducer())).start(); (new Thread(new ActiveMQHelloWorldProducer())).start(); (new Thread(new HelloWorldConsumer())).start(); } public static class ActiveMQHelloWorldProducer implements Runnable { public void run() { try { // Create ConnectionFactory ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("tcp://JavaHonk:61616"); // Create Connection Connection connection = activeMQConnectionFactory.createConnection(); connection.start(); // Create Session Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE); // Create the destination (Topic or Queue) Destination destination = session.createQueue("JavaHonk"); // Create MessageProducer from the Session to the Topic or // Queue MessageProducer producer = session.createProducer(destination); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // Create messages String text = "Java Honk ActiveMQ Hello world! From: "+ Thread.currentThread().getName() + " : "+ this.hashCode(); TextMessage message = session.createTextMessage(text); // Tell the producer to send the message System.out.println("Sent message: " + message.hashCode()+ " : " + Thread.currentThread().getName()); producer.send(message); // Clean up session.close(); connection.close(); } catch (Exception e) { System.out.println("Caught Exception: " + e); e.printStackTrace(); } } } public static class HelloWorldConsumer implements Runnable, ExceptionListener { public void run() { try { // Create a ConnectionFactory ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("tcp://JavaHonk:61616"); // Create a Connection Connection connection = activeMQConnectionFactory.createConnection(); connection.start(); connection.setExceptionListener(this); // Create a Session Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Create the destination (Topic or Queue) Destination destination = session.createQueue("JavaHonk"); // Create a MessageConsumer from the Session to the Topic or // 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("Received: " + text); } else { System.out.println("Received: " + message); } consumer.close(); session.close(); connection.close(); } catch (Exception e) { System.out.println("Caught exception: " + e); e.printStackTrace(); } } public synchronized void onException(JMSException ex) { System.out.println("ActiveMQ JMS Exception occured. Shutting down client."); } } }
- Output:
- For more details please visit ApacheMQ official site