之前的例子都是讲消息直接发送给指定的队列;现在需要发送给多个队列,订阅/发布模式
生产者不再将消息直接发往队列,而是发往exchange,然后exchange再发送给在该exchange订阅的队列;
exchange需要知道如何对接收到的消息进行转发
exchange有四种类型:direct, topic, headers and fanout
一:fanout类型
将消息发给所有在exchange注册的队列
channel.exchangeDeclare("logs", "fanout");//1:声明一个fanout的exchange
channel.queueBind(queueName, "logs", "");//将队列在exchange注册
channel.basicPublish("logs", "", null, message.getBytes());//发送消息
二:direct类型
fanout类型的exchange可以将消息发送给在exchange上注册的所有的队列;
队列注册到direct类型的exchange时增加了route