在这部分中,我们会做一些完全不同的事情 - 我们会向多个消费者传递相同的信息。这种模式就是“发布/订阅”。该模型中生产者从不将任何消息直接发送到队列,生产者通常甚至不知道要将消息发送到哪个队列,通常是,生产者把消息发送到了 交换机(exchange) 中,然后交换机负责将消息发送到某一个或者多个消息队列中。如图:
交换机还有4种不同的类型,分别是 direct , fanout , topic , headers ,每种类型路由的策略不同。本章节重点在fanout类型。
在第一章节“Hello World”中,
- channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
参数一即是交换机的名称,空字符串指的就是默认交换机,参数二指的就是交换机路由到QUEUE_NAME的队列中。
生产者代码:
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
* @author yangkun
* @date 2017年4月27日 下午4:03:08
* @description
*/
public class EmitLog {
private static final String EXCHANGE_NAME = "logs";
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
Connection conn = factory.newConnection();
Channel channel = conn.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
String message = "Hello World......";
channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
channel.close();
conn.close();
}
}
在这里创建了一个fanout类型的名为logs的交换机
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
再将消息发送到该交换机上
channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
运行结果:
import java.io.IOException;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
/**
* @author yangkun
* @date 2017年4月27日 下午5:13:35
* @description
*/
public class ReceiveLogs {
private static final String EXCHANGE_NAME = "logs";
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
Connection connection = factory.newConnection();
final Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
// 随机生成一个队列名
String queueName = channel.queueDeclare().getQueue();
System.out.println(queueName);
// 队列与交换机的绑定
channel.queueBind(queueName, EXCHANGE_NAME, "");
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println(" [x] Received '" + message + "'");
channel.basicAck(envelope.getDeliveryTag(), false);
}
};
channel.basicConsume(queueName, false, consumer);
}
}
为防止队列名重名,我们可以通过以下方式保证队列名唯一
String queueName = channel.queueDeclare().getQueue();
然后需要将交换机与队列进行绑定
channel.queueBind(queueName, EXCHANGE_NAME, "");
消费者运行结果
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);