如何解决使用 EmbeddedKafka 进行测试 - 证明重试次数
我有一个使用@EmbeddedKafka 编写的测试。 我使用 SeekToCurrentErrorHandler 配置了错误处理。
我有一个测试,我将消息推送到 Kafka,我可以看到它由 DeadLetterPublishingRecover 正确处理并发布到 DLT。
我想在我的测试中添加进一步的断言,以证明 SeekToCurrentErrorHandler 重试了 X 次,但在发生 MyCustomException 时仅重试了 1 次。
我已经用
配置了错误处理程序base.button1_Click(sender,e);
我正在努力如何在测试中获取重试信息。如果 DeadLetterPublishingRecoverer 添加一个标头,即 kafka_deliveryAttempt 标头,那就太好了。
我也试过这样做:
- 在测试中创建 RetryTemplate
errorHandler.addNotRetryableExceptions(MyCustomException.class)
- 在重试模板上设置侦听器。
@Configuration
public class TestConfiguration {
@Bean
@Primary
public RetryTemplate retryTemplate() {
return new RetryTemplateBuilder().maxAttempts(3)
.fixedBackoff(500)
.build();
}
}
解决方法
SeekToCurrentErrorHandler
不使用 RetryTemplate
;它有自己的重试机制。
从 2.7 版开始,您可以向错误处理程序添加一个或多个 RetryListener
:
/**
* Set one or more {@link RetryListener} to receive notifications of retries and
* recovery.
* @param listeners the listeners.
* @since 2.7
*/
public void setRetryListeners(RetryListener... listeners) {
this.failureTracker.setRetryListeners(listeners);
}
/**
* A listener for retry activity.
*
* @author Gary Russell
* @since 2.7
*
*/
@FunctionalInterface
public interface RetryListener {
/**
* Called after a delivery failed for a record.
* @param record the failed record.
* @param ex the exception.
* @param deliveryAttempt the delivery attempt.
*/
void failedDelivery(ConsumerRecord<?,?> record,Exception ex,int deliveryAttempt);
/**
* Called after a failing record was successfully recovered.
* @param record the record.
* @param ex the exception.
*/
default void recovered(ConsumerRecord<?,Exception ex) {
}
/**
* Called after a recovery attempt failed.
* @param record the record.
* @param original the original exception causing the recovery attempt.
* @param failure the exception thrown by the recoverer.
*/
default void recoveryFailed(ConsumerRecord<?,Exception original,Exception failure) {
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。