Spring 当异常不可重试时向DLQ发送消息

Spring 当异常不可重试时向DLQ发送消息,spring,spring-cloud-stream,spring-retry,Spring,Spring Cloud Stream,Spring Retry,我使用的是SpringCloudStreams和默认的Spring重试机制,只使用属性 我目前有一个应用程序,当在我的侦听器中抛出MyCustomException时,它会正确重试(最多重试次数)。然后,它被发送到DLQ——这一部分在下面的配置中正常工作 问题是:我还想获得DLQ的其他“不可重试异常”(例如,当我们抛出另一个CustomException或甚至RuntimeException时) 现在,它们没有被重试(这是正确的,并且按照预期工作),但我的案例的问题是它们没有被重定向到myDea

我使用的是SpringCloudStreams和默认的Spring重试机制,只使用属性

我目前有一个应用程序,当在我的侦听器中抛出MyCustomException时,它会正确重试(最多重试次数)。然后,它被发送到DLQ——这一部分在下面的配置中正常工作

问题是:我还想获得DLQ的其他“不可重试异常”(例如,当我们抛出另一个CustomException或甚至RuntimeException时)

现在,它们没有被重试(这是正确的,并且按照预期工作),但我的案例的问题是它们没有被重定向到myDeadLetterQueue

属性文件中使用的配置如下所示

spring.cloud.stream.kafka.bindings.input.consumer.enableDlq = true
spring.cloud.stream.kafka.bindings.input.consumer.dlq-name = myDeadLetterQueue

spring.cloud.stream.bindings.input.consumer.defaultRetryable = false
spring.cloud.stream.bindings.input.consumer.retryableExceptions.com.packagename.common.kafka.exception.MyCustomException = true
此场景是否缺少任何其他配置

但我的问题是,它们没有被重定向到myDeadLetterQueue

这没有道理;这就是它的工作原理;不可重试的异常将直接发送到DLQ主题,而无需重试;我只是测试了一下,没有任何问题:

@springboot应用程序
@EnableBinding(Sink.class)
公共类SO58390786应用程序{
公共静态void main(字符串[]args){
SpringApplication.run(So58390786Application.class,args);
}
@StreamListener(Sink.INPUT)
公共void侦听(字符串输入){
系统输出打印项次(输入);
如果(“状态”。等于(in)){
抛出新的非法国家例外(“ise”);
}
抛出新的非法辩论例外(“iae”);
}
@豆子
公共应用程序运行程序(KafkaTemplate模板){
返回参数->{
发送(“so58390786”,“state”.getBytes());
发送(“so58390786”,“arg.getBytes());
发送(“so58390786”,“state”.getBytes());
};
}
@KafkaListener(id=“so58390786slq”,topics=“so58390786dlq”)
public void listenDlq(字符串输入){
System.out.println(“dlq:+in”);
}
}

编辑

打开调试日志以重试以查看所看到的内容

logging.level.org.springframework.retry=debug
我明白了

2019-10-15 09:35:38.856 DEBUG 60391 --- [           main] o.s.retry.support.RetryTemplate          : Retry: count=0
2019-10-15 09:35:38.967 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Retry: count=0
state
2019-10-15 09:35:38.973 DEBUG 60391 --- [container-0-C-1] o.s.r.backoff.ExponentialBackOffPolicy   : Sleeping for 1000
2019-10-15 09:35:39.977 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=1
2019-10-15 09:35:39.978 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Retry: count=1
state
2019-10-15 09:35:39.979 DEBUG 60391 --- [container-0-C-1] o.s.r.backoff.ExponentialBackOffPolicy   : Sleeping for 2000
2019-10-15 09:35:41.982 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=2
2019-10-15 09:35:41.982 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Retry: count=2
state
2019-10-15 09:35:41.983 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=3
2019-10-15 09:35:41.983 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Retry failed last attempt: count=3

...

2019-10-15 09:35:41.994 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Retry: count=0
arg
2019-10-15 09:35:41.994 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=1
m2019-10-15 09:35:41.994 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Retry failed last attempt: count=1

嗨,加里-是的,你是对的。看起来在使用包含上述代码的库的服务中发生了try/catch。因此,错误从未正确引发,它阻止了spring重试的正常行为。无论如何,谢谢你的帮助。
logging.level.org.springframework.retry=debug
2019-10-15 09:35:38.856 DEBUG 60391 --- [           main] o.s.retry.support.RetryTemplate          : Retry: count=0
2019-10-15 09:35:38.967 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Retry: count=0
state
2019-10-15 09:35:38.973 DEBUG 60391 --- [container-0-C-1] o.s.r.backoff.ExponentialBackOffPolicy   : Sleeping for 1000
2019-10-15 09:35:39.977 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=1
2019-10-15 09:35:39.978 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Retry: count=1
state
2019-10-15 09:35:39.979 DEBUG 60391 --- [container-0-C-1] o.s.r.backoff.ExponentialBackOffPolicy   : Sleeping for 2000
2019-10-15 09:35:41.982 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=2
2019-10-15 09:35:41.982 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Retry: count=2
state
2019-10-15 09:35:41.983 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=3
2019-10-15 09:35:41.983 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Retry failed last attempt: count=3

...

2019-10-15 09:35:41.994 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Retry: count=0
arg
2019-10-15 09:35:41.994 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Checking for rethrow: count=1
m2019-10-15 09:35:41.994 DEBUG 60391 --- [container-0-C-1] o.s.retry.support.RetryTemplate          : Retry failed last attempt: count=1