在上手Activemq时遇到了奇怪的问题,消息会间隔性的出现搜不到的情况,因为偶尔出现一两次可能是网络不稳定的问题,一直出现就肯定是哪里出了问题
找了一些资料,因为ActiveMq有两种消息模型,一种是Queue(点对点模式),一种是Topic(发布订阅模式),简单描述下这两种模式的区别
- Queue: 点对点,一对一模式,该模式下的消息会确保被某一个消费者接收,就是说存在很多消费者的话,只有其中一个会接受到消息,如果当前没有消费者的话,消息会被保存下来,当有对应的消费者连接上时,就把该消息推送给这位新的消费者。
- Topic:订阅模式,这种模式的消息可以被多个消费者同时接收(如果有消费者的话),如果当时没有消费者,那么这条消息就会被丢弃,不会进行保存,后面即使有新的消费者,也接收不到该消息了
可以发现,上面描述的问题其实就是消息在点对点模式下的特征,所以要解决的话就只需要将消息的推送模式改为发布订阅模式就好了
消息生产者:
ActiveMQTopic topic = new ActiveMQTopic(destination);
jmsMessagingTemplate.convertAndSend(topic, data);
ActiveMQTopic类:订阅模式
ActiveMQQueue类:点对点模式
消息消费方:
需要在application.properties中配置消息接收模式(默认是Point)
spring.jms.pub-sub-domain=true