DEV Community

Carlos A. Martinez
Carlos A. Martinez

Posted on • Edited on

Getting Started with Message Queue (ActiveMQ )

Image description

The message queue paradigm is a sibling of the publisher/subscriber pattern, and is typically one part of a larger message-oriented middleware system. Most messaging systems support both the publisher/subscriber and message queue models in their API, e.g. Java Message Service (JMS).

Apache ActiveMQ is the most popular open source, multi-protocol, Java-based message broker.

Requirements:

  • JDK 8
  • Intellij
  • ActiveMQ 5.16.4

Download ActiveMQ 5.16.4

Extrac All, and enter on bin/win64, double clic activemq

wrapper | --> Wrapper Started as Console
wrapper | Launching a JVM...
jvm 1 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
jvm 1 | Heap sizes: current=247296k free=231812k max=932352k
jvm 1 | Extensions classpath:
jvm 1 | ACTIVEMQ_HOME: ..\..
jvm 1 | ACTIVEMQ_BASE: ..\..
jvm 1 | ACTIVEMQ_CONF: ..\..\conf
jvm 1 | ACTIVEMQ_DATA: ..\..\data
jvm 1 | Loading message broker from: xbean:activemq.xml
jvm 1 | INFO | Apache ActiveMQ 5.16.4 (localhost, ID:CO-IT024363-51943-1647971180517-0:1) is starting
jvm 1 | INFO | Listening for connections at: tcp://CO-IT024363:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1 | INFO | Connector openwire started
jvm 1 | INFO | Listening for connections at: amqp://CO-IT024363:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1 | INFO | Connector amqp started
jvm 1 | INFO | Listening for connections at: stomp://CO-IT024363:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1 | INFO | Connector stomp started
jvm 1 | INFO | Listening for connections at: mqtt://CO-IT024363:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1 | INFO | Connector ws started
jvm 1 | INFO | ActiveMQ WebConsole available at http://127.0.0.1:8161/
jvm 1 | INFO | ActiveMQ Jolokia REST API available at http://127.0.0.1:8161/api/jolokia/
jvm 1 | INFO | Connector vm://localhost started

localhost-queue

User and password for default is admin, admin

Create new queue

Image description

Image description

Publisher

ConnectionFactory factory = new ActiveMQConnectionFactory("admin", "admin","tcp://localhost:61616");        
try {
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("test_queue");

// M E S S A G E
String[] messages = {"First Message", "Second Message", "Thrid Message", "Fourth Message"};

// P R O D U C E R
MessageProducer producer = session.createProducer(destination);

for (String message : messages) {
TextMessage textMessage = session.createTextMessage(message);
producer.send(textMessage);
}           
System.out.println("Message Published");
session.close();
connection.close();
Enter fullscreen mode Exit fullscreen mode

Consumer

ConnectionFactory factory = new ActiveMQConnectionFactory("admin", "admin","tcp://localhost:61616");

try {
Connection connection = factory.createConnection();
connection.start();

Session session = connection.createSession(false,Session.CLIENT_ACKNOWLEDGE);
Destination destination = session.createQueue("test_queue");
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {

public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
 System.out.println(textMessage.getText());
 textMessage.acknowledge();
} catch (JMSException e) {
 e.printStackTrace();
}
}
});
} catch (JMSException e) {
 e.printStackTrace();
}
Enter fullscreen mode Exit fullscreen mode

Topic

Image description

Source: Active MQ

Top comments (0)