首页 > 解决方案 > 如何处理 JmsException /如何在 ActiveMQ 中设置 redeliveryPolicy?

问题描述

让我们看看当 jms 向消费者发送消息时存在一种情况,我们必须将其保存到 db,但是当 db 关闭时,由于 db 服务器关闭,我们无法将其保存到 db。那么如何确认 jms 再次发送消息?

标签: jmsactivemqspring-jmsjms-topic

解决方案


    This issue raised by many people.I resolved this using below code snippet.


        @Configuration
    public class AppConfiguration {
        @Bean
        public JmsListenerContainerFactory<?> jmsContainerFactory(DefaultJmsListenerContainerFactoryConfigurer configurer) {
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
            connectionFactory.setClientID(clientId);
            connectionFactory.setBrokerURL(brokerUrl);
            CachingConnectionFactory cf = new CachingConnectionFactory(connectionFactory);
            DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
            factory.setConnectionFactory(cf);
            factory.setSubscriptionDurable(true);
            configurer.configure(factory, cf);
            return factory;
        }
    }



    @Component("ApprovalSubsCriber")
public class ApprovalSubscriber implements SessionAwareMessageListener<TextMessage> {
@Override
    @JmsListener(destination = "topic/jmsReplyTest1", containerFactory = "jmsContainerFactory", subscription = "ApprovalSubsCriber")
    public void onMessage(TextMessage message, Session session) throws JMSException {
        // This is the received message
        System.out.println("Receive: " + message.getText());
        // Let's prepare a reply message - a "ACK" String
        ActiveMQTextMessage textMessage = new ActiveMQTextMessage();
        textMessage.setText("ACK");
        System.out.println(session.getAcknowledgeMode());
        if ("notexception".equals(message.getText())) {
            session.commit();
        } else {
            session.rollback();//If exception comes
        }
    }
}

推荐阅读