我异步地听了一个OpenMQ队列。如果我在使用消息时遇到异常,是否有办法让OpenMQ将消息再次发送给我?
@Bean
public JmsListenerContainerFactory jmsQueueListenerContainerFactory() {
DefaultJmsListenerContainerFactory jmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
jmsListenerContainerFactory.setConnectionFactory(connectionFactory());
jmsListenerContainerFactory.setPubSubDomain(false);
jmsListenerContainerFactory.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
return jmsListenerContainerFactory;
}
MessageConsumer receiver = session.createConsumer(destination);
receiver.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
TextMessage text = (TextMessage) message;
System.out.println("Received message: " + message.getText());
//The connection timed out when saving the message to the database
repository.save(text);
}
});
未取消备份的原因是您已确认模式设置为AUTO_ACKNOWLEDGE
。
jmsListenerContainerFactory.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
将其更改为CLIENT_ACKNOWLEDGE
,如下所示:
jmsListenerContainerFactory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
使用message.acknowledge()
提交消息。
使用session.recover()
撤消该消息。