手机版
你好,游客 登录 注册
背景:
阅读新闻

RabbitMQ - About ExchangeTypes 总结

[日期:2014-08-04] 来源:Linux社区  作者:alvez [字体:大 中 小]

三.Topic Exchange

如果根据topic exchange用法说明其特征的话反而更麻烦。

topic exchange正如其名,就是根据某种主题而不是特定的标题,也就是可以匹配routing key的一部分或者全部。

topic exchange的routing key可以有多个词组成,词用'.'分隔。

routing key中可以包括'*'或者'#','*'表示一个词,'#'表示0~N个词。

RabbitMQ - About ExchangeTypes 总结

比如消息发布时routing key为"honda.civic.navy",

能接收消息的队列的routing key可以是"honda.civic.navy"或"*.civic.*"或"honda.#"或"#",

但不能是"honda.accord.navy"或"honda.accord.silver"或"*.accord.*"或"ford.#"。

代码如下:

private static String TOPIC_EXCHANGE = "TOPIC_EXCHAGNE";
 
static class TopicProducer {
public static void main(String[] args) throws IOException {
ConnectionFactory connectionFactory = new ConnectionFactory();
Connection connection = connectionFactory.newConnection();
Channel channel= connection.createChannel();;
 
String content = "I miss the conversation";
channel.exchangeDeclare(TOPIC_EXCHANGE, ExchangeTypes.TOPIC);
channel.basicPublish(TOPIC_EXCHANGE, "alvez.dep.FBI.map", null, content.getBytes());
}
}
 
static class TopicConsumer {
public static void main(String[] args) throws IOException, InterruptedException {
ConnectionFactory connectionFactory = new ConnectionFactory();
Connection connection = connectionFactory.newConnection();
Channel channel= connection.createChannel();
 
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, TOPIC_EXCHANGE, "alvez.#");
 
QueueingConsumer consumer = new QueueingConsumer(channel);
String s = channel.basicConsume(queueName, true, consumer);
System.out.println(s);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
String routingKey = delivery.getEnvelope().getRoutingKey();
 
System.out.println("From:" + routingKey + "':'" + message + "'");
}
 
}

四.Headers Exchange

即消息头和队列中声明的消息头匹配时可以通信。

就可以定义多个条件进行匹配这一点来说,headers exchange和topic exchange有些相似。

有时候会给人"为什么会有这种东西?"的感觉,相比topic exchage有什么优势?

如果仅仅说"headers exchange是基于headers的,topic exchange是基于routing key的",这种回答没什么意义。

RabbitMQ - About ExchangeTypes 总结

代码如下,可以看到producer和consumer的routing key是不同的,producer的header通过properties对象传输:

private static String HEADERS_EXCHANGE = "HEADERS_EXCHANGE";
 
static class HeadersProducer {
public static void main(String[] args) throws IOException {
ConnectionFactory connectionFactory = new ConnectionFactory();
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
 
String content = "I miss the conversation";
channel.exchangeDeclare(HEADERS_EXCHANGE, ExchangeTypes.HEADERS);
AMQP.BasicProperties properties = new AMQP.BasicProperties();
Map<String,Object> map = new HashMap<>();
map.put("key1","val1");
properties.setHeaders(map);
 
channel.basicPublish(HEADERS_EXCHANGE, "alvez", properties, content.getBytes());
 
}
}
 
static class HeadersConsumer {
public static void main(String[] args) throws IOException, InterruptedException {
ConnectionFactory connectionFactory = new ConnectionFactory();
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
 
String queueName = channel.queueDeclare().getQueue();
Map<String, Object> headers = new HashMap<>();
headers.put("key1","val1");
headers.put("key2","val2");
headers.put("key3","val3");
headers.put("key4","val4");
channel.queueBind(queueName, HEADERS_EXCHANGE, "",headers);
 
QueueingConsumer consumer = new QueueingConsumer(channel);
String s = channel.basicConsume(queueName, true, consumer);
System.out.println(s);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
String routingKey = delivery.getEnvelope().getRoutingKey();
 
System.out.println("From:" + routingKey + "':'" + message + "'");
}
 
}
 
}

RabbitMQ 的详细介绍:请点这里
RabbitMQ 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-08/105106.htm

linux
相关资讯       RabbitMQ 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款