发送端如下:
@Test
public void sendMessage() throws IOException, TimeoutException{
// 创建连接和频道
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
String[] routing_keys = new String[] { "kernal.info", "cron.warning","auth.info", "kernel.critical"};
for (String routing_key : routing_keys){
String msg = UUID.randomUUID().toString();
channel.basicPublish(EXCHANGE_NAME, routing_key, null, msg.getBytes());
System.out.println(" [x] Sent routingKey = "+routing_key+" ,msg = " + msg + ".");
}
channel.close();
connection.close();
}
接收端1:
@Test
public void receiveMessage() throws IOException, TimeoutException, ShutdownSignalException, ConsumerCancelledException, InterruptedException{
// 创建连接和频道
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 声明转发器
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
// 随机生成一个队列
String queueName = channel.queueDeclare().getQueue();
//接收所有与kernel相关的消息
channel.queueBind(queueName, EXCHANGE_NAME, "kernel.*");
System.out.println(" [*] Waiting for messages about kernel. 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 = " + routingKey
+ ",msg = " + message + ".");
}
}
接收端2:
@Test
public void receiveMessage() throws IOException, TimeoutException, ShutdownSignalException, ConsumerCancelledException, InterruptedException{
// 创建连接和频道
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 声明转发器
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
// 随机生成一个队列
String queueName = channel.queueDeclare().getQueue();
// 接收所有与kernel相关的消息
channel.queueBind(queueName, EXCHANGE_NAME, "*.critical");
System.out
.println(" [*] Waiting for critical 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 = " + routingKey
+ ",msg = " + message + ".");
}
}
控制台打印如下:
发送端:
[x] Sent routingKey = kernal.info ,msg = 7471b3ad-0ab9-4fbf-bc8a-a07195c8955d.
[x] Sent routingKey = cron.warning ,msg = 08156483-4ec9-4135-aa9e-c6439ee21aa8.
[x] Sent routingKey = auth.info ,msg = bae12dfc-bcc0-4373-a0d4-54a935c6a186.
[x] Sent routingKey = kernel.critical ,msg = 3664e543-82e4-4ef2-aa2c-e4caf0683040.
接收端1:
[*] Waiting for messages about kernel. To exit press CTRL+C
[x] Received routingKey = kernel.critical,msg = 3664e543-82e4-4ef2-aa2c-e4caf0683040.
接收端2:
[*] Waiting for critical messages. To exit press CTRL+C
[x] Received routingKey = kernel.critical,msg = 3664e543-82e4-4ef2-aa2c-e4caf0683040.
可以看到,我们通过使用topic类型的转发器,成功实现了多重条件选择的订阅。
转自http://blog.csdn.net/lmj623565791/article/details/37706355