Wednesday, July 15, 2015

RabbitMQ client to publish JMS messages to WSO2 Message Broker 3.0.0 using Java

Hi All,

WSO2 Message Broker 3.0.0 is a distributed message broker that supports AMQP and MQTT.

This post will explain on how the RabbitMQ java client can be used to publish JMS messages to WSO2 Message Broker 3.0.0 with the help of Maven.

Add the RabbitMQ java client dependency

Add the following dependency the pom.xml file

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

The Code

Following is the implementation. See descriptions inline.

// The queue name
String queueName = "MyQueue";

// Creating the AMQP connection string for communication
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
factory.setVirtualHost("/carbon");
factory.setUsername("admin");
factory.setPassword("admin");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();

// Creating the queue
channel.queueDeclare(queueName, true, false, false, null);

// Binding the queue to "amq.direct" exchange. Exchanges cannot be declared in WSO2 MB 3.0.0.
channel.queueBind(queueName, "amq.direct", queueName);

// Message content to publish
String message = "This is a test message";

// Creating properties for the message.
AMQP.BasicProperties.Builder properties = new AMQP.BasicProperties.Builder();
properties.messageId("ID:" + String.valueOf(UUID.randomUUID()));
properties.contentType("text/plain");

// Publishing message
channel.basicPublish("amq.direct", queueName, properties.build(), message.getBytes());

System.out.println("Message published : " + message);

// Closing the connection
channel.close();
connection.close();

Output on management console.

Click here to download the sample maven project.

References...

No comments:

Post a Comment