Spring Rabbit, MVC + RabbitMQ Message Producer & Consumer

Spring MVC is a Java Framework for developing Java application using the MVC design pattern. It is a widely used framework with great developer community.

Spring Rabbit is a RabbitMQ implementation that facilitates development of messaging based applications using Spring.

RabbitMQ is a message broker which allows for communication between applications. It allows asynchronous communication.

This post demonstrates how using Spring MVC, you can create a message and send it using RabbitMQ and consume the message within a Spring application.

Pre-requisites

  • A full installation of RabbitMQ Server
  • Git
  • Java SDK
  • bower (not necessary)
  • Maven
  • Basic knowledge of Spring MVC (It will be easier to understand how it all works)

Now let’s dive into the code

Producer

The producer is responsible for preparing the message that is to sent using RabbitMQ. It then publishes the message.

ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();

channel.exchangeDeclare(EXCHANGE_NAME, "fanout");


channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");

channel.close();
connection.close();

The code above initializes a ConnectionFactory, then sets the host to localhost. This can be changed to your own host.

Then it creates a Channel and sets the exchange name.

The message is then published using the channel.

The code above is located at the ProducerService.

Consumer

The consumer is responsible for receiving the message and doing whatever it is supposed to do with it.

First we will configure the Consumer using xml.

The configuration looks something like this below

<rabbit:annotation-driven/>

<rabbit:connection-factory id="connectionFactory" host="localhost" port="5672" username="guest" password="guest"/>

<rabbit:admin connection-factory="connectionFactory"/>

<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/>


<bean id="rabbitListenerContainerFactory"
class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="concurrentConsumers" value="3"/>
<property name="maxConcurrentConsumers" value="10"/>
</bean>

The first line specifies that it will use annotations.

The next line defines the Connection factory with the host, port, username and password.

The configuration above is located at mvc-dispatcher-servlet.xml

Next we dive into the code for Consuming the message

@RabbitListener(bindings = @QueueBinding( value = @Queue(),
exchange = @Exchange(value = EXCHANGE_NAME, type = ExchangeTypes.FANOUT)))
public void consumerMessage(byte[] data) {
String consumedMessage = new String(data);
System.out.println(" [x] Consumed '" + consumedMessage + "'");
}

The above code defines the listener for the messages. The listener has a Queue binding which defined the queue, exchange and type of exchange.

Once a message is received by the broker that comes to the given exchange, it will be passed to this function. The function will then process the message.

The code above is located at the ConsumerService

Running the application

Install dependencies for js by running the command (assuming you already have bower installed in your system)

bower install

You can run the application using maven, this will install the Java dependencies and run the app.

mvn tomcat7:run -Dmaven.tomcat.port=<port>

Open your browser, if running on localhost at port 8081

http://localhost:8081/SpringRabbitMQ/

you will see the screen

Page for writing a sample message
the terminal or logs will display the message that is to be transmitted and the consumed message

The full code is available at

https://github.com/muiruri/SpringRabbitMQSample

Other useful resources

Spring AMQP

Spring MVC

RabbitMQ

I hope this was helpful.