java Spring JMS:为@JmsListener 注释方法设置 ErrorHandler

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40654586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:22:49  来源:igfitidea点击:

Spring JMS : Set ErrorHandler for @JmsListener annotated method

javaspringspring-bootjmsspring-jms

提问by faizi

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

我正在使用带@JmsListener注释的方法来监听 JMS 消息,如下所示。

@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 ErrorHandlerto handle the case. I am using spring boot 1.3.3.RELEASE

我如何设置一个ErrorHandler来处理这个案例。我正在使用弹簧靴 1.3.3.RELEASE

回答by faizi

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

虽然使用注释一样@EnableJms@JmsListener等来的工作与Spring JMS的的ErrorHandler可以设置这样的

@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

此处提供更多详细信息:注释驱动的侦听器端点