gpt4 book ai didi

java - @Retryable 即使没有发生异常也会多次调用

转载 作者:行者123 更新时间:2023-12-02 03:29:29 24 4
gpt4 key购买 nike

我使用了spring @Retryable来实现在使用RestTemplate调用其他服务出现问题时重试函数调用。

该函数如下所示,问题是我已将 maxAttempts 设置为 4,以防发生任何异常,它应该尝试 4 次。但即使没有任何异常(exception),该函数也执行了 4 次,并且在数据库中创建了四个员工条目。

createEmployee 函数调用另一个服务在数据库中创建员工

@Retryable(value = { Exception.class }, maxAttempts = 4, backoff = @Backoff(delay = 1000))
public Response createEmployee(EmployeeRequest employeeRequest)
{
log.info(“creating employee”)
:
// calls another micro service using RestTemplate which creates employee into the DB
:
}
AppConfig 中的

@EnableRetry

@Configuration
@EnableRetry
public class AppConfig {
}

谁能帮我解决这个问题

最佳答案

您应该检查您的“调用另一个微服务”实现。

它可能是由在该逻辑内部调用的另一个服务抛出异常。我建议创建一个自定义异常并将其用于您的重试定义。然后您可以检查是否是另一种意外异常导致了重试中的 4 次尝试。

@Retryable(value = { YourCustomException.class }, maxAttempts = 4, backoff = @Backoff(delay = 1000))
public Response createEmployee(EmployeeRequest employeeRequest)
{
log.info(“creating employee”)
:
// calls another micro service using RestTemplate which creates employee into the DB
:
}

关于java - @Retryable 即使没有发生异常也会多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56893367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com