RabbitMQ四种Exchange类型之Direct (Java)

来源:互联网 发布:中卫的云计算基地 编辑:程序博客网 时间:2024/06/05 01:07
Direct类型的Exchange是不处理路由键,需要将一个队列绑定到交换机上,要求该消息与一个特定的路由键完全匹配。这是一个完整的匹配。如果一个队列绑定到该交换机上要求路由键为 “logs”,则只有路由键为“logs”的消息才被转发,不会转发路由键为"logs.error",只会转发路由键为"logs"。 
如下图:


代码实现如下!!
Consumer:
package direct;import java.io.IOException;import java.util.concurrent.TimeoutException;import com.rabbitmq.client.AMQP;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;import com.rabbitmq.client.Consumer;import com.rabbitmq.client.DefaultConsumer;import com.rabbitmq.client.Envelope;public class DirectConsumer {private static final String EXCHANGE_NAME = "exchange_direct";public static void main(String[] argv) throws IOException, TimeoutException  {new ExchangeDirect("logs.info");new ExchangeDirect("logs.error");}static class ExchangeDirect{public  ExchangeDirect(String routingKey) throws IOException, TimeoutException {ConnectionFactory factory = new ConnectionFactory();//rabbitmq监听IPfactory.setHost("192.168.249.128");//rabbitmq监听默认端口factory.setPort(5672);//设置访问的用户factory.setUsername("test");factory.setPassword("test");Connection connection = factory.newConnection();Channel channel = connection.createChannel();//声明路由名字和类型channel.exchangeDeclare(EXCHANGE_NAME, "direct", false, true, null);//队列名称String queueName = routingKey + ".queue";//创建队列channel.queueDeclare(queueName, false, false, true, null);//把队列绑定到路由上channel.queueBind(queueName, EXCHANGE_NAME, routingKey);System.out.println(" [routingKey = "+ routingKey +"] Waiting for msg....");Consumer consumer = new DefaultConsumer(channel) {@Overridepublic void handleDelivery(String consumerTag, Envelope envelope,AMQP.BasicProperties properties, byte[] body) throws IOException {String message = new String(body, "UTF-8");System.out.println("[routingKey = "+ envelope.getRoutingKey() +"] Received msg is '" + message + "'");}};channel.basicConsume(queueName, true, consumer);}}}
Producer:
package direct;import java.io.IOException;import java.util.concurrent.TimeoutException;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.ConnectionFactory;public class DirectProducer {private static final String EXCHANGE_NAME = "exchange_direct";public static void main(String[] argv) throws Exception{new ExchangeDirect("logs.info", "logs Info test !!");new ExchangeDirect("logs.error", "logs error test !!");new ExchangeDirect("logs.warning", "logs warning test !!");}static class ExchangeDirect{public ExchangeDirect(String routingKey,String message) throws IOException, TimeoutException{ConnectionFactory factory = new ConnectionFactory();//rabbitmq监听IPfactory.setHost("192.168.249.128");//rabbitmq监听默认端口factory.setPort(5672);//设置访问的用户factory.setUsername("test");factory.setPassword("test");Connection connection = factory.newConnection();Channel channel = connection.createChannel();//声明路由名字和类型channel.exchangeDeclare(EXCHANGE_NAME, "direct", false, true, null);channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());System.out.println("[routingKey = "+routingKey+"] Sent msg is '" + message + "'");channel.close();connection.close();}}}
运行结果:

注意这里的queueName不能通过 channel.queueDeclare().getQueue() 来随机生成!!

祝生活愉快!!!


0 0