一、概念和模型
- 主题模式,基于通配符的方式,将一个消息推送给不同消费者。
- 通配符有*和#,*表示匹配一个单词,#表示匹配一个或多个单词,单词之间通过‘.’进行区分。如消息发送者的routing-key为log.error,那么消费者绑定routing-key为log.#或log.*时,都可以接收到发送的信息。但消息发送者的routing-key为log.error.out_of_memory时,只有routing-key为log.#的消费者能接受到消息。
二、Java代码实现(发送者发送两条消息,routing-key,分别为log.error、log.error,memory;两个接受者绑定的routing-key分别为log.#,log.*)
- 创建MQ连接工具类
public class RabbitmqUntil {
//获取连接
public static Connection getRabbitmqConnection() throws Exception{
//定义连接工厂
ConnectionFactory factory = new ConnectionFactory();
//设置服务地址
factory.setHost("127.0.0.1");
//设置端口(这里的端口号指定是AMQP协议所用的端口号)
factory.setPort(5672);
//设置数据库
factory.setVirtualHost("/test");
//设置用户名
factory.setUsername("test");
//设置密码
factory.setPassword("test");
return factory.newConnection();
}
}
- 发送者发布两条消息,routing-key分别为log.error、log.error.memory
public class TopicsSender {
final static String ROUTING_NAME="topics mode";
public static void sendMessage()throws Exception{
//1、获取连接
Connection rabbitmqConnection = RabbitmqUntil.getRabbitmqConnection();
//2、创建通道
Channel channel = rabbitmqConnection.createChannel();
//3、创建路由(参数1:路由名称;参数2:模式名称,direct表示路由模式,路由模式发布消息需要绑定routing-key)
channel.exchangeDeclare(ROUTING_NAME,"topic");
//4、设置每次发送信息不超过1条
channel.basicQos(1);
String message="hello ,I am error";
String routingKey ="log.error";
//5、发布消息(这里需要绑定routingKey)
channel.basicPublish(ROUTING_NAME,routingKey,null,message.getBytes());
message="hello ,I am error of out of memory";
routingKey ="log.error.out_of_memory";
channel.basicPublish(ROUTING_NAME,routingKey,null,message.getBytes());
}
public static void main(String[] args)throws Exception {
TopicsSender.sendMessage();
}
}
注:channel.exchangeDeclare(ROUTING_NAME,"topic"),direct表示主题模式
- 消费者1,接受routing-key为log.#的消息
public class TopicsReceiver1 {
//路由名称
public static final String ROUTING_NAME="topics mode";
//队列名称
public static final String ROUTING_QUEUE="topics queue1";
public static void receiveMessage() throws Exception{
//创建连接
Connection rabbitmqConnection = RabbitmqUntil.getRabbitmqConnection();
//获取通道
final Channel channel = rabbitmqConnection.createChannel();
//申明队列
channel.queueDeclare(ROUTING_QUEUE,true,false,false,null);
//申明每次处理一条数据
channel.basicQos(1);
//绑定路由(并且指定routing-key为error、warning)
channel.queueBind(ROUTING_QUEUE,ROUTING_NAME,"log.#");
DefaultConsumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
try{
System.out.println("主题模式消费者1:"+new String(body,"utf-8"));
}catch (Exception e){
e.printStackTrace();
}finally {
channel.basicAck(envelope.getDeliveryTag(),false);
}
}
};
channel.basicConsume(ROUTING_QUEUE,false,consumer);
}
public static void main(String[] args) throws Exception{
TopicsReceiver1.receiveMessage();
}
}
- 消费者2,接受routing-key为log.*的消息
public class TopicsReceiver2 {
//路由名称
public static final String ROUTING_NAME="topics mode";
//队列名称
public static final String ROUTING_QUEUE="topics queue2";
public static void receiveMessage() throws Exception{
//创建连接
Connection rabbitmqConnection = RabbitmqUntil.getRabbitmqConnection();
//获取通道
final Channel channel = rabbitmqConnection.createChannel();
//申明队列
channel.queueDeclare(ROUTING_QUEUE,true,false,false,null);
//申明每次处理一条数据
channel.basicQos(1);
//绑定路由(并且指定routing-key为error、warning)
channel.queueBind(ROUTING_QUEUE,ROUTING_NAME,"log.*");
DefaultConsumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
try{
System.out.println("主题模式消费者2:"+new String(body,"utf-8"));
}catch (Exception e){
e.printStackTrace();
}finally {
channel.basicAck(envelope.getDeliveryTag(),false);
}
}
};
channel.basicConsume(ROUTING_QUEUE,false,consumer);
}
public static void main(String[] args) throws Exception{
TopicsReceiver2.receiveMessage();
}
}
注:绑定多个routing-key,只要对队列进行多次绑定即可channel.queueBind()
- 执行上面三个类,可以看到消费者2只接收到‘hello ,I am error’这条消息,而消费者1两条都接收到