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

RabbitMQ(5)Java Client - Publish/Subscribe

 
阅读更多
RabbitMQ(5)Java Client - Publish/Subscribe

We will deliver the messages to multiple consumers. This pattern is known as "publish/subscribe".

Exchanges
The Producer will not directly contact to the queues. We will have a exchange between them. And the exchange must know exactly what to do with a message it receives. Should it be appended to a particular queue? Should it be appended to many queues? Or should it get discarded. The rules for that are defined by the exchange type.

There are few exchange types available: direct, topic, headers and fanout.
channel.exchangeDeclare("logs", "fanout");
Fanout just broadcasts all the messages it receives to all the queues it knows.

We can use this command to see all the exchanges running on our server
>sudo sbin/rabbitmqctl list_exchanges

Nameless exchange
Before, we send our message using a default exchange, which we identify by the empty string("").
channel.basicPublish("", "hello", null, message.getBytes());

Temporary queues
Before we have "hello" and "task_queue". Being able to name a queue was crucial for us. Giving a queue a name is important when you want to share the queue between producers and consumers.

In the Java client, when we supply no parameters to queueDeclare() we create a non-durable, exclusive, autodelete queue with a generated name:

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

Bindings
The relationship between exchange and a queue is called a binding.
channel.queueBind(queueName, "logs", "");

>sudo sbin/rabbitmqctl list_bindings

Putting it all together
Emit the message
package com.sillycat.easytalker.rabbitmq.publish;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class EmitLog {

private static final String EXCHANGE_NAME = "logs";
private final static String SERVER_HOST = "localhost";
public static void main(String[] argv) throws Exception {

ConnectionFactory factory = new ConnectionFactory();
factory.setHost(SERVER_HOST);

Connection connection = factory.newConnection();
Channel channel = connection.createChannel();

channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

String message = "error debugy inform warning log!";
channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
channel.close();
connection.close();
}
}

Receiving the log messages
package com.sillycat.easytalker.rabbitmq.publish;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
public class ReceiveLogs {
private static final String EXCHANGE_NAME = "logs";
private final static String SERVER_HOST = "localhost";
public static void main(String[] argv) throws Exception {

ConnectionFactory factory = new ConnectionFactory();
factory.setHost(SERVER_HOST);

Connection connection = factory.newConnection();
Channel channel = connection.createChannel();

channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, "");

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());
    System.out.println(" [x] Received '" + message + "'");
    }
    }
    }

    Queue belongs to the consumer, binding with the exchange.

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

    分享到:
    评论

    相关推荐

      rabbitmq-java-client-master.zip

      5. **RabbitMQ工作模式**:RabbitMQ支持发布/订阅(Publish/Subscribe)、点对点(Point-to-Point)、路由(Routing)、主题(Topic)等多种消息模式,满足不同应用场景的需求。 6. **Java客户端使用**:使用...

      rabbitMQ java Client

      在`rabbitmq-java-client-2.7.0`这个版本中,你可以找到RabbitMQ Java客户端的API文档和库文件,帮助开发者更好地理解和使用这个客户端。这个版本可能已经较旧,但其基本概念和使用方式仍然适用于最新的RabbitMQ ...

      rabbitmq 7种队列实现java版

      文章目录rabbitmq7种实现方式搭建maven项目引入依赖创建连接简单队列消息生产者消息消费者work queues 工作队列生产者消费者能者多劳(公平分发):消费能力强则消费更多消息Publish/Subscribe 发布订阅模式生产者...

      RabbitMQ实战

      RabbitMQ 支持多种消息发布模式,包括简单模式(Simple)、工作队列模式(Work Queues)、发布/订阅模式(Publish/Subscribe)、路由模式(Routing)、主题模式(Topics)等。 #### 二、安装RabbitMQ ##### 2.1 ...

      RabbitMQ HTTP API client for Java, Groovy, and other JVM l.zip

      client.publish(exchange, routingKey, message.getBytes()); ``` 4. **消费消息**:设置回调函数以接收并处理消息。 ```java client.getQueueConsumer(queueName).subscribe(message -> { System.out.println(...

      rabbitMQ实战

      - **Publish/Subscribe (Pub/Sub)**: 发布订阅模式,生产者发布消息,多个消费者订阅。 #### 五、RabbitMQ安装与配置 - **安装文档**: [http://www.rabbitmq.com/getstarted.html]...

      RabbitMQClient_订阅测试.zip_rabbitmq

      在实际使用中,RabbitMQ的订阅模式是基于发布/订阅(Publish/Subscribe)模型,其中生产者发送消息到一个交换机,然后由交换机根据预定义的路由规则将消息分发到一个或多个队列。消费者订阅这些队列,当有新消息到达...

      根据《RabbitMQ》实战指南敲一遍代码-RabbitMQ-demo.zip

      1. **Client Libraries**:项目可能包含了多种编程语言(如Java, Python, .NET等)的客户端库示例,用于连接RabbitMQ broker,发布和接收消息。 2. **Basic Publish/Consume**:基础的发布/消费示例,展示如何创建一...

      MQ学习文档 方便回顾!

      - Publish/Subscribe:发布/订阅模式。 - Routing:路由模式。 - Topics:主题模式。 - Headers:头部模式。 **2.3 导入Demo工程** - **结构**: - `mq-demo`:父工程,管理依赖。 - `publisher`:消息发送...

      rabbitmqdemo

      在 "rabbitmqdemo" 中的基础版示例中,你可以看到如何使用 RabbitMQ 的 Java 客户端库(rabbitmq-client)创建生产者和消费者。生产者会创建消息并发送到指定的交换器,消费者则订阅感兴趣的队列,等待接收消息。...

      RabbitMQ.docx

      3. **Publish/Subscribe(发布/订阅)**:生产者发布消息到主题(Topic),消费者订阅主题并接收消息。这种模式允许一对多的通信。 4. **Routing(路由选择)**:通过路由键将消息路由到特定的队列,支持简单的匹配...

      rabbimqclient(C++)

      RabbitMQ遵循发布/订阅(Publish/Subscribe)、工作队列(Work Queues)、直接交换(Direct Exchange)、主题交换(Topic Exchange)和头部交换(Header Exchange)等多种消息交换模式。C++客户端库为这些模式提供了相应的API...

      RabbitMQ学习案例Demo

      1. **发布/订阅模式(Publish/Subscribe)** 在这种模式下,生产者(Publisher)发送消息到一个主题(Topic),而多个消费者(Subscriber)可以订阅该主题,接收到所有发布到该主题的消息。这种模式适用于一对多的...

      rabbitmq-7example:rabbitmq官方七个例子

      **RabbitMQ官方七个例子详解** ...在Java项目中,可以利用RabbitMQ的Java客户端库`rabbitmq-client`来实现这些功能。为了运行这些示例,你需要下载并安装RabbitMQ服务器,然后运行提供的Java代码,以便观察其工作原理。

      JMS sub/pub实现聊天系统

      在"JMS sub/pub实现聊天系统"中,我们主要探讨的是如何利用JMS的发布/订阅(Publish/Subscribe)模型来构建一个聊天系统。 在JMS中,有两种消息传递模型:点对点(Point-to-Point)和发布/订阅(Publish/Subscribe...

      rabbitmq的basic、workqueue、subsrcibe、direct、topic等示例

      对于Java开发者,这些示例使用了RabbitMQ的Java客户端库,包括`com.rabbitmq.client`包下的API。 理解并实践这些示例有助于深入掌握RabbitMQ的用法,无论是在微服务架构中作为服务间通信的桥梁,还是在大型应用中...

      使用org.eclipse.paho.client.mqttv3实现mqtt 消息队列

      - 使用`MqttClient`的`publish()`方法将消息发送到指定的主题。 3. **订阅主题**: - 使用`subscribe()`方法订阅感兴趣的主题,可以同时订阅多个主题。 - 参数可以包含主题和对应的QoS级别,用于决定接收到消息...

      RabbitMQTrial.zip

      在这个名为“RabbitMQTrial.zip”的压缩包中,包含了两个示例项目,分别展示了RabbitMQ的工作者模式(Work Queue)和发布订阅模式(Publish/Subscribe),以及一个解决方案文件(RabbitMQTrial.sln),便于开发者...

      高级Java人才培训专家-1-第一章 kafka基础

      - **发布/订阅模式**(Publish/Subscribe, Pub/Sub): - 特点:发布者将消息发送到主题,所有订阅该主题的消费者都能接收到消息。 - 场景示例:新闻更新通知,多个用户订阅同一新闻频道。 **常见消息队列产品** ...

      mq_get.zip

      - 对于不同MQ服务,通常都有官方或社区提供的API和客户端库,如RabbitMQ的amqp-client,Kafka的Java和Python客户端等。 5. **消息获取方式**: - Pull模式:消费者主动从队列中拉取消息,适用于消费者有固定调度...

    Global site tag (gtag.js) - Google Analytics