I am trying to connect to a remote websocket endpoint (inside spring boot app), and if it throws an Exception I use resilience4j-retry v1.7.7 with JDK 8 to retry connection. However resilience4j-retry tries to connect once and if it fails do not retry. What have I done wrong, the connection is called on ApplicationReadyEvent
.
@Autowired
private WsConnector connector;
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
RetryConfig config = RetryConfig.custom()
.maxAttempts(5)
.waitDuration(Duration.ofSeconds(5))
.build();
RetryRegistry registry = RetryRegistry.of(config);
Retry retry = registry.retry("retryableSupplier", config);
CheckedFunction0<String> retryableSupplier = Retry.decorateCheckedSupplier(retry, connector::startConnection);
try {
System.out.println("retrying " + retryableSupplier.apply());
} catch (Throwable throwable) {
throwable.printStackTrace();
}
Retry.EventPublisher publisher = retry.getEventPublisher();
publisher.onRetry(event1 -> System.out.println(event1.toString()));
publisher.onSuccess(event2 -> System.out.println(event2.toString()));
}
@Service
public class WsConnector {
public String startConnection() throws Exception {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
WSClient client = new WSClient();
container.connectToServer(client, new URI("ws://viewpoint:8080/ping"));
return "Connected";
}
}
Following code worked,
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
RetryConfig config = RetryConfig.custom()
.maxAttempts(5)
.waitDuration(Duration.ofSeconds(5))
.build();
RetryRegistry registry = RetryRegistry.of(config);
Retry retry = registry.retry("retryableSupplier", config);
CheckedFunction0<String> checkedSupplier = Retry.decorateCheckedSupplier(retry, connector::startConnection);
Retry.EventPublisher publisher = retry.getEventPublisher();
publisher.onRetry(event1 -> System.out.println("Retrying: " + event1.toString()));
publisher.onSuccess(event2 -> System.out.println("Retrying Success: " +event2.toString()));
Try<String> result = Try.of(checkedSupplier).recover((throwable -> "I am recovered"));
System.out.println("result: " + result);
}
The issue was with the event EventPublisher
, it was working however was not visible as I initialized EventPublisher
after the retry. So the code in the question will also work provided EventPublisher
is initialized before retry is initiated. I liked above code more clean.