Spring JMS : Set ErrorHandler for @JmsListener annotated method
Asked Answered
T

1

15

I am using a @JmsListener annotated method listen to JMS messages as shown below.

@JmsListener(destination="exampleQueue")  
public void fetch(@Payload String message){  
    process(message);  
}

When this method execution result in an exception, I got a warn log

Execution of JMS message listener failed, and no ErrorHandler has been set.

How do I set an ErrorHandler to handle the case. I am using spring boot 1.3.3.RELEASE

Torsion answered 17/11, 2016 at 12:12 Comment(2)
Answered here: #8923032Anemochore
Thanks @sarahTheButterFly. Got solution by referring Kuchi answer in the link provided. I have tuned the answer a bit to work in my case. I am posting my answer belowTorsion
T
22

While using annotations like @EnableJms, @JmsListener etc to work with Spring JMS, the ErrorHandler can be set like this

@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory, ExampleErrorHandler errorHandler) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setErrorHandler(errorHandler);
    return factory;
}

@Service
public class ExampleErrorHandler implements ErrorHandler{   
    @Override
    public void handleError(Throwable t) {
        //handle exception here
    }
}

More details are available here : Annotation-driven listener endpoints

Torsion answered 18/11, 2016 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.