0

I'm fairly new to AMQ, I do understand the concept of making a message, sending it to a queue, and consuming it asynchrnously.

My main Spring configurations are the following

@Bean
    public DefaultMessageListenerContainer listenerContainer(
            MessageListenerAdapter adapter) {
        DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
        dmlc.setConnectionFactory(connectionFactory());
        dmlc.setMessageListener(adapter);
        dmlc.setDestinationName("myQueue");
        dmlc.setConcurrentConsumers(5);
        return dmlc;
    }

@Bean
MessageListenerAdapter adapter(MyClass myClass) {
    MessageListenerAdapter messageListener = new MessageListenerAdapter(
            myClass);
    messageListener.setDefaultListenerMethod("myMethod");
    return messageListener;
}   

@Bean
public JmsTemplate jmsTemplate() {
    JmsTemplate jt = new JmsTemplate(connectionFactory());
    return jt;
}

I have no trouble sending things to the queue with jmsTemplate.convertAndSend() and then recieving them with myClass#myMethod

My Problem: I need the listener to halt consuming messages if an exception is thrown. Usually the exception will tell me the remote site is offline and to try later. If I don't stop it all those messages are mostly lost. Would like to keep the queue full until I'm capable of sending again.

Is it possible?

javaNoober
  • 1,338
  • 2
  • 17
  • 43

1 Answers1

0

If you use simple JMS Transactions, you can avoid message loss on exception and not have to worry about stopping and starting your listener.

Here is a good summary on how to decide on the type of acknowledgment you need. Transactions require a bit more complex set up than the auto-ack you are using. You can find a good non-spring example of setting this up in the answer here.

Community
  • 1
  • 1
Erik Williams
  • 933
  • 6
  • 5
  • I just found out I was on auto-ack a bit ago. Tried Session.CLIENT_ACKNOWLEDGE which says would keep failed messages in queue, but it gets stuck on the same message until delivered instead, any thoughts? – javaNoober Oct 31 '14 at 19:37
  • You need to set a re delivery policy. http://activemq.apache.org/redelivery-policy.html CLIENT_ACKs are not good for all situations though as there are other exceptions that it will not be handled. This documentation has a good explanation of what each mode provides by default http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jms/listener/AbstractMessageListenerContainer.html – Erik Williams Oct 31 '14 at 19:51