重试线程安全返回装饰函数吗?

Is decorated function returned by Retry threadsafe?

我有一个 class 可以向远程服务发送消息,如下所示。 我正在使用 resilience4j-retry 重试网络调用。由于根据文档重试实例是线程安全的,因此我在 class 级别创建它并重新使用它。

public class RemoteMessageService {

    Retry retry = Retry.of("RemoteMessageService", RetryConfig.custom()
        .maxAttempts(5)
        .retryExceptions(ProcessingException.class)
        .intervalFunction(IntervalFunction.ofExponentialBackoff())
        .build());    

    public void postMessageWithRetry(final String message){

        Function<Integer, Void> postMessageFunction = Retry.decorateFunction(retry, this::postMessage);

        try {
            postMessageFunction.apply(message)
        } catch (final ProcessingException e) {
            LOG.warn("Got processing exception: {}", e.getMessage());
        } catch (final Exception e) {
            LOG.error("Got unknown exception: {}", e.getMessage());
        }
    }

    private Void postMessage(final String message){
        // Do a network call to send the message to a rest service
        // throw ProcessingException in case of timeout
        return null;
    }

}

我的问题是 Retry.decorateFunction(retry, this::postMessage); 返回的修饰函数是否也是线程安全的?

在那种情况下,我可以将它移动到 class 级别,而不是每次调用 postMessageWithRetry 函数时都重复它。

查看resilience4j-retry代码后,我发现装饰函数实际上是线程安全的;只要我们首先装饰的函数是线程安全的。

所以我可以重写代码如下,因为 postMessage 函数是线程安全的,因此修饰的 postMessageFunction 函数也是线程安全的。

public class RemoteMessageService {

    private final Retry retry = Retry.of("RemoteMessageService", RetryConfig.custom()
        .maxAttempts(5)
        .retryExceptions(ProcessingException.class)
        .intervalFunction(IntervalFunction.ofExponentialBackoff())
        .build());    

    private final Function<Integer, Void> postMessageFunction = Retry.decorateFunction(retry, this::postMessage);

    public void postMessageWithRetry(final String message) {

        try {
            postMessageFunction.apply(message)
        } catch (final ProcessingException e) {
            LOG.warn("Got processing exception: {}", e.getMessage());
        } catch (final Exception e) {
            LOG.error("Got unknown exception: {}", e.getMessage());
        }
    }

    private Void postMessage(final String message) {
        // Do a network call to send the message to a rest service
        // throw ProcessingException in case of timeout
        return null;
    }

}