topic模式 RoutingKey 按照英文单词点号多拼接规则填充。其中消费者匹配规则时候 * 代表一个单词,#表示多个单词
消费者C1的RoutingKey 规则按照*.orange.*
匹配 绑定队列Q1
java
package com.esint.rabbitmq.work05;
import com.esint.rabbitmq.RabbitMQUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
public class ReceiveLogsTopic01 {
public static final String EXCHANGE_NAME = "topic";
public static void main(String[] args) throws Exception {
Channel channel = RabbitMQUtils.getChannel();
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
String queueName = "Q1";
channel.queueDeclare(queueName,false,false,false,null);
channel.queueBind(queueName,EXCHANGE_NAME,"*.orange.*");
System.out.println("Q1队列等待接受消息...");
DeliverCallback deliverCallback = (deliverTag,mes)->{
System.out.println("打印ReceiveLogsTopic01控制台消息:" + new String(mes.getBody(),"UTF-8"));
System.out.println("接收队列:" + queueName + " 绑定键:" + mes.getEnvelope().getRoutingKey());
};
channel.basicConsume(queueName,true,deliverCallback,deliverTag->{});
}
}
消费者C2的RoutingKey 规则按照*.rabbit.* 和 lazy.#
匹配 绑定队列Q2
java
package com.esint.rabbitmq.work05;
import com.esint.rabbitmq.RabbitMQUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
public class ReceiveLogsTopic02 {
public static final String EXCHANGE_NAME = "topic";
public static void main(String[] args) throws Exception {
Channel channel = RabbitMQUtils.getChannel();
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
String queueName = "Q2";
channel.queueDeclare(queueName,false,false,false,null);
channel.queueBind(queueName,EXCHANGE_NAME,"*.*.rabbit");
channel.queueBind(queueName,EXCHANGE_NAME,"lazy.#");
System.out.println("Q2队列等待接受消息...");
DeliverCallback deliverCallback = (deliverTag,mes)->{
System.out.println("打印ReceiveLogsTopic01控制台消息:" + new String(mes.getBody(),"UTF-8"));
System.out.println("接收队列:" + queueName + " 绑定键:" + mes.getEnvelope().getRoutingKey());
};
channel.basicConsume(queueName,true,deliverCallback,deliverTag->{});
}
}
生产者顺序发送
log
bindKeyMap.put("quick.orange.rabbit","Q1 Q2 catch【1】");
bindKeyMap.put("lazy.orange.elep","Q1 Q2 【2】");
bindKeyMap.put("quick.orange.fox","Q1【3】");
bindKeyMap.put("lazy.brown.fox","Q2【4】");
bindKeyMap.put("lazy.pink.rabbit","Q2 Q2【5】");
bindKeyMap.put("quick.brown.fox","wu【6】");
bindKeyMap.put("quick.orange.male.rabbit","wu【7】");
bindKeyMap.put("lazy.orange.male.rabbit","Q2【8】");
java
package com.esint.rabbitmq.work05;
import com.esint.rabbitmq.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import java.util.HashMap;
import java.util.Map;
public class ProductLogs {
public static final String EXCHANGE_NAME = "topic";
public static void main(String[] args) throws Exception {
Channel channel = RabbitMQUtils.getChannel();
HashMap<String,String > bindKeyMap = new HashMap<>();
bindKeyMap.put("quick.orange.rabbit","Q1 Q2 catch【1】");
bindKeyMap.put("lazy.orange.elep","Q1 Q2 【2】");
bindKeyMap.put("quick.orange.fox","Q1【3】");
bindKeyMap.put("lazy.brown.fox","Q2【4】");
bindKeyMap.put("lazy.pink.rabbit","Q2 Q2【5】");
bindKeyMap.put("quick.brown.fox","wu【6】");
bindKeyMap.put("quick.orange.male.rabbit","wu【7】");
bindKeyMap.put("lazy.orange.male.rabbit","Q2【8】");
for (Map.Entry<String, String> bindingKeyEntry : bindKeyMap.entrySet()) {
String key = bindingKeyEntry.getKey();
String value = bindingKeyEntry.getValue();
channel.basicPublish(EXCHANGE_NAME,key,null,value.getBytes("UTF-8"));
System.out.println("生产者发消息:"+ value);
}
}
}
运行消费者1 消费者2,执行生产者:
生产者:
消费者1:
消费者2: