`
sillycat
  • 浏览: 2539853 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

RabbitMQ(7)Java Client - Topics

 
阅读更多
RabbitMQ(7)Java Client - Topics

Although using the direct exchange improved our system, it still has limitations - it can't do routing based on multiple criteria.

We want to subscribe to not only logs based on severity, but also based on the source which emitted the log.

Severity info/warn/critical
Facility   auth/cron/kern

Topic exchange
For topic exchange, routing_key must be a list of words.

The binding key must also be in the same form. The logic behind the topic exchange is similar to a direct one. A message sent with a particular rouing key will be delivered to all the queues that are bound with a matching binding key.

* (star) can substitute for exactly one word.
#(hash) can substitute for zero or more words.

We plan our routing key like this '<speed>.<colour>.<species>'.
Q1 "*.orange.*" is interested in all the orange animals
Q2 "*.*.rabbit" and "lazy.#" wants to hear everything about rabbits, and everything about lazy animals.

All come together
package com.sillycat.easytalker.rabbitmq.topic;

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

public class EmitLogTopic {

private static final String EXCHANGE_NAME = "topic_logs";

private final static String SERVER_HOST = "localhost";

public static void main(String[] argv) {
Connection connection = null;
Channel channel = null;

try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(SERVER_HOST);
connection = factory.newConnection();
channel = connection.createChannel();

channel.exchangeDeclare(EXCHANGE_NAME, "topic");

channel.basicPublish(EXCHANGE_NAME, "bird.white.fly", null,
"fly bird, haha.".getBytes());
channel.basicPublish(EXCHANGE_NAME, "bird.black.walk", null,
"walking cock!".getBytes());
channel.basicPublish(EXCHANGE_NAME, "lazy.black.sleep", null,
"sleep warm!".getBytes());
channel.basicPublish(EXCHANGE_NAME, "fast.white.rabbit", null,
"running fast rabbit!".getBytes());

} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (Exception ignore) {
}
}
}
}

}

package com.sillycat.easytalker.rabbitmq.topic;

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

public class ReceiveLogsTopic1 {

private static final String EXCHANGE_NAME = "topic_logs";

private final static String SERVER_HOST = "rd.digby.com";

public static void main(String[] argv) {
Connection connection = null;
Channel channel = null;
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(SERVER_HOST);
connection = factory.newConnection();
channel = connection.createChannel();

channel.exchangeDeclare(EXCHANGE_NAME, "topic");

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

channel.queueBind(queueName, EXCHANGE_NAME, "lazy.#");

System.out
.println("
  • Waiting for messages. To exit press CTRL+C");
  • 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 + "'");
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (connection != null) {
    try {
    connection.close();
    } catch (Exception ignore) {
    }
    }
    }
    }
    }

    ReceiveLogsTopic1 will receive all lazy animals. The only difference from ReceiveLogsTopic2 to ReceiveLogsTopic1 is as follow:
    channel.queueBind(queueName, EXCHANGE_NAME, "*.white.*");

    And ReceiveLogsTopic2 will focus on all the white colour animals.

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




    分享到:
    评论

    相关推荐

      rabbitmq-java-client-bin-3.3.4.zip

      在"rabbitmq-java-client-bin-3.3.4.zip"这个压缩包中,包含的是RabbitMQ的Java客户端库,这是与RabbitMQ服务器通信的一个关键组件。RabbitMQ提供了多种语言的客户端,Java客户端则是针对Java开发者设计的,使得Java...

      rabbitmq-java-client-bin-3.3.4

      在“rabbitmq-java-client-bin-3.3.4”这个压缩包中,包含了该版本的Java客户端库及相关文档,为Java应用提供了可靠的异步通信支持。 首先,RabbitMQ是基于AMQP(Advanced Message Queuing Protocol)协议的开源...

      rabbitmq-java-client-bin-2.7.0.zip

      在这个"rabbitmq-java-client-bin-2.7.0.zip"压缩包中,我们主要关注的是RabbitMQ的Java客户端库。 RabbitMQ Java客户端是Java开发者与RabbitMQ服务器进行交互的主要工具,允许程序发送和接收消息。2.7.0是这个...

      rabbitmq-java-client-3.4.1.zip

      在这个"rabbitmq-java-client-3.4.1.zip"压缩包中,包含的是RabbitMQ Java客户端库的3.4.1版本,这个版本的客户端可以让你在Java应用中轻松地与RabbitMQ服务器进行交互。 **RabbitMQ核心概念:** 1. **消息**:是...

      rabbitmq-java (2).zip

      在这个名为"rabbitmq-java (2).zip"的压缩包中,我们可以看到几个关键文件,它们构成了一个使用Java与RabbitMQ交互的项目。 1. `rabbitmq-java.iml`:这是IntelliJ IDEA项目文件,包含了项目的模块设置和依赖关系。...

      rabbitmq-java-client-bin-3.0.4.zip

      在这个场景中,"rabbitmq-java-client-bin-3.0.4.zip"是一个包含RabbitMQ Java客户端库的压缩包,适用于Android应用和后台服务器之间的通信。 首先,我们来了解一下RabbitMQ Java客户端。这个客户端库允许Java...

      RabbitMQ rabbitmq-server-3.6.12-1.el6.noarch 及其安装所需要的软件打包

      RabbitMQ rabbitmq-server-3.6.12-1.el6.noarch 及其安装所需要的软件打包都在这里面,主要报卡一下软件:socat-1.7.3.2.tar.gz、rabbitmq-server-3.6.12-1.el6.noarch.rpm、rabbitmq-release-signing-key.asc、otp_...

      rabbitmq-java-client-master.zip

      【标题】:“rabbitmq-java-client-master.zip”是一个包含RabbitMQ Java客户端库源代码的压缩文件,用于在Java应用程序中与RabbitMQ消息队列进行交互。 【描述】:“rabbitmq-server-3.7.9.exe”是RabbitMQ服务器...

      rabbitmq-client-1.3.0.jar

      本文将详细探讨RabbitMQ的客户端库——rabbitmq-client-1.3.0.jar,以及它在Java应用程序中的应用。 首先,`rabbitmq-client-1.3.0.jar`是RabbitMQ官方提供的Java客户端库,用于与RabbitMQ服务器进行通信。这个版本...

      rabbitMQ实战java版-rabbitMQ-demo.zip

      《RabbitMQ实战Java版——基于rabbitMQ-demo.zip的详解》 在当今的分布式系统中,消息队列作为异步处理、解耦组件的关键技术,得到了广泛应用。RabbitMQ作为一款开源的消息代理和队列服务器,以其稳定性和易用性...

      rabbitmq-dotnet-client-3.5.0

      7. 持久化与确认模式:为了保证高可用性,开发者可以设置消息和队列的持久化,即使服务器重启也不会丢失数据。另外,还可以开启publisher confirms或consumer acknowledgments,确保消息已被正确处理。 8. 性能优化...

      rabbitmq-java-client-javadoc-2.7.0.zip

      在Java客户端中,`com.rabbitmq.client`包提供了主要的API类和接口。`ConnectionFactory`是创建连接到RabbitMQ服务器的工厂类,它允许配置诸如主机名、端口、用户认证等参数。`Connection`对象代表了与RabbitMQ...

      rabbitmq-dotnet-client-3.6.4-dotnet-4.6.1.rar

      RabbitMQ-dotnet-client-3.6.4-dotnet-4.6.1.rar是一个包含RabbitMQ .NET客户端库的压缩包,适用于.NET Framework 4.6.1环境。这个压缩包提供了一个演示如何在WCF(Windows Communication Foundation)服务中使用...

      rabbitmq-client.jar

      rabbitmq的javaClient库,导入到项目中便可使用

      Java Rabbitmq-client

      Java 客户端库 RabbitMQ 遵循AMQP协议,那是一个开放的,并且通用的消息协议。java Android RabbitMQ可以用来发送和接收消息

      rabbitmq-java-client-2.7.0.zip

      《RabbitMQ Java客户端2.7.0详解》 RabbitMQ Java客户端2.7.0是用于与RabbitMQ消息代理进行交互的Java库,它提供了丰富的API,使得Java开发者能够轻松地在应用程序中集成消息队列功能。RabbitMQ作为一款开源的消息...

      RabbitMQ amqp-client-5.6.0.jar

      只能用于 android 7.0 以及后面的版本。别人的demo https://blog.csdn.net/lamunuosang/article/details/72287843

      RabbitMQ使用jar包-amqp-client-5.2.0.jar

      使用JAVA进行运用了RabbitMQ的程序时所需的源码包,此包导入工程之后便可使用。

      rabbitmq-server-3.8.13-1.el7.noarch.rpm

      rabbitmq-server-3.8.13-1.el7.noarch.rpm

      rabbitmq-server-generic-unix-3.5.7.tar.rar下载,rabbitmq安装包

      在您提供的资源中,“rabbitmq-server-generic-unix-3.5.7.tar.rar”是一个针对Linux平台的RabbitMQ服务器的离线安装包。这个版本为3.5.7,您需要在Windows环境下解压后再用于Linux系统。下面将详细介绍RabbitMQ的...

    Global site tag (gtag.js) - Google Analytics