Send 生产者发送消息:
//级别
private static String[] LOG_LEVELS={"error","info","warning","other"};
//服务
private static String[] SERVERS = {"order","user","email"};
发送消息
for(String server : SERVERS){
for(String level:LOG_LEVELS){
String routingKey = server+"."+level;//组合路由键,如 order.error
String message = "";
for(int i=0;i<3;i++){//每一种路由键发送3条消息
message="hello.."+i+"___"+routingKey;
//发送消息指定routing_key
channel.basicPublish(RabbitmqConfigure.EXCHANGE_NAME, routingKey, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
}
}
}
接收消息,队列和routing key的绑定修改:
A:channel.queueBind(queueName, RabbitmqConfigure.EXCHANGE_NAME,"*.error");
B:channel.queueBind(queueName, RabbitmqConfigure.EXCHANGE_NAME,"order.*");
C:channel.queueBind(queueName, RabbitmqConfigure.EXCHANGE_NAME,"user.*");
channel.queueBind(queueName, RabbitmqConfigure.EXCHANGE_NAME,"*.info");
D:channel.queueBind(queueName, RabbitmqConfigure.EXCHANGE_NAME,"#");
发送消息:
A、接收所有error 级别日志( *.error)
B、接收 order 服务的日志(order.*)
C、接收user服务和info级别的日志(user.* *.info)
D、接收所有的日志(#)
不指定发送消息的routing key( routing key 设置为空字符串,channel.basicPublish(RabbitmqConfigure.EXCHANGE_NAME, "", null, "路由键为空字符串".getBytes()); ),绑定路由键为 “*” 的队列到消息,绑定为“#” 的队列可以收到。
绑定键为 #.* 的队列会获取到一个名为..的路由键的消息,它会取到一个路由键为单个单词的消息
rabbitmq官网:http://www.rabbitmq.com/tutorials/tutorial-five-java.html
rabbitmq中文:http://rabbitmq.mr-ping.com/tutorials_with_python/[5]Topics.html
code: http://download.csdn.net/detail/convict_eva/9612865