如何处理JmsException /如何在ActiveMQ中设置redeliveryPolicy?

时间:2018-06-15 07:27:37

标签: jms activemq spring-jms jms-topic

当jms向消费者发送消息并且我们必须将其保存到db时,请看一个场景,但是当db关闭时,由于db server down,我们无法将其保存到db。所以如何确认jms到再次发送消息?

1 个答案:

答案 0 :(得分:0)

    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
        }
    }
}