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

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

当此方法执行导致异常时,我收到警告日志
Execution of JMS message listener failed, and no ErrorHandler has been set.

如何设置ErrorHandler来处理这种情况。我正在使用Spring Boot 1.3.3.RELEASE

最佳答案

当使用@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
    }
}

可在此处获取更多详细信息:Annotation-driven listener endpoints

09-25 21:02