ActiveMQ Integration

In this post Apache ActiveMQ will be integrated using Spring JMS libraries. Note that an inmemory broker is used for easy demo purposes. Code reference:

POM Dependency

Like all other modules Spring Boot has starter for ActiveMQ too. Following are the dependencies required.


activemq-amqp is required to use amqp protocol to connect to ActiveMQ otherwise a TCP connection string is expected by the library.

Property file configuration

Since here inmemory ActiveMQ is used there is no need to specify user, password and brokerUrl.

Second property i.e. packages.trustAll is to instruct JMS to trust all packages when it has to do object serialization, otherwise ClassNotFoundException will occurr.

If external broker is being used, one can configure its property as shown below:

Check out ActiveMQProperties for more options on how to configure ActiveMQ.


Spring JMS ships with JmsTemplate class which can be used to publish messages to broker. Boot will autoconfigure JmsTemplate instance and it can be autowired directly in classes which handle publishing work.

import org.springframework.jms.core.JmsTemplate;

public class JmsSender implements EventHandler{

    private static final Logger LOGGER = LoggerFactory.getLogger(JmsSender.class);

    private JmsTemplate jmsTemplate;

    public void handle(RegistrationEvent event) {
        jmsTemplate.convertAndSend("members", event);"Registration event send to destination : members");

Here EventHanlder is independent of Jms, it is used to enforce Visitor patter so that each application event has handler associated to it. Below is how RegistrationEvent looks like:

public class RegistrationEvent extends Event {

    private final String username;

    public RegistrationEvent(Member member) {
        username = member.getEmail();

    public void accept(EventHandler handler) {

Event class extended by RegistrationEvent implements Serializable interface so that Jms API is able to write it out on wire.


To create a consumer method has to be designated as JmsListener and destination has to be provided. If object serialization is being used then that object can be used as method parameter. However in real production systems, messaging format should be data models agnostic and something like JSON, XML or Apache Thrift should be used. For simplicity here direct data model is being used.

import org.springframework.jms.annotation.JmsListener;

public class RegistrationEventListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationEventListener.class);

    @JmsListener(destination = "members")
    public void receive(RegistrationEvent event){"Received event {} from members queue", event.getEventType());

Prev      TOC      Next