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

[RABBITMQ - ARTICLE:1] Installing RabbitMQ in Ubuntu

This article describes about how to install RabbitMQ in Ubuntu and how to configure it for our use and also explains about the logs available.

Versions: RabbitMQ 3.6.1
Dependency: Erlang 18.2

Before start the installation, we need to install Erlang 18.2 which is a dependency for RabbitMQ. You can find the installation steps for Erlang in my previous blog:

http://ajanthane.blogspot.com/2016/06/installing-erlang-in-ubuntu.html

After successfully installed the Erlang, download the RabbitMQ Server Generic Unix from the below location.
https://www.rabbitmq.com/install-generic-unix.html , Here you can find the latest version.

To find previous versions, you can follow this link.

As here our version is 3.6.1, go to the above link and download the rabbitmq-server-generic-unix-3.6.1.tar.xz.

After extracting, we can start the RabbitMQ Server. Go to

/home/ajanthan/rabbitmq_server-3.6.1/sbin

and execute the below command

ajanthan@ajanthan-ThinkPad-T440p:~/rabbitmq_server-3.6.1/sbin$
./rabbitmq-server start

By Default the management console is not enabled in the installation. To enable it you need to run the below command to install the management plugins.
ajanthan@ajanthan-ThinkPad-T440p:~/rabbitmq_server-3.6.1/sbin$
./rabbitmq-plugins enable rabbitmq_management

After installing the management plugins, if we done a restart the console will look like this.



Commands for start and stop RabbitMQ Server:
ajanthan@ajanthan-ThinkPad-T440p:~/rabbitmq_server-3.6.1/sbin$ ./rabbitmq-server start

ajanthan@ajanthan-ThinkPad-T440p:~/rabbitmq_server-3.6.1/sbin$ ./rabbitmqctl stop
Command to check log files:
ajanthan@ajanthan-ThinkPad-T440p:~/rabbitmq_server-3.6.1$ sudo tail -f var/log/rabbitmq/rabbit@ajanthan-ThinkPad-T440p.log


Now you can access the management console using:
URL: http://localhost:15672
Username: guest
Password: guest

References


[1] https://www.rabbitmq.com/

Installing Erlang in Ubuntu

When installing RabbitMQ, Erlang is a dependency. To install specific version of Erlang you can follow this article.

All the Erlang versions can be downloaded from http://www.erlang.org/download/ location. After selecting the correct version use the below commands to install Erlang in Ubuntu.
sudo apt-get update
sudo apt-get --fix-missing -y install build-essential m4 libncurses5-dev libssh-dev unixodbc-dev libgmp3-dev libwxgtk2.8-dev libglu1-mesa-dev fop xsltproc 
mkdir -p /src/erlang
cd /src/erlang
wget http://www.erlang.org/download/otp_src_18.2.tar.gz 
tar -xvzf otp_src_18.2.tar.gz
chmod -R 777 otp_src_18.2 
cd otp_src_18.2 
./configure
make
sudo make install