1.開啟隊列持久化
只需要添加三行代碼
jmsTemplate.setDeliveryMode(2); jmsTemplate.setExplicitQosEnabled(true); jmsTemplate.setDeliveryPersistent(true);
2. 開啟主題持久化,啟動類添加如下配置
@Bean(name = "topicListenerFactory") public JmsListenerContainerFactory<DefaultMessageListenerContainer> topicListenerFactory(ConnectionFactory connectionFactory){ DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); factory.setSubscriptionDurable(true);// Set this to "true" to register a durable subscription, factory.setClientId("B"); factory.setConnectionFactory(connectionFactory); return factory; }
//消費者消費 destination隊列或者主題的名字
@JmsListener(destination = "boot_topic",containerFactory = "topicListenerFactory")
public void getMessage(TextMessage message,Session session) throws JMSException {
System.out.println("消費者獲取到消息:"+message.getText());
}
這里需要注意,主題的數據不會被消費,會被一直記錄下來,只能手動清除