如何在春季重试框架中配置延迟以首次重试

发布于 2025-02-05 12:04:10 字数 1391 浏览 2 评论 0原文

即时通讯在一个应用程序上,即IM使用Spring @Retryable执行重试操作3次(默认情况下重试计数为3)。以下是我的示例代码

@Retryable(
        value= CustomException.class,
        backoff = @Backoff(
                delay = 5000,
                multiplier = 2,
                maxDelay = 20000
        )
) 
public void performRetry() throws CustomException {
    try {

        if(RetrySynchronizationManager.getContext().getRetryCount()==0) { // retry count starts from 0.
            logger.info("Starting Retry attempts. Thread name {}. Giving delay of 3 seconds", Thread.currentThread().getName());
            Thread.sleep(3000);
        }

        boolean response = service.execute();  // calling main service

        // Rest of the code

    } catch(Exception e) {
        logger.error("Retry Count {} Error occurred {}", RetrySynchronizationManager.getContext().getRetryCount(), e.getMessage());
        throw new CustomException(e.getMessage());
    }
}

我的查询是,我想在第一次尝试(而不是首次重试)之前引入3秒钟的延迟,即首次调用我的主服务service.execute();

由于在春季重试,它立即触发了第一次尝试而不会延迟。因此,在我的代码中,在我呼叫我的主服务service.execute();首次使用thread.sleep(3000);

IM使用retrysynchronizationManager。 getContext()。getReTryCount()获得重试计数。因此,如果它的0含义第一次尝试,则表示第二次尝试(第一次重试呼叫),而2表示第三次尝试(第二个重试呼叫)。

还有其他方法可以让使用thread.sleep()分开延迟吗?

我有什么办法可以指定 @retryable注释中的第一次重试的延迟?

等待早期回应。

Im working on an application where im using spring @Retryable to perform retry operation for 3 times (By default retry count is 3). Below is my sample code

@Retryable(
        value= CustomException.class,
        backoff = @Backoff(
                delay = 5000,
                multiplier = 2,
                maxDelay = 20000
        )
) 
public void performRetry() throws CustomException {
    try {

        if(RetrySynchronizationManager.getContext().getRetryCount()==0) { // retry count starts from 0.
            logger.info("Starting Retry attempts. Thread name {}. Giving delay of 3 seconds", Thread.currentThread().getName());
            Thread.sleep(3000);
        }

        boolean response = service.execute();  // calling main service

        // Rest of the code

    } catch(Exception e) {
        logger.error("Retry Count {} Error occurred {}", RetrySynchronizationManager.getContext().getRetryCount(), e.getMessage());
        throw new CustomException(e.getMessage());
    }
}

My query is that i want to introduce a delay of 3 seconds before doing first attempt (not first retry) i.e. calling my main service service.execute(); for the first time.

Since in spring retry it immediately triggers the first attempt without any delay. So in my code before im calling my main service service.execute(); for the first time im using Thread.sleep(3000);

Im using RetrySynchronizationManager.getContext().getRetryCount() to get retry count. So if its 0 meaning first attempt, 1 means second attempt (first retry call) and 2 means third attempt (second retry call).

Is there any other way that i can give delay apart from using thread.sleep()?

Is there any way i can specify delay for the first retry in @Retryable annotation?

Waiting for an early response.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半透明的墙 2025-02-12 12:04:10

我的查询是,我想在第一次重试之前引入3秒钟的延迟,即致电我的主服务service.execute();第一次。

第一个重试是第二个呼叫

无法为第一个呼叫配置延迟;您必须在通话代码中这样做。

My query is that i want to introduce a delay of 3 seconds before doing first retry i.e. calling my main service service.execute(); for the first time.

The first retry is the second call.

There is no way to configure a delay for the first call; you would have to do that in your calling code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文