春天从请求中使用自定义布尔值重试

问题描述

我是春季注释和春季重试的新手。下面是示例代码,我的查询基于方法参数isRetryNeeded,我需要确定是否需要重试(这里是3次)。谢谢

package com.example.retry;

import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;

public interface BackendAdapter {

    @Retryable(value = { RemoteServiceNotAvailableException.class },maxAttempts = 3,backoff = @Backoff(delay = 1000))
    public String getBackendResponse(boolean isRetryNeeded,boolean simulateretryfallback);

    @Recover
    public String getBackendResponseFallback(RuntimeException e);

}

解决方法

没有内置支持它的东西。

但是,由于您的重试取决于RemoteServiceNotAvailableException,因此如果isRetryNeededfalse,只需抛出其他异常。