2

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

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);是否也是线程安全的?

在这种情况下,我可以将其移至类级别,而不是每次调用 postMessageWithRetry 函数时都重复它。

4

1 回答 1

1

查看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;
    }

}    
于 2019-05-24T03:42:17.110 回答