向rabbitmq服务器发送确认取决于转换器和监听器 [英] Sending acknowledgment to rabbitmq server in depends on converter and listener

查看:359
本文介绍了向rabbitmq服务器发送确认取决于转换器和监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我介绍了我的案例背景:

我正在使用 spring-boot spring-rabbitmq 。它对我有用,你应该知道我必须为收到的消息实现自定义转换器。

(1)从这个转换器可以抛出异常,例如在inproper message等等。

(2)成功转换(无异常)后,将调用侦听器。然后,在监听器中它也可以抛出异常。

First of all, I present context of my case:
I am using spring-boot and spring-rabbitmq. It is working for me, you should know that I must have implemented custom converter for received messages.
(1) From this converter can be thrown exception, for instance in case of inproper message and so on.
(2) After successfull converting (no exception) listener is invoked. Then, in the listener it can also be thrown exception.

现在,我想强制做两件事:

(1') 在转换器中出现故障时不要重新排队消息。只需将确认发送到队列并模拟一切正常。

(2')在这种情况下,默认设置是什么?当内部 spring-rabbitmq引擎决定向队列发送确认?当它决定重新排队的命令?是否有可能根据情况进行管理?

Now, I would like to force two things:
(1') Don't requeue message in case of expcetion in converter. Simply, send acknowledgment to queue and simulate that everything is alright.
(2') What is default setting in this case ? When internal spring-rabbitmq engine decide to send acknowledgment to queue ? When it decide command to requeue ? Is it possible to manage it in depends on situation ?

我在docs中找到:


如果没有启用重试并且监听器抛出异常,
默认情况下,交付将无限期重试。您可以通过两种方式修改此
行为;将defaultRequeueRejected属性设置为false
并尝试零重新传递;或者,抛出
AmqpRejectAndDontRequeueException来表示消息应该是
被拒绝。这是启用重试并且达到
最大传递尝试时使用的机制。

If retries are not enabled and the listener throws an exception, by default the delivery will be retried indefinitely. You can modify this behavior in two ways; set the defaultRequeueRejected property to false and zero re-deliveries will be attempted; or, throw an AmqpRejectAndDontRequeueException to signal the message should be rejected. This is the mechanism used when retries are enabled and the maximum delivery attempts are reached.

例如,取决于在监听器中捕获异常我应该决定是否要重新排队消息,就像我想的那样(只需从 catch中抛出 AmqpRejectAndDontRequeueException )。我不确定这是不是好方法,这就是我问你关于你的意见的原因。

For example, in depends on catched exception in listener I should decide if I would like to requeue message, as I think (simply by throwing from catch AmqpRejectAndDontRequeueException). I am not sure if it is good way and it is why I am asking you about your opinion.

推荐答案

请阅读< a href =http://docs.spring.io/spring-amqp//reference/html/ =nofollow noreferrer>参考手册。

行为(主要)由 ErrorHandler 控制。

抛出 MessageConversionException - 容器为大多数异常重新排队消息,但有些异常被认为是致命的。一般来说,如果一条消息无法转换,那么重新发送它就毫无意义。

Throw a MessageConversionException - the container requeues messages for most exceptions but some exceptions are considered fatal. Generally, if a message can't be converted, then redelivering it makes no sense.

这一点(在令人惊讶的情况下?)中明确解释为异常处理

This is all clearly explained in the section (surprisingly?) called Exception Handling


从版本1.3.2开始,默认的ErrorHandler现在是一个ConditionalRejectingErrorHandler,它将拒绝(而不是重新排队)因无法恢复的错误而失败的消息:

Starting with version 1.3.2, the default ErrorHandler is now a ConditionalRejectingErrorHandler which will reject (and not requeue) messages that fail with an irrecoverable error:

osamqp ... MessageConversionException

o.s.amqp...MessageConversionException

osmessaging ... MessageConversionException

o.s.messaging...MessageConversionException

osmessaging ... MethodArgumentNotValidException

o.s.messaging...MethodArgumentNotValidException

osmessaging ... MethodArgumentTypeMismatchException

o.s.messaging...MethodArgumentTypeMismatchException

java.lang.NoSuchMethodException

java.lang.NoSuchMethodException

java.lang.ClassCastException

java.lang.ClassCastException

转换传入的消息有效负载时可以抛出第一个一个MessageConverter。如果在映射到@RabbitListener方法时需要额外的转换,则转换服务可能抛出第二个。如果在侦听器中使用验证(例如@Valid)并且验证失败,则可能抛出第三个。如果入站消息转换为对目标方法不正确的类型,则可能抛出第四个。例如,参数声明为Message但收到Message。

The first can be thrown when converting the incoming message payload using a MessageConverter. The second may be thrown by the conversion service if additional conversion is required when mapping to a @RabbitListener method. The third may be thrown if validation (e.g. @Valid) is used in the listener and the validation fails. The fourth may be thrown if the inbound message was converted to a type that is not correct for the target method. For example, the parameter is declared as Message but Message is received.

在版本1.6.3中添加了第五个和第六个。

The fifth and sixth were added in version 1.6.3.

您可以根据需要自定义 ErrorHandler

You can customize the ErrorHandler as needed.

这篇关于向rabbitmq服务器发送确认取决于转换器和监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆