Spring cloud 春云溪+;spring重试,如何添加恢复回调并禁用发送到DLQ的逻辑?

Spring cloud 春云溪+;spring重试,如何添加恢复回调并禁用发送到DLQ的逻辑?,spring-cloud,spring-cloud-stream,spring-rabbit,spring-retry,Spring Cloud,Spring Cloud Stream,Spring Rabbit,Spring Retry,我正在使用SpringCloudStream+RabbitMQ活页夹 在我的@StreaListener中,我想使用RetryTemplate对特定异常应用重试逻辑。在重试次数用尽或抛出不可重试错误后,我想添加一个恢复回调,该回调将在我的Postgres DB中保存一条带有错误消息的新记录,并以该消息结束(移动到下一个)。 到目前为止,我得到的是: @StreamListener(Sink.INPUT) 公共无效存储用户(用户用户){ User User=userService.saveUser

我正在使用SpringCloudStream+RabbitMQ活页夹

在我的@StreaListener中,我想使用RetryTemplate对特定异常应用重试逻辑。在重试次数用尽或抛出不可重试错误后,我想添加一个恢复回调,该回调将在我的Postgres DB中保存一条带有错误消息的新记录,并以该消息结束(移动到下一个)。 到目前为止,我得到的是:

@StreamListener(Sink.INPUT)
公共无效存储用户(用户用户){
User User=userService.saveUser(User);//可能引发异常
log.info(“>>用户创建成功:{}”,用户);
}
@StreamRetryTemplate
公共RetryTemplate myRetryTemplate(){
RetryTemplate RetryTemplate=新RetryTemplate();
retryTemplate.setBackOffPolicy(新的ExponentialBackOffPolicy());

Map目前没有提供自定义恢复回调的机制

republishToDlq
设置为
false
(以前是)。它被更改为
true
,如果
autoBindDlq
为false(默认值),这是错误的;我将为此打开一个问题

然后,当重试次数用尽时,异常将被抛出回容器;您可以使用
ListenerContainerCustomizer
添加自定义
ErrorHandler

但是,您在那里获得的数据将出现一个
ListenerExecutionFailed
异常,在其
failedMessage
属性中包含原始(未转换的)Spring AMQP
消息
,而不是您的
用户
对象

编辑

您可以将侦听器添加到绑定的错误通道

@springboot应用程序
@EnableBinding(Sink.class)
公共类SO62137618应用程序{
公共静态void main(字符串[]args){
SpringApplication.run(So62137618Application.class,args);
}
@StreamListener(Sink.INPUT)
公共void侦听(字符串输入){
系统输出打印项次(输入);
抛出新的运行时异常(“测试”);
}
@ServiceActivator(inputChannel=“用户主题.用户消费者.错误”)
公共无效错误(字符串输入){
System.out.println(“.getFailedMessage().getPayload()中“+新字符串((字节[])的重试次数已耗尽”);
}
}

查看对我的答案的编辑。添加这样的错误通道句柄将取代标准的错误处理。我添加了republishToDlq=false和@ServiceActivator,但没有任何更改。错误不会发布到错误通道,我以前尝试过。在耗尽之后,我得到了org.springframework.MessagingException.Am我遗漏了什么?另外,我没有得到错误处理程序部分,为什么我需要它?错误不应该直接进入错误通道吗?抱歉-我做了更正-你必须从异常中提取数据。默认的错误处理程序发布到DLX/DLQ;添加错误处理程序方法可以防止这种情况。谢谢@Gary Russel,它成功了!并且即使没有错误处理程序配置!现在它也不会尝试将消息推送到dlq,它会立即转到错误通道:)
spring.cloud.stream.bindings.input.destination = user-topic
spring.cloud.stream.bindings.input.group = user-consumer
2020-06-01 20:05:58.674  INFO 18524 --- [idge-consumer-1] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:56722]
2020-06-01 20:05:58.685  INFO 18524 --- [idge-consumer-1] o.s.a.r.c.CachingConnectionFactory       : Created new connection: rabbitConnectionFactory.publisher#319c51b0:0/SimpleConnection@2a060201 [delegate=amqp://guest@127.0.0.1:56722/, localPort= 50728]
2020-06-01 20:05:58.697  INFO 18524 --- [idge-consumer-1] c.e.i.o.b.c.RetryConfiguration           : retry finish
2020-06-01 20:05:58.702 ERROR 18524 --- [127.0.0.1:56722] o.s.a.r.c.CachingConnectionFactory       : Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'DLX' in vhost '/', class-id=60, method-id=40)