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 ");
    }
}


我从上面的subscription方法引发了异常,但无法通知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方法发布消息(如上所述)。

最佳答案

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应用程序,那么我猜监听器正在使用Boot的jmsListenerContainerFactory bean(这是默认的bean名称)。

10-04 19:26