在RabbitMQ侦听器中隐藏运行时异常

在RabbitMQ侦听器中隐藏运行时异常

本文介绍了在RabbitMQ侦听器中隐藏运行时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,我使用了一些例外来拒绝该消息,但是在控制台中却显示了例外,乍一看似乎还不行.

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

我正在使用spring-boot和默认记录器!

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

public static class UndispatchException extends
       AmqpRejectAndDontRequeueException{

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

}

这里是列表器

@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

推荐答案

在您的日志记录配置中,设置

In your logging configuration, set the log level for

org.springframework.amqp.rabbit.listener.ConditionalRejectingErrorHandler

错误(该消息记录在WARN级别).

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

使用Spring Boot,您只需添加...

With Spring Boot, you can simply add...

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

...到您的application.properties(或.yml)文件.

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

编辑

如果您想做其他事情(例如记录某些异常),则可以复制ConditionalRejectingErrorHandler并在handleError()方法中进行更改. 代码在这里.

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.

这篇关于在RabbitMQ侦听器中隐藏运行时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:57