Apache ActiveMQ is an open-source message broker that facilitates communication between distributed systems by enabling them to send and receive messages asynchronously. It's commonly used in enterprise applications to decouple components, enhance scalability, and ensure reliable communication.
For us, Mulesoft Architects and Developers, it’s a great option to be used as a message broker for our mule apps. If we wanted to test it locally, Docker is a great option. Running ActiveMQ in a Docker container is a straightforward process
Here’s how we’d do it
Pull the ActiveMQ image
First, we need to download the ActiveMQ image
docker pull rmohr/activemq
Create Volumes
We will create 2 docker volumes for the container, one for configuration and the other one for data. This way we can retain data and config even if the container is stopped or removed. Declaring a volume ensures that data written to that location is stored outside the container, making it persistent and not tied to the lifecycle of the container.docker volume create activemq_conf
docker volume create activemq_data
Run the container
To run the container we will use the following command:
docker run -d --name mule-activemq -p 61616:61616 -p 8161:8161 \
-v activemq_conf:/opt/activemq/conf \
-v activemq_data:/opt/activemq/data \
rmohr/activemq
-d
- to run the container in the background--name mule-activemq
- Provides a name for our container-p 61616:61616
- maps the port 61616 in the host to the port 61616 in the container. This is the broker's main connection port-p 8161:8161
- maps the port 8161 in the host to the port 8161 in the container. This is the web console port for browser access-v activemq_conf:/opt/activemq/conf
- Maps the activemq configuration folder to the dedicated volume we created for configuration-v activemq_data:/opt/activemq/data
- Maps the activemq data folder to the dedicated volume we created for datarmohr/activemq
- refers to the image we’ve just downloaded in the first step
Test
To verify the activemq container is running as expected, first check the container is running:docker ps
docker logs [ACTIVEMQ_CONTAINER_NAME]
Lastly, open your web browser and go to http://localhost:8161. You should see the home page of ActiveMQ
- Click on Manage ActiveMQ broker and log in. The default credentials are:
- user: admin
- pass: admin
(Optional) Customize the Configuration
If we need to customize the configuration, we can create a customactivemq.xml
file or update the existing one. The best option for that is to mount it to the container using a volume. Here’s how we’d run the container in that case:
docker run -d --name mule-activemq -p 61616:61616 -p 8161:8161 \
-v activemq_data:/opt/activemq/data \
-v /path/to/your/activemq.xml:/opt/activemq/conf/activemq.xml \
rmohr/activemq
/path/to/your/activemq.xml
with the absolute path to your configuration file.What can we customize with this file?
- Broker settings like name or persistence
- Transport Connectors - Define the protocols and ports for client connections (the web console, the AMQP, the STOMP...)
- Destinations - Predefined queues and topics
- Persistence Store - KahaDB or JDBC for example
- Memory management - Control how memory is allocated for messages
- Authentication & Authorization - To Secure access to the broker
- Plugins - To extend broker functionality
- SSL/TLS Configuration
- Network of Brokers - Configure broker clustering or federation
- Logging - Use
log4j
for detailed logging configuration
Here’s an example of an activemq.xml file that we can use as a template:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- ActiveMQ Broker Configuration -->
<broker xmlns="http://activemq.apache.org/schema/core"
brokerName="myBroker"
useJmx="true"
persistent="true"
schedulerSupport="true"
advisorySupport="true">
<!-- System Usage Limits -->
<systemUsage>
<memoryUsage>
<memoryUsage limit="128mb"/>
</memoryUsage>
<storeUsage>
<storeUsage limit="1gb"/>
</storeUsage>
<tempUsage>
<tempUsage limit="512mb"/>
</tempUsage>
</systemUsage>
<!-- Transport Connectors -->
<transportConnectors>
<!-- OpenWire Protocol (Default) -->
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616"/>
<!-- STOMP Protocol -->
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613"/>
<!-- WebSocket for STOMP -->
<transportConnector name="websocket" uri="ws://0.0.0.0:61614"/>
<!-- Web Console -->
<transportConnector name="admin" uri="http://0.0.0.0:8161"/>
</transportConnectors>
<!-- Destinations -->
<destinations>
<queue physicalName="exampleQueue"/>
<topic physicalName="exampleTopic"/>
</destinations>
<!-- Persistence Store -->
<persistenceAdapter>
<kahaDB directory="${activemq.data}/kahadb" />
</persistenceAdapter>
<!-- Plugins: Authentication and Authorization -->
<plugins>
<!-- Authentication Plugin -->
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="admin" password="admin" groups="admins"/>
<authenticationUser username="user" password="user" groups="users"/>
</users>
</simpleAuthenticationPlugin>
<!-- Authorization Plugin -->
<authorizationPlugin>
<map>
<authorizationMap>
<authorizationEntries>
<authorizationEntry queue=">" read="users" write="admins" admin="admins"/>
<authorizationEntry topic=">" read="users" write="admins" admin="admins"/>
</authorizationEntries>
</authorizationMap>
</map>
</authorizationPlugin>
</plugins>
<!-- Scheduler for Delayed Delivery -->
<schedulerSupport>true</schedulerSupport>
<!-- Network of Brokers (Example) -->
<networkConnectors>
<networkConnector uri="static:(tcp://remote-broker1:61616,tcp://remote-broker2:61616)"
name="bridgeToRemoteBrokers"
duplex="false"/>
</networkConnectors>
</broker>
<!-- SSL Context Configuration (Optional) -->
<sslContext>
<sslContext keyStore="/path/to/keystore.jks" keyStorePassword="password"
trustStore="/path/to/truststore.jks" trustStorePassword="password"/>
</sslContext>
</beans>