Sunday, 22 March 2015

Implementing AMQP based messaging solutions using Spring concepts for RabbitMQ Fanout Exchange (XML based configuration)


In this example we will create a single java application which will send and receive messages from RabbitMQ using spring amqp.

Design:
  1. Exchange type: Fanout exchange
  2. Input argument: String
  3. Queue argument: default arguments
  4. Configuration type: xml based configuration
Note about design: In this example we will start with the basic design and then try to improve on this in the coming examples
  • We can pass String or Message(org.springframework.amqp.core.Message) as input argument. We will see what are the advantages of using each type in the coming posts
  • When we create a queue, it is created with few default values like durable as true, auto delete as false etc, we will try to understand what each value means and also configure other optional parameters like TTL, Dead letter exchange etc and understand the behavior
  • We will also look how to configure using annotations in the comings tutorials

In this tutorial, we will create 2 queues (myQueue1 and myQueue2) and bind them to fanout exchange (myExchange) created. In our main class, we send the messages to the exchange and retrieve message both the queues which are bound to this exchange.



Prerequisites:
  1. RabbitMQ is installed locally
  2. Maven is configured

Step 1: Update the pom.xml with spring-amqp and spring-core dependencies
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
            <version>1.4.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.2.RELEASE</version>
        </dependency>

Step 2: Update the spring context xml with rabbit connection, exchange and queue details
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
 xsi:schemaLocation="http://www.springframework.org/schema/rabbit
           http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
<rabbit:connection-factory id="connectionFactory"
        host="localhost" username="guest" password="guest" />

    <rabbit:template id="rabbitTemplate"
        connection-factory="connectionFactory" exchange="myExchange" />

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

    <rabbit:queue name="myQueue1" />
    <rabbit:queue name="myQueue2" />

    <rabbit:fanout-exchange name="myExchange">
        <rabbit:bindings>
            <rabbit:binding queue="myQueue1" />
            <rabbit:binding queue="myQueue2" />
        </rabbit:bindings>
    </rabbit:fanout-exchange>
In the xml file, we declare connection factory, rabbit template, queues, bindings etc. Among these few attributes are optional depending on the design or development approach of your application. In order to understand more about each attribute please click here

Step 3: Create main class to send and receive messages using RabbitTemplate
     AbstractApplicationContext context = new ClassPathXmlApplicationContext("rabbit-context.xml");
     RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
     rabbitTemplate.convertAndSend("Message");
        
     String msg1  = (String) rabbitTemplate.receiveAndConvert("myQueue1");
     System.out.println(msg1);
     String msg2  = (String) rabbitTemplate.receiveAndConvert("myQueue2");
     System.out.println(msg2);

When we run the above class, rabbitTemplate sends the message string to the exchange which is configured in the rabbit-context.xml and then receive the message from the one of the queues specified in the argument of the receive method.

In the next tutorial, we will look at how to add a listener class so that we process the messages automatically

No comments:

Post a Comment