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()); }
@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); }
/** * 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"); } }