我正在尝试连接到远程 websocket 端点(在 spring boot 应用程序内),如果它引发异常,我将使用带有 JDK 8 的弹性 4j-retry v1.7.7 重试连接。但是,resilience4j-retry 尝试连接一次,如果失败,请不要重试。我做错了什么,连接被调用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";
}
}