0

Let see one scenario is there when jms send message to consumer and we have to save that to db,but when db is down we cant able to save that to db due to db server is down.So how to acknowledge jms to send message again?

1 Answers1

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