Saturday, June 11, 2016

[RABBITMQ - ARTICLE:2] Connecting to RabbitMQ using Java Client

Consider, here the samples are given to the Queue / Topic is created in RabbitMQ virtual host, not in the default / host.

For the installation of RabbitMQ, follow my previous post on it. http://ajanthane.blogspot.com/2016/06/rabbitmq-article-1-installing-rabbitmq.html

Creating a Virtual Host, Queue, Topic and User in RabbitMQ through management console. You can follow the below screen shots to achieve it.

Log into Management Console -> Select Virtual Hosts 

 Create New Virtual Host
 Select Users
Create a New User
 Give permission to the created user to access the created virtual host.


Give permission to guest user to access new virtual host, this will help us to use guest user to create queue, topic through management console.

Create a queue in virtual host.

Create a new Exchange.

Publishing Directly to Queue in RabbitMQ

 

<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>RabbitMQClient</groupId>
  <artifactId>RabbitMQClient</artifactId>
  <version>0.0.1</version>
  <name>RabbitMQClient</name>
  <description>RabbitMQClient</description>
  <dependencies>
  <dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>3.6.1</version>
</dependency>
</dependencies>
<repositories>
<repository>
        <id>sonatype-nexus-staging</id>
        <name>Nexus Release Repository</name>
        <url>http://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
      </repository>
  </repositories>  
</project>


import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;


public class SendMessageToQueue {
 
 private static final String QUEUE_NAME = "TEST_EVENT_Q";

    public static void main(String[] argv)
            throws java.io.IOException, TimeoutException {

        System.out.println("Message Sending started........");

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setVirtualHost("EVENTS");
        factory.setUsername("wso2user");
        factory.setPassword("wso2user");
        factory.setPort(5671); // Here the 5671 is the TCP Listener Port
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        // channel.queueDeclare(QUEUE_NAME, true, false, false, null); 
        // If queue created already the above queue declaration is not needed.
        String message = "Test Message to Queue";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");

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

}


Publishing Through Topic Exchange


Here we need to bind the Topic to a queue. So, when we send a message with a routing key to the exchange, based on the routing key it will be routed to Queue.



import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

public class SendMessageToRabbitMQ {

    private static final String EXCHANGE_NAME = "EVENTS_EXC";

    public static void main(String[] argv)
            throws java.io.IOException, TimeoutException {

        System.out.println("Message Sending started........");

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setVirtualHost("EVENTS");
        factory.setUsername("wso2user");
        factory.setPassword("wso2user");
        factory.setPort(5671); // Here the 5671 is the TCP Listener Port
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //channel.exchangeDeclare(EXCHANGE_NAME, "topic", true, false, false, null);
        // As we already have the exchange the decalaration is not needed.
        String message = "Test Message to RabbitMQ Exchange from Java Client";

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

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

References


[1] https://www.rabbitmq.com/tutorials/tutorial-one-java.html
[2] https://www.rabbitmq.com/tutorials/tutorial-three-java.html

No comments:

Post a Comment