Messaging with JMS and RabbitMQ

Pivotal has a new connector available that lets you send and consume messages using Spring’s battle tested JmsTemplate and the JMS API with RabbitMQ as your broker.

In case you’re not familiar, RabbitMQ is an AMQP broker. You can download it on just about any platform. Some even have installers (like brew install rabbitmq on the Mac, and sudo apt-get install rabbitmq-server on Ubuntu Linux).

If you like the JMS API, this connector lets you shift to using RabbitMQ’s rock solid message broker without having to alter the rest of your application.

But this connector also affords you the option to seamlessly migrate towards AMQP messaging with Rabbit by first switching to RabbitMQ as a broker, and later updating your code using Spring AMQP.

If you’re new to Spring JMS and JmsTemplate, please read the rest of this blog to see how simple it makes JMS messaging.

The StockConsumer class contains Spring bean definitions that will be added to the application context at startup. The jmsListener() method creates an object that subscribes to JMS messages and responds accordingly.

It includes:

DefaultMessageListenerContainer – this container will respond asynchronously to messages. Observe how the method signature defines it’s expectation to be injected with a javax.jms.ConnectionFactory. RabbitMQ’s JMS connector implements this spec interface.

The destination name is rabbit-trader-channel.

pubSubDomain has been switched to true. That means it will consume messages in a topic-oriented fashion, allowing other consumers to receive copies as well.

The Receiver class doesn’t implement any JMS spec interfaces. But that’s okay! It is wrapped with Spring’s MessageListenerAdapter. This lets you create message driven POJOs.

We just looked at the consumption of message. Now let’s proceed to writing a stock quote producer. You can find a simple chunk of code here:package com.rabbitmq.jms.sample;

JmsTemplate is a key component used to publish JMS messages. It removes the need for you to deal with JMS’s busy API and instead focus on sending messages.

By using the @EnableAutoConfiguration annotation, Spring Boot is signaled to automatically create a JmsTemplate when it detects spring-jms on your classpath.

The central piece of this demo is the RMQConnectionFactory. This connection factory empowers JmsTemplate to connect to RabbitMQ and use it as a JMS broker. Spring Boot automatically plugs it into the JmsTemplate so you don’t have to lift a finger.

This stock quoting demonstration app uses Spring Scheduling to generate a new quote every five seconds. It starts with a list of stocks and an initial price, then randomly raises or lowers the price by anywhere from 0 – 10% (at random). The key method which publishes the quote can be found in this line:

jmsTemplate.send("rabbit-trader-channel", messageCreator);

Note: For demonstration purposes, the produce and the consumer run inside the same JVM as one application. This is not a requirement. You can easily have each part running in a separate application.

You can see that it prints out the details of connectionFactory, revealing it to be an RMQConnectionFactory. Then as it produces stock quotes in one part, they are transmitted through RabbitMQ and read from the other end.