广播消息:
一个点发送,所有有监听订阅的程序都能收到消息。
应用场景:一个配置更新了,其他点都需要知道配置更新需加载。
mq创建
主要是创建组时与队列有点区别
mqadmin updateSubGroup -c rocketmq-cluster -d true -g broad-string-consumer -n "192.168.3.252:9876;192.168.3.253:9876" mqadmin updateSubGroup -c rocketmq-cluster -d true -g broad-string-consumer -n "192.168.3.252:9876;192.168.3.253:9876"
mqadmin updateTopic -c rocketmq-cluster -t TOPIC-SYS-ACC-BIZ-BROADCAST
mqadmin updateSubGroup -c rocketmq-cluster -d true -g GID-ACC-BIZBROADCAST
生产者
产生消息与产生普通消息一样
@Component public class RocketTest extends BaseTest{ @Autowired private AccBizSender accBizSender; @Test public void accBizSenderTest() throws Exception { for (int i=0;i<10;i++) { BaseMqRequest baseMqRequest = new BaseMqRequest(); baseMqRequest.setBizType("sendTest"); baseMqRequest.setData("{i="+i+"}"); accBizSender.sendMessageBroadcast(baseMqRequest); } MyDateUtil.sleep(1000000); } }
消费者
消息模式必须指定
messageModel = MessageModel.BROADCASTING
import org.apache.rocketmq.spring.annotation.MessageModel; import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; import org.springframework.stereotype.Component; import com.ccjr.commons.constants.RocketMqConstants; import lombok.extern.slf4j.Slf4j; @Slf4j @Component @RocketMQMessageListener(topic = RocketMqConstants.TOPIC_SYS_ACC_BIZ_BROADCAST, consumerGroup = RocketMqConstants.G_TAG_ACC_BIZBROADCAST, selectorExpression = RocketMqConstants.TAG_ACC_BIZBROADCAST, messageModel = MessageModel.BROADCASTING) public class AccBizListener extends BaseListener { @Override public void onMessageExec(String boContext) { super.initMqMsg(RocketMqConstants.TOPIC_SYS_ACC_BIZ_BROADCAST, RocketMqConstants.G_TAG_ACC_BIZBROADCAST, RocketMqConstants.TAG_ACC_BIZBROADCAST); log.info("接收广播消息ACC::"+boContext); } }
测试结果:
系统A: 2022-11-14 11:21:10.343 INFO 2740 [ti:4af8db59ad8a] -- [MessageThread_4] c.ccjr.cus.mq.listerner.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=3}"} 2022-11-14 11:21:10.343 INFO 2740 [ti:14daff420a85] -- [MessageThread_1] c.ccjr.cus.mq.listerner.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=0}"} 2022-11-14 11:21:10.343 INFO 2740 [ti:59c55d117568] -- [essageThread_10] c.ccjr.cus.mq.listerner.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=9}"} 2022-11-14 11:21:10.343 INFO 2740 [ti:cd8331a33522] -- [MessageThread_7] c.ccjr.cus.mq.listerner.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=6}"} 2022-11-14 11:21:10.343 INFO 2740 [ti:c06c620aed6f] -- [MessageThread_2] c.ccjr.cus.mq.listerner.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=1}"} 2022-11-14 11:21:10.343 INFO 2740 [ti:8649c25fb46b] -- [MessageThread_5] c.ccjr.cus.mq.listerner.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=4}"} 2022-11-14 11:21:10.343 INFO 2740 [ti:77cfbf7db01a] -- [MessageThread_3] c.ccjr.cus.mq.listerner.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=2}"} 2022-11-14 11:21:10.343 INFO 2740 [ti:28c5eee7b43c] -- [MessageThread_8] c.ccjr.cus.mq.listerner.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=7}"} 2022-11-14 11:21:10.343 INFO 2740 [ti:9e346a603f5c] -- [MessageThread_9] c.ccjr.cus.mq.listerner.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=8}"} 2022-11-14 11:21:10.343 INFO 2740 [ti:67c235b561e4] -- [MessageThread_6] c.ccjr.cus.mq.listerner.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=5}"} 系统B: 2022-11-14 11:21:10.346 INFO 4936 [ti:43e2a62170b5] -- [MessageThread_1] c.c.acc.biz.mq.listener.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=1}"} 2022-11-14 11:21:10.346 INFO 4936 [ti:8e5713c22a14] -- [MessageThread_7] c.c.acc.biz.mq.listener.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=6}"} 2022-11-14 11:21:10.346 INFO 4936 [ti:9354a92f3f36] -- [MessageThread_9] c.c.acc.biz.mq.listener.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=8}"} 2022-11-14 11:21:10.346 INFO 4936 [ti:f1b44a0ba79c] -- [MessageThread_5] c.c.acc.biz.mq.listener.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=4}"} 2022-11-14 11:21:10.346 INFO 4936 [ti:09acbaf4b249] -- [MessageThread_6] c.c.acc.biz.mq.listener.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=5}"} 2022-11-14 11:21:10.346 INFO 4936 [ti:00d1ed751683] -- [MessageThread_4] c.c.acc.biz.mq.listener.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=3}"} 2022-11-14 11:21:10.346 INFO 4936 [ti:4f378ca6e336] -- [essageThread_10] c.c.acc.biz.mq.listener.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=9}"} 2022-11-14 11:21:10.346 INFO 4936 [ti:ae09aaa74d79] -- [MessageThread_3] c.c.acc.biz.mq.listener.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=2}"} 2022-11-14 11:21:10.346 INFO 4936 [ti:8102a4dc4d15] -- [MessageThread_2] c.c.acc.biz.mq.listener.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=0}"} 2022-11-14 11:21:10.348 INFO 4936 [ti:2fe1bf33a5b6] -- [MessageThread_8] c.c.acc.biz.mq.listener.AccBizListener : 接收广播消息ACC::{"bizType":"sendTest","data":"{i=7}"}
标签:11,ACC,--,mq,广播,springboot2,ti,AccBizListener,rocketMq From: https://www.cnblogs.com/a393060727/p/16889024.html