【问题标题】:resilience4j-retry:1.7.1 - does not retry websocket connection弹性4j-重试:1.7.1 - 不重试websocket连接
【发布时间】:2022-01-16 17:52:53
【问题描述】:

我正在尝试连接到远程 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";
        }
    }

【问题讨论】:

    标签: java spring spring-boot resilience4j resilience4j-retry


    【解决方案1】:

    以下代码有效,

         @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,那么问题中的代码也将起作用。我喜欢上面的代码更干净。

    【讨论】:

      猜你喜欢
      • 2021-03-07
      • 1970-01-01
      • 1970-01-01
      • 2020-07-18
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      • 1970-01-01
      相关资源
      最近更新 更多