Wednesday, December 29, 2010

Short Description of RabbitMQ Messaging BUS.

Those who are knowing JMS and MDB will easily know Messaging terminology. For new beginner before knowing RabbitMQ it will be gr8 to know some of the other topics like

1) What is messaging?

Messaging describes the sending and receiving of data. Having said that sending responsibility will be of Publisher/Sender and receiving responsibility will be of receiver. Both Sender and Receiver are mutually exclusive Entity i.e. unknown of one another. Common point of their meeting a called queue where message send by Sender in format like text, email, image etc will be stored and will be delivered to the receiver who had subscribed for it.

2) Why use AMQP?

AMQP is an Open Standard for Messaging Middleware standard for wire-level protocol and semantic framework for high performance enterprise messaging.

3) What is the Open Telecom Platform (OTP)?

The Open Telecom Platform (OTP) is a library of management, monitoring, and support code for constructing extremely high-performance, reliable, scalable, available distributed network applications written in Erlang.


4) How RabbitMQ use AMQP and OTP?

RabbitMQ enables developers of messaging solutions to take advantage of AMQP and Open Telecom Platform (OTP) i.e. Rabbit MQ is build using AMQP on top of OTP.

5) How RabbitMQ Work?

In Rabbit MQ Framework Sender send the message to the Exchange. Further this Exchange applies logic to differentiate this message and divert it to proper queue. Finally Receiver get the message from Queue as per their subscription.

Different plugin are avaiable best one is to use rabbitmq-management it provide GUI to the end user to create Exchange/Queue throught URL i.e.http://localhost:55672/mgmt/ having default username:guest and passwd:guest

Following JAVA code can be used to send and receive the message using different format of Exchange like fan-out, Direct etc.

Sender Class:

package rabbitMQ;

import com.rabbitmq.client.ConnectionFactory;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.Channel;

public class Send {

public static void main(String[] argv)

throws java.io.IOException {

Connection conn = null;

ConnectionFactory factory = new ConnectionFactory();

factory.setHost("localhost");

/*factory.setUsername("siddhu");

factory.setPassword("siddhu");

factory.setVirtualHost("/");

factory.setHost("localhost");

factory.setPort(5672);*/

conn = factory.newConnection();

Channel chan = conn.createChannel();

//chan.queueDeclare("hello", false, false, false, null);

//String exchangeName = "myExchange";

String exchangeName = "myTopicExchange";

String routingKey = "quick.orange.rabbit";

//for(int i=0;i<10;i++)

for(;;)

{

String s = "Hello World for Exchange myTopicExchange with Routing key quick.orange.rabbit!";

//chan.basicPublish("", "hello", null, s.getBytes());

chan.basicPublish(exchangeName, routingKey, null, s.getBytes());

//chan.basicPublish("", routingKey, null, s.getBytes());

System.out.println(" [x] Sent 'Hello World!'");

}

//chan.close();

//conn.close();

}

}

Receiver Class:

package rabbitMQ;

import com.rabbitmq.client.ConnectionFactory;

import com.rabbitmq.client.Connection;

import com.rabbitmq.client.Channel;

import com.rabbitmq.client.QueueingConsumer;

public class Recv {

public static void main(String[] argv)

throws java.io.IOException,

java.lang.InterruptedException {

Connection conn = null;

ConnectionFactory factory = new ConnectionFactory();

factory.setHost("localhost");

conn = factory.newConnection();

String exchangeName = "myTopicExchange";

//String exchangeName = "logs";

String queueName = "topicQueue2";

//String queueName = "logQueue";

//String queueName = "siddhuQueue";

String routingKey = "*.*.rabbit";

boolean durable = true;

Channel chan = conn.createChannel();

/*chan.exchangeDeclare(exchangeName, "direct", durable);

chan.queueDeclare(queueName, durable,false,false,null);

chan.queueBind(queueName, exchangeName, routingKey);*/

//chan.queueDeclare("hello", false, false, false, null);

System.out.println(" [*] Waiting for Queue2 messages. To exit press CTRL+C");

QueueingConsumer consumer = new QueueingConsumer(chan);

//chan.basicConsume("hello", true, consumer);

boolean noAck = true;

chan.basicConsume(queueName, noAck, consumer);

//by siddhu Start [

/*String exchangeName1 = "mydirectExchange";

String queueName1 = "topicQueue1";

String routingKey1 = "*.orange.*";

System.out.println(" [*] Waiting for Queue1 messages. To exit press CTRL+C");

QueueingConsumer consumer1 = new QueueingConsumer(chan);

chan.basicConsume(queueName1, noAck, consumer1);*/

//by siddhu End ]

while (true) {

QueueingConsumer.Delivery delivery = consumer.nextDelivery();

System.out.println(" [x] Received " + new String(delivery.getBody()));

}

}

}


Note: Do visit to http://www.rabbitmq.com/


1 comment:

SVN commit said...

Awesome