Spring JMS : Set ErrorHandler for @JmsListener annotated method

家住魔仙堡 提交于 2020-05-26 11:27:45

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/40654586/spring-jms-set-errorhandler-for-jmslistener-annotated-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!