我有一个向远程服务发送消息的类,如下所示。我正在使用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 函数时都重复它。