Monday, July 28, 2014

RabbitMq Hello-World with Eclipse and Maven

This blog post is based on the RabbitMQ Hello World Tutorial. You should read that first. I'll just show you how you can get that example up-and-running using Eclipse rather than on the command line. I'll also include the steps needed to install rabbitmq.

My Environment:

Installing RabbitMQ

Download the deb package. Once its downloaded double-click it to open it in Ubuntu Software Center and install it. That's pretty much it. The message broker is now running with default configuration settings.

You can double check the message broker is running (or start it) like so:
sudo invoke-rc.d rabbitmq-server start
You should see something like this:
* Starting message broker rabbitmq server
* message broker already running
Other useful commands:
sudo invoke-rc.d rabbitmq-server stop
sudo invoke-rc.d rabbitmq-server status

Setting up an Eclipse Project

You'll need a version of Eclipse with M2E installed. I used Spring Tool Suite simply because I already had it installed, and it includes M2E.

Use the "New Maven Project" wizard to create a simple maven jar project. Then put the following stuff in its pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>kdvolder.pivotal.io</groupId>
    <artifactId>rabitmq-hello-world</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>rabitmq-hello-world</name>
    <description>rabitmq-hello-world</description>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <dependencies>
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>3.3.4</version>
        </dependency>
    </dependencies>

</project>
The code from the tutorial can be used pretty much as is. Put it into your maven project's main source folder.


Running The Code


When you are done, you can run the two sample apps 'Send' and 'Recv' by right-clicking them in the Package Explorer and selecting  "Run As >> Java Application".


If you just want to get the code and not copy-paste it together yourself. It is on github.


 

2 comments:

  1. when I run rec.java as java application. I see the below error, I am running this application on windows, m2e and STS are already installed in eclipse.

    ReplyDelete