Java Code Examples for org.asynchttpclient.ListenableFuture#get()

The following examples show how to use org.asynchttpclient.ListenableFuture#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TacPublishTestService.java    From tac with MIT License 6 votes vote down vote up
/**
 * test with http .
 *
 * @param instId
 * @param msCode
 * @param params
 * @return
 */
private TacResult<?> onlinePublishTestHttp(Long instId, String msCode, Map<String, Object> params) {

    AsyncHttpClient asyncHttpClient = asyncHttpClient();

    ListenableFuture<Response> execute = asyncHttpClient.preparePost(containerWebApi + "/" + msCode)
        .addHeader("Content-Type", "application/json;charset=UTF-8").setBody(JSONObject.toJSONString(params))
        .execute();
    Response response;
    try {
        response = execute.get(10, TimeUnit.SECONDS);
        if (response.getStatusCode() == HttpConstants.ResponseStatusCodes.OK_200) {
            TacResult tacResult = JSONObject.parseObject(response.getResponseBody(), TacResult.class);
            return tacResult;
        }
        log.error("onlinePublishTestHttp msCode:{} params:{} {}", msCode, params, response);
        throw new IllegalStateException("request engine error " + msCode);
    } catch (Exception e) {
        throw new IllegalStateException(e.getMessage(), e);
    }

}
 
Example 2
Source File: HttpClientTest.java    From tac with MIT License 6 votes vote down vote up
@Test
public void test() throws InterruptedException, ExecutionException, TimeoutException {

    JSONObject data = new JSONObject();
    data.put("name", "ljinshuan");

    AsyncHttpClient asyncHttpClient = asyncHttpClient();

    ListenableFuture<Response> execute = asyncHttpClient.preparePost("http://localhost:8001/api/tac/execute/shuan")
        .addHeader("Content-Type", "application/json;charset=UTF-8").setBody(data.toJSONString()).execute();
    Response response = execute.get(10, TimeUnit.SECONDS);

    if (response.getStatusCode() == HttpConstants.ResponseStatusCodes.OK_200) {
        TacResult tacResult = JSONObject.parseObject(response.getResponseBody(), TacResult.class);

        System.out.println(tacResult);
    }
    System.out.println(response);
}
 
Example 3
Source File: WebhookTest.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
@Override
public <T> ListenableFuture<T> executeRequest(org.asynchttpclient.Request request,
                                              AsyncHandler<T> handler) {

  WebhookOnThrowableHandler<T> wrapped = new WebhookOnThrowableHandler<>(
      handler,
      new CountDownLatch(1));
  ListenableFuture<T> future = super.executeRequest(request, wrapped);
  try {
    future.get();
    future.done();
  } catch (InterruptedException | ExecutionException e) {
    // The future threw an exception, wait for onThrowable to complete.
    wrapped.waitForOnThrowableToFinish();
  }
  return future;
}
 
Example 4
Source File: ClickhouseWriterTest.java    From flink-clickhouse-sink with MIT License 5 votes vote down vote up
private void send(String data, String url, String basicCredentials, AtomicInteger counter) {
    String query = String.format("INSERT INTO %s VALUES %s ", "groot3.events", data);
    BoundRequestBuilder requestBuilder = asyncHttpClient.preparePost(url).setHeader("Authorization", basicCredentials);
    requestBuilder.setBody(query);
    ListenableFuture<Response> whenResponse = asyncHttpClient.executeRequest(requestBuilder.build());
    Runnable callback = () -> {
        try {
            Response response = whenResponse.get();
            System.out.println(Thread.currentThread().getName() + " " + response);

            if (response.getStatusCode() != 200) {
                System.out.println(Thread.currentThread().getName() + " try to retry...");
                int attempt = counter.incrementAndGet();
                if (attempt > MAX_ATTEMPT) {
                    System.out.println("Failed ");
                } else {
                    send(data, url, basicCredentials, counter);
                }
            }
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    };

    Executor executor = ForkJoinPool.commonPool();
    whenResponse.addListener(callback, executor);
}
 
Example 5
Source File: AsyncHttpRequest.java    From junit-servers with MIT License 5 votes vote down vote up
@Override
protected HttpResponse doExecute() throws Exception {
	final HttpUrl endpoint = getEndpoint();
	final String scheme = endpoint.getScheme();
	final String userInfo = null;
	final String host = endpoint.getHost();
	final int port = endpoint.getPort();
	final String path = Utf8UrlEncoder.encodePath(endpoint.getPath());
	final String query = null;
	final String fragment = null;
	final Uri uri = new Uri(scheme, userInfo, host, port, path, query, fragment);

	final String method = getMethod().getVerb();
	final RequestBuilder builder = new RequestBuilder(method, true).setUri(uri);

	handleQueryParameters(builder);
	handleBody(builder);
	handleHeaders(builder);
	handleCookies(builder);

	final Request request = builder.build();
	final ListenableFuture<Response> future = client.executeRequest(request);

	final long start = nanoTime();
	final Response response = future.get();
	final long duration = nanoTime() - start;

	return AsyncHttpResponseFactory.of(response, duration);
}
 
Example 6
Source File: HttpClient.java    From galeb with Apache License 2.0 4 votes vote down vote up
public Response execute(RequestBuilder builder) throws InterruptedException, java.util.concurrent.ExecutionException {
    Request request = builder.build();
    ListenableFuture<Response> listenableFuture = asyncHttpClient.executeRequest(request);
    return listenableFuture.get();
}