3.3.2 消息生产者/** * Created On : 2/11/2022. * <p> * Author : huayu * <p> * Description: RabbitMQ 主题模式消息生产者 */@Slf4j@Componentpublic class RabbitMQTopicProducer {@Autowiredprivate RabbitTemplate rabbitTemplate;/*** @author : huayu* @date: 1/11/2022* @param: [topicExchange, topicRoutingKey, topicMsg]* @return : void* @description : 使用主题模式,发送消息到主题交换机,主题交换机会根据发送消息的路由键,根据匹配规则将消息投递到匹配的队列中*/public void sendTopicMsg2TopicExchange(String topicExchange,String topicRoutingKey,String topicMsg){log.info("++++++direct模式消息生产者,发送直连消息:{},到交换机:{},路由键:{} ++++++",topicMsg,topicExchange,topicRoutingKey);rabbitTemplate.convertAndSend(topicExchange,topicRoutingKey,topicMsg);}}3.3.3 消费者3.3.3.1 消费者One/** * Created On : 2/11/2022. * <p> * Author : huayu * <p> * Description: RabbitMQTopicConsumerOne */@Slf4j@Component@RabbitListener(queues = RabbitMQConstant.RABBITMQ_TOPIC_QUEUE_NAME_KH96_ONE)public class RabbitMQTopicConsumerOne {@RabbitHandlerpublic void consumeTopicMsgFromTopicQueue(String topicMapJson){log.info("****** Topic 主题模式,消费One,消费队列One,消息:{}******",topicMapJson);// TODO 核心业务逻辑处理}}3.3.3.2 消费者Two/** * Created On : 2/11/2022. * <p> * Author : huayu * <p> * Description: RabbitMQTopicConsumerTwo */@Slf4j@Component@RabbitListener(queues = RabbitMQConstant.RABBITMQ_TOPIC_QUEUE_NAME_KH96_TWO)public class RabbitMQTopicConsumerTwo {@RabbitHandlerpublic void consumeTopicMsgFromTopicQueue(String topicMapJson){log.info("****** Topic 主题模式,消费 Two,消费队列 Two,消息:{}******",topicMapJson);// TODO 核心业务逻辑处理}}3.3.3.3 消费者Three/** * Created On : 2/11/2022. * <p> * Author : huayu * <p> * Description: RabbitMQTopicConsumerThree */@Slf4j@Component@RabbitListener(queues = RabbitMQConstant.RABBITMQ_TOPIC_QUEUE_NAME_KH96_THREE)public class RabbitMQTopicConsumerThree {@RabbitHandlerpublic void consumeTopicMsgFromTopicQueue(String topicMapJson){log.info("****** Topic 主题模式,消费 Three,消费队列 Three,消息:{}******",topicMapJson);// TODO 核心业务逻辑处理}}3.3.4 请求测试方法/** * Created On : 1/11/2022. * <p> * Author : huayu * <p> * Description: 测试 RabbitMQ 消息队列的操作入口 */@Slf4j@RestControllerpublic class RabbitMQController {@Autowiredprivate RabbitMQTopicProducer rabbitMQTopicProducer;@GetMapping("/topic")public RequestResult<String> testRabbitMQTopic(@RequestParam String topicMsg){log.info("------- topic 主题模式,发送消息 -------");//模拟发送5条直连消息Stream.of(95,96,97,98,99).forEach(directNo ->{//模拟创建消息对象Map<String,Object> fanoutMap =new HashMap<>();fanoutMap.put("directNo",directNo);fanoutMap.put("directData",topicMsg);fanoutMap.put("directTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));//调用主题模式消息生产者,发送消息//场景1:使用唯一路由键 rabbitmq_topic_routing_key_kh96.only , 发送消息rabbitMQTopicProducer.sendTopicMsg2TopicExchange(RabbitMQConstant.RABBITMQ_TOPIC_EXCHANGE_KH96,RabbitMQConstant.RABBITMQ_TOPIC_ROUTING_KEY_KH96_ONLY,JSON.toJSONString(fanoutMap));//场景2:使用单词匹配路由键 rabbitmq_topic_routing_key_kh96.*,发送消息//rabbitMQTopicProducer.sendTopicMsg2TopicExchange(RabbitMQConstant.RABBITMQ_TOPIC_EXCHANGE_KH96//,"rabbitmq_topic_routing_key_kh96.abc"//,JSON.toJSONString(fanoutMap));//场景3:0 或多词匹配 rabbitmq_topic_routing_key_kh96.#,发送消息//rabbitMQTopicProducer.sendTopicMsg2TopicExchange(RabbitMQConstant.RABBITMQ_TOPIC_EXCHANGE_KH96//,"rabbitmq_topic_routing_key_kh96.abc.def"//,JSON.toJSONString(fanoutMap));});return ResultBuildUtil.success("使用主题模式 。发送消息成功");}}3.3.5 请求测试3.3.5.1 场景1:使用唯一路由键
经验总结扩展阅读
- 六盘水有什么特色美食 贵州六盘水最有名的七大美食
- 浙江丽水最有名的六大名菜
- 河南菜有哪些特色菜 河南最有名的六大特色名菜
- 北京菜有哪些特色菜 北京最有名的六大特色名菜
- 陕西菜有哪些特色菜 陕西最有名的六大特色名菜
- 2023年1月27日入宅好不好 2023年农历正月初六入宅吉日
- 2023年农历正月初六珠宝开光吉日 2023年1月27日珠宝开光黄道吉日
- 2023年1月27日祭祀吉日一览表 2023年农历正月初六祭祀吉日
- 2023年1月27日拜神行吗 2023年农历正月初六宜拜神吗
- 2023年1月27日请佛好吗 2023年农历正月初六请佛吉日
