Getting started with Spring, RabbitMQ and Maven on MacOS

So, you want to get started with RabbitMQ in a Maven project as soon as possible? Well, bear with me here, and we’ll get you up and running in no time!

First things first: you need to install RabbitMQ. You can do this by using MacPorts:
sudo port install rabbitmq-server

After RabbitMQ has been installed and is running, it’s time to create the project. Using the following Maven POM and standard Maven structure, we create our project:



    
        epub-organizer
        epub-organizer
        1.0
    
    4.0.0

    epub-organizer-amqp-rabbitmq

    
        1.1.1.RELEASE
    

    
        
            org.springframework.amqp
            spring-amqp
            ${spring.amqp.version}
        
        
            org.springframework.amqp
            spring-rabbit
            ${spring.amqp.version}
        
    

This pom.xml includes all the dependencies needed to make the client connect with RabbitMQ

Next up, is the code to receive some messages, and to produce some messages. We’ll call them the Producer and Consumer.

package nl.jworks.epub.organizer.amqp.rabbitmq;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class Producer {
    public static void main(String[] args) throws Exception {
        ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
        AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);

        amqpTemplate.convertAndSend("helloworld.queue", "Hello RabbitMQ");
    }
}

and the consumer:

package nl.jworks.epub.organizer.amqp.rabbitmq;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class Consumer {
    public static void main(String[] args) throws Exception{
        ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
        AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);

        System.out.println(amqpTemplate.receiveAndConvert("helloworld.queue"));
    }
}

and we’re almost done. All we need now is the Spring applicationContext.xml, which will create the RabbitMQ queue, and wire everything together:



    
    

    
    

    
    

    
    

If you start the Producer now, the Producer will send a message to the automatically created queue (you can see this in the web console). When running the Consumer, it will get the message from the RabbitMQ queue, and will convert and display it in the console. Now you’ve sent your first message on RabbitMQ!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>