英文:
Spring JMS MappingJackson2MessageConverter - No way to handle errors
问题
我正在使用Spring JMS与ActiveMQ,并且正在使用MappingJackson2MessageConverter
在各个组件之间传递消息。
我的核心问题是,当MappingJackson2MessageConverter
在将String反序列化为某个对象时遇到内部反序列化错误时,这些异常没有以任何我可以响应的方式被引发出来。我的错误处理逻辑从未被调用过,我也无法访问任何异常。这些反序列化错误似乎被完全吞没了,尽管它们在控制台中被记录下来,但我没有任何机会根据它们执行自定义逻辑。
我如下所示定义了我的MappingJackson2MessageConverter
bean:
@Bean
public MessageConverter jacksonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("type");
return converter;
}
我如下所示定义了一个JMS监听器工厂:
@Bean
public JmsListenerContainerFactory<?> jmsListenerQueueFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer, JmsErrorHandler jmsErrorHandler) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setErrorHandler(jmsErrorHandler); // <-- 我希望这个会被使用
configurer.configure(factory, connectionFactory);
return factory;
}
在上面的代码中,setErrorHandler
调用是重要的。我定义了一个简单的 ErrorHandler
对象,试图捕获 @JmsListener
方法可能抛出的错误。这个简单的错误处理程序如下:
public class JmsErrorHandler implements ErrorHandler {
@Override
public void handleError(Throwable t) {
System.out.println("我们有错误!");
}
}
我的监听器也很简单,如下所示:
@JmsListener(id = MY_ID, destination = MY_DEST)
public void processMessage(MyPojoMessage myPojoMessage) {
// 这段代码显然永远不会被执行,因为发生了序列化错误。
}
总体来说,这是一个非常简单和基本的设置。现在,当我发送一个无效的消息,Jackson无法解析时,我在控制台中得到以下错误:
19-10-2020 09:05:33.933 [DefaultMessageListenerContainer-1] WARN o.s.j.l.DefaultMessageListenerContainer.invokeErrorHandler - Execution of JMS message listener failed, and no ErrorHandler has been set.
org.springframework.jms.listener.adapter.ListenerExecutionFailedException: Listener method 'public void com.example.processMessage(com.example.MyPojoMessage)' threw exception; nested exception is org.springframework.jms.support.converter.MessageConversionException: Failed to convert JSON message content; nested exception is com.fasterxml.jackson.databind.JsonMappingException
这个异常是预期的,因为我提供了一个Jackson无法序列化的无效载荷。然而,似乎没有任何方法可以配置代码以实际捕获这个异常,以便我可以对其进行响应。我在上面的代码中设置了错误处理程序。然而,日志抱怨没有设置ErrorHandler
,这似乎是错误的。
我看不到其他获取这个Jackson内部异常引用的方法。有没有任何办法?
英文:
I am using Spring JMS with ActiveMQ, and I am using the MappingJackson2MessageConverter
to pass along messages between various components.
My core problem is that when the MappingJackson2MessageConverter
experiences an internal deserialization error, when deserializing a String into some Object, these exceptions are not raised in any way that I can respond to them. My Error Handler logic never gets called, and no exceptions are thrown that I have access to. These deserialization errors appear to be completely swallowed and, while they are logged in the console, I am offered no chance to perform custom logic in response to them.
I define my MappingJackson2MessageConverter
bean as follows:
@Bean
public MessageConverter jacksonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
return converter;
}
I define a JMS Listener factory as follows:
@Bean
public JmsListenerContainerFactory<?> jmsListenerQueueFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer, JmsErrorHandler jmsErrorHandler) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setErrorHandler(jmsErrorHandler); // <-- I expect this to be used
configurer.configure(factory, connectionFactory);
return factory;
}
Of importance in the above line is the setErrorHandler
call. I have defined a simple ErrorHandler
object that is attempting to catch errors that my @JmsListener
methods may throw. This simple error handler looks like this:
public class JmsErrorHandler implements ErrorHandler {
@Override
public void handleError(Throwable t) {
System.out.println("WE HAVE THE ERROR!");
}
}
My listener is also simple, as follows:
@JmsListener(id = MY_ID, destination = MY_DEST)
public void processMessage(MyPojoMessage myPojoMessage) {
// This code is obviously never reached, since the serialization error occurs.
Overall, this is a very simple and basic setup. Now when I send an invalid message, that Jackson will fail to parse, I get the following errors in my console:
19-10-2020 09:05:33.933 [35m[DefaultMessageListenerContainer-1][0;39m [31mWARN [0;39m o.s.j.l.DefaultMessageListenerContainer.invokeErrorHandler - Execution of JMS message listener failed, and no ErrorHandler has been set.
org.springframework.jms.listener.adapter.ListenerExecutionFailedException: Listener method 'public void com.example.processMessage(com.example.MyPojoMessage)' threw exception; nested exception is org.springframework.jms.support.converter.MessageConversionException: Failed to convert JSON message content; nested exception is com.fasterxml.jackson.databind.JsonMappingException
This exception is expected - since I provide an invalid payload that Jackson cannot serialize. However, there is seemingly no way for me to configure the code to actually catch this exception in a way I can respond to it. I am setting the Error Handler in the above code. Yet the logs complain that no ErrorHandler has been set
which seems wrong.
I see no other ways to get a reference to this Jackson inner exception. Is there any way?
答案1
得分: 3
看起来 ConditionalOnMissingBean
自动配置已经开启,针对 bean name。
所以你应该有类似以下的代码:
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer, JmsErrorHandler jmsErrorHandler) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setErrorHandler(jmsErrorHandler); // <-- 我期望这部分会被使用
configurer.configure(factory, connectionFactory);
return factory;
}
英文:
Looks like ConditionalOnMissingBean autoconfiguration is on bean name
So you should have something like this.
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer, JmsErrorHandler jmsErrorHandler) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setErrorHandler(jmsErrorHandler); // <-- I expect this to be used
configurer.configure(factory, connectionFactory);
return factory;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论