石景山建设网站,wordpress怎么下载,简单商业网站模板,如何再网站上做免费广告词上一期我们讲了SpringBoot集成activeMQ实现Queue模式点对点通信#xff0c;这一期我们接着讲SpringBoot集成activeMQ实现Topic发布/订阅模式通信。发布/订阅模式通信是对点对点通信模式的扩展。Queue模式下一个人发送的消息只能由一个人接收#xff0c;而Topic模式下#xf…上一期我们讲了SpringBoot集成activeMQ实现Queue模式点对点通信这一期我们接着讲SpringBoot集成activeMQ实现Topic发布/订阅模式通信。 发布/订阅模式通信是对点对点通信模式的扩展。Queue模式下一个人发送的消息只能由一个人接收而Topic模式下A发送的消息可以被所有监听A的对象的接收即 ①一个消息可以被多个服务接收 ②订阅一个主题的消费者只能消费自它订阅之后发布的消息。 ③消费端如果在生产端发送消息之后启动是接收不到消息的除非生产端对消息进行了持久化(例如广播只有当时听到的人能听到信息)下面我们来看看代码中的实现①pom.xml和Queue模式下的配置相同②application.yml的配置较Queue模式下多配置了一个topic: wucy-topicspring: activemq: broker-url: tcp://127.0.0.1:61616 user: admin password: adminqueue: wucy-queuetopic: wucy-topicserver: port: 8080③关于queue的配置ActivemqConfig.java因为activeMQ默认不接受Topic消息所以需要配置topicListenerContainer来开启Configurationpublic class ActivemqConfig {Value(${queue})private String queue;Value(${topic})private String topic;Beanpublic Queue wucyQueue() {return new ActiveMQQueue(queue);}Beanpublic Topic wucyTopic() {return new ActiveMQTopic(topic);}/** * JmsListener注解默认只接收queue消息,如果要接收topic消息,需要设置containerFactory */Bean public JmsListenerContainerFactory topicListenerContainer(ConnectionFactory activeMQConnectionFactory) { DefaultJmsListenerContainerFactory topicListenerContainer new DefaultJmsListenerContainerFactory(); topicListenerContainer.setPubSubDomain(true); topicListenerContainer.setConnectionFactory(activeMQConnectionFactory); return topicListenerContainer; }}④创建生产者RequestMapping(/registerTopic)public String registerTopic(String name) {long startTime System.currentTimeMillis();// 数据库的操作try {Thread.sleep(50);// 为了提高用户体验// 发短信去调用别人的API// mqServer.send(发送短信*******);// Thread.sleep(1000);// 发邮件qq发邮件的smtpJSONObject json new JSONObject();json.put(type