spring jms全局ErrorHandler无法正常工作

时间:2018-04-16 16:06:41

标签: java spring jms spring-jms

MyProjectJmsListenerContainerFactory类:

@Configuration
public class MyProjectJmsListenerContainerFactory {

    @Bean
    public JmsListenerContainerFactory<?> myFactory(
            ConnectionFactory connectionFactory,
            DefaultJmsListenerContainerFactoryConfigurer configurer, 
            MyProjectJmsGlobalErrorHandler errorHandler) {
        ActiveMQConnectionFactory activeMQConnectionFactory =
                       (ActiveMQConnectionFactory)connectionFactory;
        RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
        redeliveryPolicy.setMaximumRedeliveries(1);
        activeMQConnectionFactory.setRedeliveryPolicy(redeliveryPolicy);

        DefaultJmsListenerContainerFactory factory = 
                 new DefaultJmsListenerContainerFactory();
        factory.setErrorHandler(errorHandler);
        configurer.configure(factory, activeMQConnectionFactory);
        return factory;
    }
}

MyProjectJmsGlobalErrorHandler类:

@Component
public class MyProjectJmsGlobalErrorHandler implements ErrorHandler {

    @Override
    public void handleError(Throwable throwable) {
        System.out.println("JMS EXCEPTION HANDLER CAUGHT THE EXCEPTION !!");       
    }
}

JMSTextMessageConverter类:

@Configuration
public class JMSTextMessageConverter {

    @Bean
    public MessageConverter jacksonJmsMessageConverter() {
        MappingJackson2MessageConverter mappingJackson2MessageConverter = 
                     new MappingJackson2MessageConverter();
        mappingJackson2MessageConverter.setTargetType(MessageType.TEXT);
        mappingJackson2MessageConverter.setTypeIdPropertyName("_type");

        return mappingJackson2MessageConverter;
    }   
}

JMSMessagePublisher类:

@Component
public class JMSMessagePublisher {

    private JmsTemplate jmsTemplate;

    private MessageConverter messageConverter;

    @Autowired
    public JMSMessagePublisher(JmsTemplate jmsTemplate,    
                           MessageConverter messageConverter) {
        this.jmsTemplate = jmsTemplate;
        this.messageConverter = messageConverter;
    }

    public void publishMessage() {
        jmsTemplate.setMessageConverter(messageConverter);
        jmsTemplate.convertAndSend("test.q1", "Hello World");
    }
}

JMSListener类:

@Component
public class JMSListener {

    @JmsListener(destination = "test.q1")
    public void subscribe(String message) throws Exception {
        System.out.println(" message received ::::::::"+message);
        throw new Exception("Exception from message listener ");
    }
}

我从上面的 subscribe 方法抛出异常,但无法收到 ErrorHandler 的通知,我可以在日志中注意到以下警告。

WARN 1993 --- [enerContainer-1] o.s.j.l.DefaultMessageListenerContainer  : 
Execution of JMS message listener failed, and no ErrorHandler has been set.

我的JMS配置有什么问题,即如何设置ErrorHandler以便在handleError(Throwable throwable)方法中全局捕获/通知所有异常。

我使用publishMessage()类中的JMSMessagePublisher方法发布消息(上面给出)。

1 个答案:

答案 0 :(得分:1)

ErrorHandler仅用于处理侦听器在处理消息时抛出的错误(异常)。

由于没有连接,因此没有消息可以处理错误。

您可以编写自己的逻辑,以尝试从连接工厂不时获得连接。

修改

我刚刚进行了测试,它对我来说很好......

@SpringBootApplication
public class So49861714Application {

    public static void main(String[] args) {
        SpringApplication.run(So49861714Application.class, args);
    }

    @Bean
    public ApplicationRunner runner(JmsTemplate template) {
        return args -> template.convertAndSend("foo", "testMessage");
    }

    @Bean
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(
            DefaultJmsListenerContainerFactoryConfigurer configurer,
            ConnectionFactory connectionFactory,
            ErrorHandler myErrorHandler) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        factory.setErrorHandler(myErrorHandler);
        return factory;
    }

    @Bean
    public ErrorHandler myErrorHandler() {
        return t -> {
            System.out.println("In error handler");
            t.printStackTrace();
        };
    }

    @JmsListener(destination = "foo")
    public void listen(String in) {
        System.out.println(in);
        throw new RuntimeException("test");
    }

}

testMessage
In error handler
org.springframework.jms.listener.adapter.ListenerExecutionFailedException: Listener method 'public void com.example.So49861714Application.listen(java.lang.String)' threw exception; nested exception is java.lang.RuntimeException: test
...
Caused by: java.lang.RuntimeException: test

我刚注意到您的工厂bean名为 myFactory,但您未在@JmsListener 中指定它。 如果您的应用程序是Spring Boot应用程序,我猜测该侦听器正在使用引导的jmsListenerContainerFactory bean(这是默认的bean名称)。