httpRequestState = future.getState();
httpRequestState = future.getState();
httpRequestState = future.getState();
httpRequestState = future.getState();
public boolean cancelLeafStage(Duration timeout) { checkState(!isClosed(), "client is closed"); URI uri = current().getPartialCancelUri(); if (uri == null) { return false; } Request request = prepareDelete() .setHeader(USER_AGENT, USER_AGENT_VALUE) .setUri(uri) .build(); HttpResponseFuture<StatusResponse> response = httpClient.executeAsync(request, createStatusResponseHandler()); try { StatusResponse status = response.get(timeout.toMillis(), MILLISECONDS); return familyForStatusCode(status.getStatusCode()) == Family.SUCCESSFUL; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw Throwables.propagate(e); } catch (ExecutionException e) { throw Throwables.propagate(e.getCause()); } catch (TimeoutException e) { return false; } }
public static <T, E extends Exception> T executeAsync(JettyHttpClient client, Request request, ResponseHandler<T, E> responseHandler) throws E { HttpResponseFuture<T> future = null; try { future = client.executeAsync(request, responseHandler); } catch (Exception e) { fail("Unexpected exception", e); } try { return future.get(); } catch (InterruptedException e) { currentThread().interrupt(); throw propagate(e); } catch (ExecutionException e) { propagateIfPossible(e.getCause()); if (e.getCause() instanceof Exception) { // the HTTP client and ResponseHandler interface enforces this throw (E) e.getCause(); } // e.getCause() is some direct subclass of throwable throw propagate(e.getCause()); } }
@Test(timeOut = 30000) public void testStop() throws Exception { DummyServlet servlet = new DummyServlet(); createAndStartServer(servlet); try (HttpClient client = new JettyHttpClient()) { URI uri = URI.create(httpServerInfo.getHttpUri().toASCIIString() + "/?sleep=50000"); Request request = prepareGet().setUri(uri).build(); HttpResponseFuture<?> future = client.executeAsync(request, createStatusResponseHandler()); // wait until the servlet starts processing the request servlet.getLatch().await(1, TimeUnit.SECONDS); // stop server while the request is still active server.stop(); // wait until the server is stopped server.join(); // request should fail rather than sleeping the full duration try { future.get(5, TimeUnit.SECONDS); fail("expected exception"); } catch (ExecutionException e) { assertInstanceOf(e.getCause(), UncheckedIOException.class); } } }
private void executeRequest(ProjectCollection collection, List<SchemaField> schema, List<Event> events, CompletableFuture<Void> future, boolean tried) { HttpResponseFuture<StringResponse> f = HTTP_CLIENT.executeAsync(Request.builder() .setUri(buildInsertUri(collection, schema)) .setMethod("POST") .setBodyGenerator(new BinaryRawGenerator(events, schema)) .build(), createStringResponseHandler()); f.addListener(() -> { try { StringResponse stringResponse = f.get(1L, MINUTES); if (stringResponse.getStatusCode() == 200) { future.complete(null); } else { RuntimeException ex = new RuntimeException(stringResponse.getStatusMessage() + " : " + stringResponse.getBody().split("\n", 2)[0]); future.completeExceptionally(ex); } } catch (InterruptedException|ExecutionException|TimeoutException e) { if(!tried) { executeRequest(collection, schema, events, future, true); } else { future.completeExceptionally(e); LOGGER.error(e); } } }, Runnable::run); }
public static <T, E extends Exception> T executeAsync(JettyHttpClient client, Request request, ResponseHandler<T, E> responseHandler) throws E { HttpResponseFuture<T> future = null; try { future = client.executeAsync(request, responseHandler); } catch (Exception e) { fail("Unexpected exception", e); } try { return future.get(); } catch (InterruptedException e) { currentThread().interrupt(); throw new RuntimeException(e); } catch (ExecutionException e) { throwIfUnchecked(e.getCause()); if (e.getCause() instanceof Exception) { // the HTTP client and ResponseHandler interface enforces this throw AbstractHttpClientTest.<E>castThrowable(e.getCause()); } // e.getCause() is some direct subclass of throwable throw new RuntimeException(e.getCause()); } }
public static <T, E extends Exception> T executeAsync(JettyHttpClient client, Request request, ResponseHandler<T, E> responseHandler) throws E { HttpResponseFuture<T> future = null; try { future = client.executeAsync(request, responseHandler); } catch (Exception e) { fail("Unexpected exception", e); } try { return future.get(); } catch (InterruptedException e) { currentThread().interrupt(); throw new RuntimeException(e); } catch (ExecutionException e) { throwIfUnchecked(e.getCause()); if (e.getCause() instanceof Exception) { // the HTTP client and ResponseHandler interface enforces this throw AbstractHttpClientTest.<E>castThrowable(e.getCause()); } // e.getCause() is some direct subclass of throwable throw new RuntimeException(e.getCause()); } }
@Test public void testExceptionFromProcessor() throws Exception { Request request = prepareGet() .setUri(URI.create("http://example.org")) .build(); RuntimeException expectedException = new RuntimeException("test exception"); HttpResponseFuture<String> future = new TestingHttpClient(input -> { throw expectedException; }).executeAsync(request, new CaptureExceptionResponseHandler()); try { future.get(); fail("expected exception"); } catch (ExecutionException e) { Throwable cause = e.getCause(); assertInstanceOf(cause, CapturedException.class); assertEquals(cause.getCause(), expectedException); } } }
@Test public void testExceptionFromProcessor() throws Exception { Request request = prepareGet() .setUri(URI.create("http://example.org")) .build(); RuntimeException expectedException = new RuntimeException("test exception"); HttpResponseFuture<String> future = new TestingHttpClient(input -> { throw expectedException; }).executeAsync(request, new CaptureExceptionResponseHandler()); try { future.get(); fail("expected exception"); } catch (ExecutionException e) { Throwable cause = e.getCause(); assertInstanceOf(cause, CapturedException.class); assertEquals(cause.getCause(), expectedException); } } }
@Test public void testExceptionFromProcessor() throws Exception { Request request = prepareGet() .setUri(URI.create("http://example.org")) .build(); RuntimeException expectedException = new RuntimeException("test exception"); HttpResponseFuture<String> future = new TestingHttpClient(input -> { throw expectedException; }).executeAsync(request, new CaptureExceptionResponseHandler()); try { future.get(); fail("expected exception"); } catch (ExecutionException e) { Throwable cause = e.getCause(); assertInstanceOf(cause, CapturedException.class); assertEquals(cause.getCause(), expectedException); } } }
@Test(timeOut = 60000) public void testStop() throws Exception { createAndStartServer(); try (HttpClient client = new JettyHttpClient()) { URI uri = URI.create(httpServerInfo.getHttpUri().toASCIIString() + "/?sleep=50000"); Request request = prepareGet().setUri(uri).build(); HttpResponseFuture<?> future = client.executeAsync(request, createStatusResponseHandler()); server.stop(); try { future.get(1, TimeUnit.SECONDS); fail("expected exception"); } catch (ExecutionException e) { assertInstanceOf(e.getCause(), RuntimeIOException.class); } } }