resilience4j-retry:1.7.1 - 不重试 websocket 连接
resilience4j-retry:1.7.1 - does not retry websocket connection
我正在尝试连接到远程 websocket 端点(在 spring 启动应用程序内),如果它抛出异常,我将使用 resilience4j-retry v1.7.7 和 JDK 8 来重试连接.然而 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";
}
}
以下代码有效,
@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);
}
问题出在事件 EventPublisher
上,它正在运行,但在我重试后初始化 EventPublisher
时不可见。因此,如果 EventPublisher
在启动重试之前已初始化,则问题中的代码也将起作用。我喜欢上面的代码更干净。
我正在尝试连接到远程 websocket 端点(在 spring 启动应用程序内),如果它抛出异常,我将使用 resilience4j-retry v1.7.7 和 JDK 8 来重试连接.然而 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";
}
}
以下代码有效,
@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);
}
问题出在事件 EventPublisher
上,它正在运行,但在我重试后初始化 EventPublisher
时不可见。因此,如果 EventPublisher
在启动重试之前已初始化,则问题中的代码也将起作用。我喜欢上面的代码更干净。