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
- RabbitMQ四种Exchange类型之Direct (Java)
- RabbitMQ四种Exchange类型之Direct (Erlang)
- RabbitMQ四种Exchange类型之Fanout (Java)
- RabbitMQ四种Exchange类型之Topic (Java)
- RabbitMQ四种Exchange类型之Headers(Java)
- RabbitMQ Exchange模式之Direct
- RabbitMQ四种Exchange类型之Fanout (Erlang)
- RabbitMQ四种Exchange类型之Topic(Erlang)
- RabbitMQ四种Exchange类型之Headers(Erlang)
- rabbitmq direct、fanout、topic 三种Exchange java 代码比较
- RabbitMQ Exchange 模式 - direct
- RabbitMQ的Exchange 模式之direct(指定模式)
- RabbitMQ的四种ExChange
- RabbitMQ:四种ExChange用法
- RabbitMQ三种Exchange模式(fanout,direct,topic)介绍
- RabbitMQ三种Exchange模式(fanout,direct,topic)
- RabbitMQ三种Exchange模式(fanout,direct,topic)
- 【RabbitMQ】——三种Exchange模式(Fanout、Direct、Topic)
- 3.AbstractFactory-抽象工厂模式
- 查看flash分区命令小结
- Encharts 显示国内城市空气质量及领导行程图
- 如何把Activity的布局文件添加至窗口
- 根据日期判断 该日期是本月的第几周
- RabbitMQ四种Exchange类型之Direct (Java)
- C#--三行代码带你理解神秘的拆箱和装箱
- 第七章 Thread-Per-Message
- 【java】文件上传
- Memory Management(5)DXE 内存管理流程
- 中英文按照首字母A~Z顺序排序
- IntelliJ 导出jar包方法及部分问题解决
- php 生成百万数,不重复,并且打乱数字顺序,插入数据库
- 设置eclipse自动生成的author等注释