コード例 #1
0
ファイル: ProxyServiceImpl.java プロジェクト: bbyk/qbit
  /**
   * Checks client health periodically to see if we are connected. Tries to reconnect if not
   * connected.
   */
  private void checkClient() {

    try {

      /** If the errorCount is greater than 0, make sure we are still connected. */
      if (errorCount.get() > 0) {
        errorCount.set(0);
        if (backendServiceHttpClient == null || backendServiceHttpClient.isClosed()) {

          if (backendServiceHttpClient != null) {
            try {
              backendServiceHttpClient.stop();
            } catch (Exception ex) {
              logger.debug("Was unable to stop the client connection", ex);
            }
          }
          backendServiceHttpClient = httpClientBuilder.buildAndStart();
          lastHttpClientStart = time;
        }
      }

      /** If the ping builder is present, use it to ping the service. */
      if (pingBuilder.isPresent()) {

        if (backendServiceHttpClient != null) {
          pingBuilder
              .get()
              .setBinaryReceiver(
                  (code, contentType, body) -> {
                    if (code >= 200 && code < 299) {
                      pingCount.incrementAndGet();
                    } else {
                      errorCount.incrementAndGet();
                    }
                  })
              .setErrorHandler(
                  e -> {
                    logger.error("Error doing ping operation", e);
                    errorCount.incrementAndGet();
                  });

          final HttpRequest httpRequest = pingBuilder.get().build();

          backendServiceHttpClient.sendHttpRequest(httpRequest);
        }
      }

    } catch (Exception ex) {
      errorHandler.accept(ex);
      logger.error("Unable to check connection");
    }
  }