RabbitMQ(6)Java Client - Routing

RabbitMQ(6)Java Client - Routing

Bindings
A binding is a relationship between an exchange and a queue. This can be simply read as: the queue is interested in messages from this exchange.

channel.queueBind(queueName, EXCHANGE_NAME, "black");

Creating a binding with binding_key.

The meaning of a binding key depends on the exchange type. The fanout exchanges, which we used previously, simply ignored its value.

Direct exchange
We want to extend the log system, and allow filtering messages based on their severity. For example we may want the script which is writing log messages to the disk to only receive critical errors, and not waste disk space on warning or info log messages.

We will use direct exchange instead. The routing algorithm behind a direct exchange is simple - a message goes to the queues whose binding key exactly matches the routing key of the message.

Multiple bindings
It is perfectly legal to bind multiple queues with the same binding key. If all the queues bind with the same binding key and the message is sent with this binding key. It will work some like fanout.

Emitting logs
channel.exchangeDeclare(EXCHANGE_NAME, "direct");

And we will send message with binding key as serverity
channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes());

To simplify things we will assume that 'serverity' can be one of 'info', 'warning', 'error'.

Subscribing
We are going to create a new binding for each severity we're interested in.
String queueName = channel.queueDeclare().getQueue();
for(String severity : argv){   
  channel.queueBind(queueName, EXCHANGE_NAME, severity);
}

All together
EmitLogDrect.java it will send the message with binding key via direct exchange
package com.sillycat.easytalker.rabbitmq.routing;

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

public class EmitLogDirect {

private static final String EXCHANGE_NAME = "direct_logs";

private final static String SERVER_HOST = "localhost";

public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(SERVER_HOST);

Connection connection = factory.newConnection();
Channel channel = connection.createChannel();

channel.exchangeDeclare(EXCHANGE_NAME, "direct");

channel.basicPublish(EXCHANGE_NAME, "info", null, "hello info".getBytes());
channel.basicPublish(EXCHANGE_NAME, "error", null, "hello error".getBytes());
channel.basicPublish(EXCHANGE_NAME, "warning", null, "hello warning".getBytes());
channel.basicPublish(EXCHANGE_NAME, "debug", null, "hello debug".getBytes());

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

}

ReceiveLogsDirect.java it will receive only the error and warning messages.
package com.sillycat.easytalker.rabbitmq.routing;

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

public class ReceiveLogsDirect {

private static final String EXCHANGE_NAME = "direct_logs";

private final static String SERVER_HOST = "localhost";

public static void main(String[] argv) throws Exception {

ConnectionFactory factory = new ConnectionFactory();
factory.setHost(SERVER_HOST);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();

channel.exchangeDeclare(EXCHANGE_NAME, "direct");

String queueName = channel.queueDeclare().getQueue();

channel.queueBind(queueName, EXCHANGE_NAME, "error");
channel.queueBind(queueName, EXCHANGE_NAME, "warning");

QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(queueName, true, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
String routingKey = delivery.getEnvelope().getRoutingKey();
System.out.println(" [x] Received '" + routingKey + "':'" + message
+ "'");
}
}
}



references:
http://www.rabbitmq.com/tutorials/tutorial-four-java.html



你可能感兴趣的:(rabbitmq)