Getting Started with Message Queue (ActiveMQ )
Published: 2022-03-22 18:59:08

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/929hhnviq68ljujcg4td.png)
 
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](https://activemq.apache.org/activemq-5016004-release)

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](http://localhost:8161/)

User and password for default is admin, admin

### Create new queue

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5oxjh30l2r6c3jegh4za.png)
 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5hqqu3ko2q2i5ii0pxnh.png)

## 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();
```

## 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();
}
``` 

# Topic

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i2nxawu25t6j7pskc1bg.png)
 



Source: [Active MQ](https://activemq.apache.org/)