我已经在我的SimpleRabbitListenerContainerFactory中注册了错误处理程序,但是在发布致命消息时未调用该处理程序。 代码
@Component
@Configuration
public class ListenerContainerFactory {
static final Logger logger = LoggerFactory.getLogger(ListenerContainerFactory.class);
@Autowired
RabbitMqConfig rabbitMqConfig;
@Autowired
EPPQ2Subscriber receiver;
@Autowired
EPPQ2ChanelAwareSubscriber receiverChanel;
public ListenerContainerFactory(ConfigurableApplicationContext ctx) {
printContainerStartMsg();
}
private void printContainerStartMsg() {
logger.info("----------- Scrubber Container Starts --------------");
}
@Bean
public SimpleRabbitListenerContainerFactory queueListenerContainer(AbstractConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
connectionFactory.setAddresses(rabbitMqConfig.getSubscriberHosts());
connectionFactory.setVirtualHost("hydra.services");
connectionFactory.setPort(rabbitMqConfig.getSubscriberPort());
connectionFactory.setUsername(rabbitMqConfig.getSubscriberUsername());
connectionFactory.setPassword(rabbitMqConfig.getSubscriberPassword());
SimpleRabbitListenerContainerFactory factory = new `SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setErrorHandler(errorHandler());
return factory;
}
@Bean
MessageListenerAdapter listenerAdapter(EPPQ2Subscriber receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
/*@Bean
MessageListenerAdapter listenerAdapterWithChanel(EPPQ2ChanelAwareSubscriber receiverChanel) {
return new MessageListenerAdapter(receiverChanel);
}*/
@Bean
public ErrorHandler errorHandler() {
return new ConditionalRejectingErrorHandler(fatalExceptionStrategy());
}
@Bean
public ScrubberFatalExceptionStrategy fatalExceptionStrategy() {
return new ScrubberFatalExceptionStrategy();
}
}
@Component
@Configuration
public class EPPQ2ListenerConfigurer implements RabbitListenerConfigurer{
public EPPQ2ListenerConfigurer(ConfigurableApplicationContext ctx) {
// TODO Auto-generated constructor stub
}
@Override
public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
}
@Bean
MessageHandlerMethodFactory messageHandlerMethodFactory() {
DefaultMessageHandlerMethodFactory messageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory();
messageHandlerMethodFactory.setMessageConverter(consumerJackson2MessageConverter());
return messageHandlerMethodFactory;
}
@Bean
public MappingJackson2MessageConverter consumerJackson2MessageConverter() {
return new MappingJackson2MessageConverter();
}
}
@Component
public class ScrubberFatalExceptionStrategy extends ConditionalRejectingErrorHandler.DefaultExceptionStrategy {
private static final Logger LOGGER = LoggerFactory.getLogger(ScrubberFatalExceptionStrategy.class);
@Override
public boolean isFatal(Throwable t) {
if (t instanceof ListenerExecutionFailedException) {
ListenerExecutionFailedException lefe =
(ListenerExecutionFailedException) t;
logger.error("Failed to process inbound message from queue "
+ lefe.getFailedMessage().getMessageProperties().getConsumerQueue()
+ "; failed message: " + lefe.getFaile
at
dMessage(), t);
}
return super.isFatal(t);
}
}
当我发送无效的json消息时,我希望我的处理程序被调用,但是未调用处理程序。
日志消息:
com.fasterxml.jackson.core.base.ParserMinimalBase._reportInvalidEOF(ParserMinimalBase.java:618)
at com.fasterxml.jackson.core.base.ParserBase._handleEOF(ParserBase.java:485)
at com.fasterxml.jackson.core.base.ParserBase._eofAsNextChar(ParserBase.java:497)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._skipWSOrEnd(UTF8StreamJsonParser.java:2933)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextFieldName(UTF8StreamJsonParser.java:964)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:512)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:364)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:29)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:369)
... 23 common frames omitted
[30m
有人可以指导我如何实现错误处理程序。
编辑-2更多堆栈跟踪。
[30m2019-03-25 08:41:23,159[0;39m [39mDEBUG[0;39m [[34mSimpleAsyncTaskExecutor-5[0;39m] [33morg.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter[0;39m: Processing [GenericMessage [payload=byte[1918], headers={amqp_receivedDeliveryMode=NON_PERSISTENT, amqp_receivedRoutingKey=hydra.Syphon.q1, amqp_deliveryTag=1, amqp_consumerQueue=hydra.Syphon.q1, amqp_redelivered=false, id=74e9f222-4ce9-a854-0f2a-35feac99bfca, amqp_consumerTag=amq.ctag-pu7gaBgwJDeCzJMKKOZg3Q, timestamp=1553521283156}]]
[30m2019-03-25 08:41:23,305[0;39m [39mDEBUG[0;39m [[34mSimpleAsyncTaskExecutor-5[0;39m] [33morg.springframework.messaging.handler.invocation.InvocableHandlerMethod[0;39m: Could not resolve parameter [0] in public void com.discover.dftp.scrubber.subscriber.EPPQ2Subscriber.receiveMessage(com.discover.dftp.scrubber.domain.Message,com.rabbitmq.client.Channel,java.lang.String,org.springframework.amqp.core.MessageProperties): Could not read JSON: Unexpected end-of-input: expected close marker for Object (start marker at [Source: (byte[])"{
"HEADER": {
"RETRY_COUNT":0,
"PUBLISH_EVENT_TYPE":"AUTH"
},
"PAYLOAD":{
"MTI": "100",
"MTI_REQUEST": "100",
"MERCHANT_TYPE": "5311",
"[truncated 1418 bytes]; line: 6, column: 13])
at [Source: (byte[])"{
"HEADER": {
"RETRY_COUNT":0,
"PUBLISH_EVENT_TYPE":"AUTH"
},
"PAYLOAD":{
"MTI": "100",
"MTI_REQUEST": "100",
"[truncated 1418 bytes]; line: 55, column: 24] (through reference chain: com.discover.dftp.scrubber.domain.Message["PAYLOAD"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected end-of-input: expected close marker for Object (start marker at [Source: (byte[])"{
"HEADER": {
"RETRY_COUNT":0,
"PUBLISH_EVENT_TYPE":"AUTH"
},
"PAYLOAD":{
"MTI": "100",
"MTI_REQUEST": "100",
"PAN": "6011000000000000",
"PROCCODE": "00",
"PROCCODE_REQUEST": "00",
"FROM_ACCOUNT": "00",
"TO_ACCOUNT": "00",
"TRANSACTION_AMOUNT": "000000000100",
"TRANSMISSION_MMDDHHMMSS": "0518202930",
"STAN": "000001",
"LOCALTIME_HHMMSS": "010054",
"LOCALDATE_YYMMDD": "180522",
"EXPIRATION_DATE_YYMM": "2302",
"MERCHANT_TYPE": "5311",
"[truncated 1418 bytes]; line: 6, column: 13])
at [Source: (byte[])"{
"HEADER": {
"RETRY_COUNT":0,
"PUBLISH_EVENT_TYPE":"AUTH"
},
"PAYLOAD":{
"MTI": "100",
"MTI_REQUEST": "100",
"MERCHANT_TYPE": "5311",
"[truncated 1418 bytes]; line: 55, column: 24] (through reference chain: com.discover.dftp.scrubber.domain.Message["PAYLOAD"])
[30m2019-03-25 08:41:23,309[0;39m [31mWARN [0;39m [[34mSimpleAsyncTaskExecutor-5[0;39m] [33morg.springframework.amqp.rabbit.listener.ConditionalRejectingErrorHandler[0;39m: Execution of Rabbit message listener failed.
org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener method could not be invoked with the incoming message
Endpoint handler details:
Method [public void com.discover.dftp.scrubber.subscriber.EPPQ2Subscriber.receiveMessage(com.discover.dftp.scrubber.domain.Message,com.rabbitmq.client.Channel,java.lang.String,org.springframework.amqp.core.MessageProperties)]
Bean [com.discover.dftp.scrubber.subscriber.EPPQ2Subscriber@56994b]
at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:193)
at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:127)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1547)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1473)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1461)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:1456)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1405)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:870)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:854)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1600(SimpleMessageListenerContainer.java:78)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.mainLoop(SimpleMessageListenerContainer.java:1137)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1043)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.messaging.converter.MessageConversionException: Could not read JSON: Unexpected end-of-input: expected close marker for Object (start marker at [Source: (byte[])"{
"HEADER": {
"RETRY_COUNT":0,
"PUBLISH_EVENT_TYPE":"AUTH"
},
"PAYLOAD":{
"MTI": "100",
"MTI_REQUEST": "100",
truncated 1418 bytes]; line: 55, column: 24] (through reference chain: com.discover.dftp.scrubber.domain.Message["PAYLOAD"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected end-of-input: expected close marker for Object (start marker at [Source: (byte[])"{
"HEADER": {
"RETRY_COUNT":0,
"PUBLISH_EVENT_TYPE":"AUTH"
},
"PAYLOAD":{
"MTI": "100",
"MTI_REQUEST": "100",
"[truncated 1418 bytes]; line: 6, column: 13])
at [Source: (byte[])"{
"HEADER": {
"RETRY_COUNT":0,
"PUBLISH_EVENT_TYPE":"AUTH"
},
"PAYLOAD":{
"MTI": "100",
"MTI_REQUEST": "100",
truncated 1418 bytes]; line: 55, column: 24] (through reference chain: com.discover.dftp.scrubber.domain.Message["PAYLOAD"])
at org.springframework.messaging.converter.MappingJackson2MessageConverter.convertFromInternal(MappingJackson2MessageConverter.java:234)
at org.springframework.messaging.converter.AbstractMessageConverter.fromMessage(AbstractMessageConverter.java:181)
at org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver.resolveArgument(PayloadArgumentResolver.java:137)
at org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:117)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:147)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:116)
at org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:49)
at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:190)
... 12 common frames omitted
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Unexpected end-of-input: expected close marker for Object (start marker at [Source: (byte[])"{
"HEADER": {
"RETRY_COUNT":0,
"PUBLISH_EVENT_TYPE":"AUTH"
},
"PAYLOAD":{
"MTI": "100",
"MTI_REQUEST": "100",
"[truncated 1418 bytes]; line: 6, column: 13])
at [Source: (byte[])"{
"HEADER": {
"RETRY_COUNT":0,
"PUBLISH_EVENT_TYPE":"AUTH"
},
"PAYLOAD":{
"MTI": "100",
"MTI_REQUEST": "100",
"[truncated 1418 bytes]; line: 55, column: 24] (through reference chain: com.discover.dftp.scrubber.domain.Message["PAYLOAD"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:394)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:353)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapAndThrow(BeanDeserializerBase.java:1711)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:371)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3121)
at org.springframework.messaging.converter.MappingJackson2MessageConverter.convertFromInternal(MappingJackson2MessageConverter.java:221)
... 19 common frames omitted
Caused by: com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input: expected close marker for Object (start marker at [Source: (byte[])"{
"HEADER": {
"RETRY_COUNT":0,
"PUBLISH_EVENT_TYPE":"AUTH"
},
"PAYLOAD":{
"MTI": "100",
"[truncated 1418 bytes]; line: 6, column: 13])
at [Source: (byte[])"{
"HEADER": {
"RETRY_COUNT":0,
"PUBLISH_EVENT_TYPE":"AUTH"
},
"PAYLOAD":{
"MTI": "100",
"MTI_REQUEST": "100",
"[truncated 1418 bytes]; line: 55, column: 1949]
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportInvalidEOF(ParserMinimalBase.java:618)
at com.fasterxml.jackson.core.base.ParserBase._handleEOF(ParserBase.java:485)
at com.fasterxml.jackson.core.base.ParserBase._eofAsNextChar(ParserBase.java:497)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._skipWSOrEnd(UTF8StreamJsonParser.java:2933)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextFieldName(UTF8StreamJsonParser.java:964)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:512)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:364)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:29)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:369)
... 23 common frames omitted
[30m2019-03-25 08:41:23,310[0;39m [31mWARN [0;39m [[34mSimpleAsyncTaskExecutor-5[0;39m] [33morg.springframework.amqp.rabbit.listener.ConditionalRejectingErrorHandler$DefaultExceptionStrategy[0;39m: Fatal message conversion error; message rejected; it will be dropped or routed to a dead letter exchange, if so configured: (Body:'[B@1707e57(byte[1918])' MessageProperties [headers={}, contentLength=0, receivedDeliveryMode=NON_PERSISTENT, redelivered=false, receivedExchange=, receivedRoutingKey=hydra.Syphon.q1, deliveryTag=1, consumerTag=amq.ctag-pu7gaBgwJDeCzJMKKOZg3Q, consumerQueue=hydra.Syphon.q1])
[30m2019-03-25 08:41:23,311[0;39m [1;31mERROR[0;39m [[34mSimpleAsyncTaskExecutor-5[0;39m] [33morg.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer[0;39m: Execution of Rabbit message listener failed, and the error handler threw an exception
org.springframework.amqp.AmqpRejectAndDontRequeueException: Error Handler converted exception to fatal
at org.springframework.amqp.rabbit.listener.ConditionalRejectingErrorHandler.handleError(ConditionalRejectingErrorHandler.java:105)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeErrorHandler(AbstractMessageListenerContainer.java:1373)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.handleListenerException(AbstractMessageListenerContainer.java:1626)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1419)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:870)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:854)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1600(SimpleMessageListenerContainer.java:78)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.mainLoop(SimpleMessageListenerContainer.java:1137)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1043)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener method could not be invoked with the incoming message
Endpoint handler details:
Method [public void com.discover.dftp.scrubber.subscriber.EPPQ2Subscriber.receiveMessage(com.discover.dftp.scrubber.domain.Message,com.rabbitmq.client.Channel,java.lang.String,org.springframework.amqp.core.MessageProperties)]
Bean [com.discover.dftp.scrubber.subscriber.EPPQ2Subscriber@56994b]
at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:193)
at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:127)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1547)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1473)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1461)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:1456)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1405)
... 6 common frames omitted
Caused by: org.springframework.messaging.converter.MessageConversionException: Could not read JSON: Unexpected end-of-input: expected close marker for Object (start marker at [Source: (byte[])"{
答案 0 :(得分:0)
如果您定义像您这样的自定义容器工厂:
@Bean
public SimpleRabbitListenerContainerFactory queueListenerContainer(
然后,您需要确保在目标@RabbitListener
中确实使用了它。
查看其JavaDocs:
/**
* The bean name of the {@link org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory}
* to use to create the message listener container responsible to serve this endpoint.
* <p>If not specified, the default container factory is used, if any.
* @return the {@link org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory}
* bean name.
*/
String containerFactory() default "";
请参阅有关此文档的文档:https://docs.spring.io/spring-amqp/docs/2.1.4.RELEASE/reference/#async-annotation-driven