我有一个sqslistener,它使用一个定制的queuemessagehandlerfactory和一个从json到pojo再到pojo的消息转换器。如果侦听器方法由于任何原因无法解析消息中的json主体,它将抛出org.springframework.messaging.converter.messageconversionexception。我要捕获此异常并正确记录它。我知道jms监听器允许您添加一个定制的errorhandler作为工厂定义的一部分。
有没有类似的方法来定义一个自定义的errorhandler来使用spring的sqslistener捕获这个异常并记录它?
谢谢!
@Bean
public QueueMessageHandlerFactory queueMessageHandlerFactory(MessageConverter messageConverter) {
QueueMessageHandlerFactory factory = new QueueMessageHandlerFactory();
AcknowledgmentHandlerMethodArgumentResolver acknowledgmentResolver =
new AcknowledgmentHandlerMethodArgumentResolver(
ACKNOWLEDGMENT_HEADER_NAME);
PayloadArgumentResolver payloadArgumentResolver = new PayloadArgumentResolver(messageConverter);
factory.setArgumentResolvers(Arrays.asList(acknowledgmentResolver, payloadArgumentResolver));
return factory;
}
@Bean
protected MessageConverter messageConverter(ObjectMapper objectMapper) {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setObjectMapper(objectMapper);
converter.setStrictContentTypeMatch(false);
return converter;
}
侦听器:
@SqsListener(value = "${aws.sqs.queueName}", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
public void receiveMessage(MyCustomPOJO pollo, Acknowledgment acknowledgment) {
LOG.info("Message Received using SQS Listener " + pollo);
try {
acknowledgment.acknowledge().get();
} catch (InterruptedException | ExecutionException e) {
LOG.error("Error acknowledging message: " + pollo);
}
}
由发送“test message”而不是反序列化的pojo触发的异常。
org.springframework.messaging.converter.MessageConversionException: Could not read JSON: Unrecognized token 'Test': was expecting ('true', 'false' or 'null')
at [Source: Test Message!; line: 1, column: 5]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Test': was expecting ('true', 'false' or 'null')
at [Source: Test Message!; line: 1, column: 5]
at org.springframework.messaging.converter.MappingJackson2MessageConverter.convertFromInternal(MappingJackson2MessageConverter.java:224) ~[spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.messaging.converter.AbstractMessageConverter.fromMessage(AbstractMessageConverter.java:175) ~[spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver.resolveArgument(PayloadArgumentResolver.java:135) ~[spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:112) ~[spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:135) ~[spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:107) ~[spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMatch(AbstractMethodMessageHandler.java:502) [spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessageInternal(AbstractMethodMessageHandler.java:461) [spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessage(AbstractMethodMessageHandler.java:399) [spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer.executeMessage(SimpleMessageListenerContainer.java:228) [spring-cloud-aws-messaging-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer$MessageExecutor.run(SimpleMessageListenerContainer.java:418) [spring-cloud-aws-messaging-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer$SignalExecutingRunnable.run(SimpleMessageListenerContainer.java:310) [spring-cloud-aws-messaging-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_251]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_251]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_251]
Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Test': was expecting ('true', 'false' or 'null')
at [Source: Test Message!; line: 1, column: 5]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1702) ~[jackson-core-2.8.7.jar:2.8.7]
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:558) ~[jackson-core-2.8.7.jar:2.8.7]
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2839) ~[jackson-core-2.8.7.jar:2.8.7]
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleOddValue(ReaderBasedJsonParser.java:1903) ~[jackson-core-2.8.7.jar:2.8.7]
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:749) ~[jackson-core-2.8.7.jar:2.8.7]
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3834) ~[jackson-databind-2.8.7.jar:2.8.7]
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3783) ~[jackson-databind-2.8.7.jar:2.8.7]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2880) ~[jackson-databind-2.8.7.jar:2.8.7]
at org.springframework.messaging.converter.MappingJackson2MessageConverter.convertFromInternal(MappingJackson2MessageConverter.java:219) ~[spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
... 14 more
1条答案
按热度按时间7fyelxc51#
无需担心,我需要做的似乎只是在我的自定义处理程序中添加一个@messageexceptionhandler注解: