Topic匹配模式
来源:互联网 发布:猪肉绦虫知乎 编辑:程序博客网 时间:2024/06/06 02:03
topic类型的交换器允许在RabbitMQ中使用模糊匹配来绑定自己感兴趣的信息:
*(星号)表示一个单词
#(井号)表示零个或者多个单词
RoutingKey为“black.critical.high”的日志会投递到queue1和queue2,。
RoutingKey为“red.critical.high”的日志会只投递到queue2。
RoutingKey为“white.critical.high”的日志会投递到queue2,并且虽然queue2的两个匹配规则都符合但只会向queue2投递一份。
public class Producer { private static final String EXCHANGE_NAME = "topic_logs"; public static void main(String[] argv) { Connection connection = null; Channel channel = null; try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); channel = connection.createChannel();//声明一个匹配模式的交换器 channel.exchangeDeclare(EXCHANGE_NAME, "topic"); // 待发送的消息 String[] routingKeys = new String[]{"black.critical.high", "red.critical.high", "white.critical.high", };//发送消息 for(String severity :routingKeys){ String message = "From "+severity+" routingKey' s message!"; channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes()); System.out.println("TopicSend [x] Sent '" + severity + "':'" + message + "'"); } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (Exception ignore) { } } } }}
public class Consumer01 { private static final String EXCHANGE_NAME = "topic_logs"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel();//声明一个匹配模式的交换器 channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String queueName = channel.queueDeclare().getQueue(); // 路由关键字 String[] routingKeys = new String[]{"#.high", "white.critical.*"};//绑定路由关键字 for (String bindingKey : routingKeys) { channel.queueBind(queueName, EXCHANGE_NAME, bindingKey); System.out.println("ReceiveLogsTopic2 exchange:"+EXCHANGE_NAME+", queue:"+queueName+", BindRoutingKey:" + bindingKey); } Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println("ReceiveLogsTopic2 [x] Received '" + envelope.getRoutingKey() + "':'" + message + "'"); } }; channel.basicConsume(queueName, true, consumer); }}
public class Consumer02 { private static final String EXCHANGE_NAME = "topic_logs"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel();//声明一个匹配模式的交换器 channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String queueName = channel.queueDeclare().getQueue(); // 路由关键字 String[] routingKeys = new String[]{"black.critical.high"};//绑定路由关键字 for (String bindingKey : routingKeys) { channel.queueBind(queueName, EXCHANGE_NAME, bindingKey); System.out.println("ReceiveLogsTopic1 exchange:"+EXCHANGE_NAME+", queue:"+queueName+", BindRoutingKey:" + bindingKey); } System.out.println("ReceiveLogsTopic1 [*] Waiting for messages. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println("ReceiveLogsTopic1 [x] Received '" + envelope.getRoutingKey() + "':'" + message + "'"); } }; channel.basicConsume(queueName, true, consumer); }}
参考文章:
http://blog.csdn.net/Super_RD/article/details/70850453
阅读全文
0 0
- Topic匹配模式
- MQTT topic匹配规则基础
- RabbitMQ Exchange 模式 - topic
- rabbitmq 交换模式-Topic
- rabbitmq 交换模式-Topic
- Topic和Direct、Fanout匹配解析
- RabbitMQ Exchange模式之Topic
- RabbitMQ消息分发模式----"Topic"主题模式
- RabbitMQ消息分发模式----"Topic"主题模式
- topic
- weblogic JMS 初体验Topic模式
- 3,ActiveMQ-Topic订阅发布模式
- spring boot整合RabbitMQ(Topic模式)
- 模式匹配
- 模式匹配
- 模式匹配
- 模式匹配
- 模式匹配
- Zocodc/Zillow/Qualtrics面试题
- Java进程CPU使用率高排查
- block写法
- IQKeyboardManager使用导航上移问题
- Java类初始化
- Topic匹配模式
- java内存模型
- LightOJ 1401
- windows 下多socket连接(二)
- 使用formdata+ajax文件上传中遇到的问题
- 为什么计算机会有补码这种奇怪的规则?
- objective-C call
- LinkedList源码
- 致tensorflow