rabbitmq学习3:Publish/Subscribe
在前面的Work Queue中的消息是均匀分配消息给消费者;如果我想把消息分发给所有的消费者呢?那应当怎么操作呢?这就是要下面提到的Publish/Subscribe(分布/订阅)。让我们开始Publish/Subscribe之旅吧!
Publish/Subscribe的工作示意图如下:

在上图中的X表示Exchange(交换区);Exchange的类型有:direct , topic , headers 和 fanout
Publish/Subscribe的Exchang的类型为fanout;声明Publish/Subscribe的Exchang代码如下:
channel.exchangeDeclare("logs", "fanout"); 对于Work Queue中提到的发布消息的代码如下:
channel.basicPublish("", queueName, null, message.getBytes()); 但对于Publish/Subscribe中发布消息中的Queue的使用的是默认的;代码如下:
channel.basicPublish( "logs", "", null, message.getBytes());
Exchange和各Queue之间是如何通信的呢?主要是通过把Exchange和各Queue绑定(binding);示意代码如下:
channel.queueBind(queueName, exchangeName, "");
Publish/Subscribe加入绑定的工作示意图如下:


那我们就开始程序代码吧:P端的代码如下:
package com.abin.rabbitmq;
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";
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, "fanout");//声明Exchange
for (int i = 0; i <= 2; i++) {
String message = "hello word!" + i;
channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
}
channel.close();
connection.close();
}
} 运行结果如下:
[x] Sent 'hello word!0' [x] Sent 'hello word!1' [x] Sent 'hello word!2'
C端的代码如下:
package com.abin.rabbitmq;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
public class ReceiveLogsOne {
private static final String EXCHANGE_NAME = "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, "fanout");
String queueName = "log-fb1";
channel.queueDeclare(queueName, false, false, false, null);
channel.queueBind(queueName, EXCHANGE_NAME, "");//把Queue、Exchange绑定
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 + "'");
}
}
} 对于C端的代码我写了二个差不多的程序,只需要修改一下queueName。这样就形成了二个Queue;运行结果相同;
运行结果可能如下:
[x] Received 'hello word!0' [x] Received 'hello word!1' [x] Received 'hello word!2'
相关推荐
bailangsyc 2020-05-27
iflreey 2020-04-29
小爷有点狂 2019-11-10
启动的时候加一个参数p则是启动生产者, 否则启动的就是消费者.我们可以启动多个发布者, 多个订阅者, 任何一个发布者发布的的消息都可以同时被所有订阅者收到. 后加入的订阅者不能收到之前发布的历史消息.
八角塘塘主 2019-11-04
wugang0 2019-11-03
lyhubei 2011-06-28
请叫我杰哥 2014-08-20
powrexly 2013-10-27
liym 2012-07-03
知行天下 2012-03-16
androidty 2011-03-12
xiekaikaibing 2017-03-30
李红灯 2019-02-15
zhoucheng0 2014-08-04
MichaelJScofield 2018-01-12
前端外刊评论 2018-01-24
HowIMetYourDad 2018-01-23


