Example #1
0
  public static void main(String... args) throws Exception {

    final AdminBuilder adminBuilder = AdminBuilder.adminBuilder();

    final ServiceEndpointServer adminServer = adminBuilder.build();

    adminServer.startServer();

    final HealthServiceAsync healthService = adminBuilder.getHealthService();
    healthService.register("foo", 1, TimeUnit.DAYS);
    healthService.checkInOk("foo");
    healthService.register("bar", 1, TimeUnit.DAYS);
    healthService.checkInOk("bar");

    healthService.clientProxyFlush();

    Sys.sleep(100);
    final HttpClient client =
        HttpClientBuilder.httpClientBuilder().setPort(adminBuilder.getPort()).buildAndStart();

    final HttpResponse httpResponse = client.get("/services/qbit-admin/ok");

    puts(httpResponse.body());

    final HttpResponse httpResponsePage = client.get("/qbit/admin.html");

    puts(httpResponsePage.body());
  }
Example #2
0
  @Test
  public void test() throws Exception {

    final int openPortStartAt = PortUtils.findOpenPortStartAt(7777);
    HttpServer server = HttpServerBuilder.httpServerBuilder().setPort(openPortStartAt).build();

    server.setHttpRequestConsumer(
        serverRequest -> {
          final MultiMap<String, String> headers = MultiMap.multiMap();

          headers.add("foo", "bar").add("foo", "baz");
          serverRequest.getReceiver().response(200, "application/json", "true", headers);
        });

    server.startServer();

    HttpClient client =
        HttpClientBuilder.httpClientBuilder().setPort(openPortStartAt).setHost("localhost").build();

    client.start();

    final HttpTextResponse httpResponse = client.get("/hi");

    Sys.sleep(1000);
    puts(httpResponse.headers());

    boolean foundFoo = httpResponse.headers().keySet().contains("foo");
    assertTrue(foundFoo);
  }
Example #3
0
  /**
   * 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");
    }
  }
Example #4
0
  /**
   * Creates a backend request from the client request and then forwards it.
   *
   * @param clientRequest clientRequest
   */
  private void createBackEndRequestPopulateAndForward(final HttpRequest clientRequest) {
    try {

      if (backendServiceHttpClient == null) {
        handleHttpClientErrorsForBackend(
            clientRequest, new HttpClientClosedConnectionException("Not connected"));
        long timeSinceLastStart = time - lastHttpClientStart;
        if (timeSinceLastStart > 10_000) {
          checkClient();
        }
        return;
      }
      /* forward request to backend client. */
      final HttpRequestBuilder httpRequestBuilder =
          HttpRequestBuilder.httpRequestBuilder()
              .copyRequest(clientRequest)
              .setBinaryReceiver(
                  new HttpBinaryReceiver() {
                    @Override
                    public void response(
                        final int code,
                        final String contentType,
                        final byte[] body,
                        final MultiMap<String, String> headers) {
                      handleBackendClientResponses(clientRequest, code, contentType, body, headers);
                    }

                    @Override
                    public void response(int code, String contentType, byte[] body) {
                      response(code, contentType, body, MultiMap.empty());
                    }
                  })
              .setErrorHandler(e -> handleHttpClientErrorsForBackend(clientRequest, e));

      /** Give user of the lib a chance to populate headers and such. */
      beforeSend.accept(httpRequestBuilder);

      backendServiceHttpClient.sendHttpRequest(httpRequestBuilder.build());
    } catch (HttpClientClosedConnectionException httpClientClosedConnectionException) {

      errorCount.incrementAndGet();
      errorHandler.accept(httpClientClosedConnectionException);
      logger.error("Unable to forward request", httpClientClosedConnectionException);
      handleHttpClientErrorsForBackend(clientRequest, httpClientClosedConnectionException);
      backendServiceHttpClient = null;

      long timeSinceLastStart = time - lastHttpClientStart;
      if (timeSinceLastStart > 10_000) {
        checkClient();
      }

    } catch (Exception ex) {
      errorCount.incrementAndGet();
      errorHandler.accept(ex);
      logger.error("Unable to forward request", ex);
      handleHttpClientErrorsForBackend(clientRequest, ex);

      long timeSinceLastStart = time - lastHttpClientStart;
      if (timeSinceLastStart > 10_000) {
        checkClient();
      }
    }
  }