2

我有一个带有注释的 Spring 方法@Async

@Async
public void doSomething(long id, String text) {
    //do something
}

该方法调用远程服务,该服务偶尔会超时。有没有办法捕捉超时并在@AsyncN 秒后将任务重新提交给方法?

我怀疑从内部调用带注释的方法会起作用,因为 Spring 使用切入点来拦截调用。

我能想到的唯一方法是重写AsyncExecutionAspectSupport.handleError以重建 instanceof MethodInvocation,然后调用它的副本 AsyncExecutionInterceptor.invoke让我指定延迟。

还有其他方法吗?也许有人已经这样做了?


旁白

我可以想象一个解决方案,通过该解决方案@Async扩展为指定重试案例,其方式与 Spring 中管理事务的方式非常相似,但这比上述理论解决方案更基本!

@Async(retryOn=TimeoutException.class, maxRetries=2, delayStrategy=DelayStrategy.DOUBLE)
4

0 回答 0