hide runtime exception in RabbitMQ listener

☆樱花仙子☆ 提交于 2019-12-23 03:08:04

问题


I used some exception to reject the message in some cases that intentionally happened but shown the exception in the console which looks not alright for the first glance.

how can I hide that specific exception from logging on console/file

I'm Using spring-boot and the default loggers!

public static class UndispatchException extends 
       AmqpRejectAndDontRequeueException{

    public UndispatchException() {
        super("Dispatch still looking for a driver");
    }

}

here the listner

@RabbitListener(queues = TEST_QUEUE)
public void handle(Dispatch in) {
    if(in.isRequeue()){
        log.debug("will reject the message");
        throw new UndispatchException();
    }
    log.debug("won't reject the message");

}

here is the log I want to hide it! which mandetory to have to requeue the message in some cases!

2018-05-15 18:41:11.494  WARN 2709 --- [cTaskExecutor-1] s.a.r.l.ConditionalRejectingErrorHandler : Execution of Rabbit message listener failed.

org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener method 'public void com.amqp.handleException.demo.DemoApplication.handle(com.amqp.handleException.demo.DemoApplication$Dispatch)' threw exception
    at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:140) ~[spring-rabbit-1.7.7.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:106) ~[spring-rabbit-1.7.7.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:856) ~[spring-rabbit-1.7.7.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:779) ~[spring-rabbit-1.7.7.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$001(SimpleMessageListenerContainer.java:105) [spring-rabbit-1.7.7.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$1.invokeListener(SimpleMessageListenerContainer.java:208) ~[spring-rabbit-1.7.7.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1349) [spring-rabbit-1.7.7.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:760) ~[spring-rabbit-1.7.7.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:1292) [spring-rabbit-1.7.7.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:1262) [spring-rabbit-1.7.7.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1800(SimpleMessageListenerContainer.java:105) [spring-rabbit-1.7.7.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1518) [spring-rabbit-1.7.7.RELEASE.jar:na]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]
Caused by: com.amqp.handleException.demo.DemoApplication$UndispatchException: Dispatch still looking for a driver
    at com.amqp.handleException.demo.DemoApplication.handle(DemoApplication.java:47) ~[classes/:na]
    at sun.reflect.GeneratedMethodAccessor38.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:180) ~[spring-messaging-4.3.15.RELEASE.jar:4.3.15.RELEASE]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:112) ~[spring-messaging-4.3.15.RELEASE.jar:4.3.15.RELEASE]
    at org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:49) ~[spring-rabbit-1.7.7.RELEASE.jar:na]
    at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:126) ~[spring-rabbit-1.7.7.RELEASE.jar:na]
    ... 12 common frames omitted

回答1:


In your logging configuration, set the log level for

org.springframework.amqp.rabbit.listener.ConditionalRejectingErrorHandler

to ERROR (that message is logged at WARN level).

With Spring Boot, you can simply add...

logging.level.org.springframework.amqp.rabbit.listener.ConditionalRejectingErrorHandler=ERROR

...to your application.properties (or .yml) file.

EDIT

If you wish to do something different (such as log certain exceptions) you can make a copy of the ConditionalRejectingErrorHandler and make changes in the handleError() method. The code is here.

You would then configure the listener container (or listener container factory) with your custom error handler.



来源:https://stackoverflow.com/questions/50350377/hide-runtime-exception-in-rabbitmq-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!