Java Code Examples for com.ning.http.client.ListenableFuture#get()

The following examples show how to use com.ning.http.client.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: GithubTest.java    From bistoury with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void githubTest() throws IOException, ExecutionException, InterruptedException {
    Request request = client.prepareGet("https://api.github.com/repos/xleiy/algorithm/git/trees/master")
            .addHeader("Accept", "application/json")
            .addHeader("'content-type", "application/json")
            .addHeader("Authorization", token)
            .build();
    ListenableFuture<Response> future = client.executeRequest(request);
    Response response = future.get();
    System.out.println(response.getStatusCode());
    System.out.println(response.getResponseBody(Charsets.UTF_8.name()));
}
 
Example 2
Source File: GithubTest.java    From bistoury with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void githubFileTest() throws ExecutionException, InterruptedException, IOException {
    Request request = client.prepareGet("https://api.github.com/repos/xleiy/algorithm/contents/src/main/java/com/xx/leetcode/AddTwoNumbers.java")
            .addQueryParam("ref", "master")
            .addHeader("Accept", "application/json")
            .addHeader("'content-type", "application/json")
            .addHeader("Authorization", token)
            .build();
    ListenableFuture<Response> future = client.executeRequest(request);
    Response response = future.get();
    System.out.println(response.getStatusCode());
    System.out.println(response.getResponseBody(Charsets.UTF_8.name()));
}
 
Example 3
Source File: LoadBalancerClientImpl.java    From Singularity with Apache License 2.0 5 votes vote down vote up
public SingularityCheckingUpstreamsUpdate getLoadBalancerServiceStateForRequest(
  String singularityRequestId
)
  throws IOException, InterruptedException, ExecutionException, TimeoutException {
  final String loadBalancerStateUri = getLoadBalancerStateUri(singularityRequestId);
  final BoundRequestBuilder requestBuilder = httpClient.prepareGet(
    loadBalancerStateUri
  );
  final Request request = requestBuilder.build();
  LOG.debug(
    "Sending load balancer {} request for {} to {}",
    request.getMethod(),
    singularityRequestId,
    request.getUrl()
  );
  ListenableFuture<Response> future = httpClient.executeRequest(request);
  Response response = future.get(loadBalancerTimeoutMillis, TimeUnit.MILLISECONDS);
  LOG.debug(
    "Load balancer {} request {} returned with code {}",
    request.getMethod(),
    singularityRequestId,
    response.getStatusCode()
  );
  Optional<BaragonServiceState> maybeBaragonServiceState = Optional.ofNullable(
    objectMapper.readValue(response.getResponseBodyAsBytes(), BaragonServiceState.class)
  );
  return new SingularityCheckingUpstreamsUpdate(
    maybeBaragonServiceState,
    singularityRequestId
  );
}
 
Example 4
Source File: LoadBalancerClientImpl.java    From Singularity with Apache License 2.0 4 votes vote down vote up
private LoadBalancerUpdateHolder sendRequest(
  LoadBalancerRequestId loadBalancerRequestId,
  Request request,
  BaragonRequestState onFailure
) {
  try {
    LOG.trace(
      "Sending LB {} request for {} to {}",
      request.getMethod(),
      loadBalancerRequestId,
      request.getUrl()
    );

    ListenableFuture<Response> future = httpClient.executeRequest(request);

    Response response = future.get(loadBalancerTimeoutMillis, TimeUnit.MILLISECONDS);

    LOG.trace(
      "LB {} request {} returned with code {}",
      request.getMethod(),
      loadBalancerRequestId,
      response.getStatusCode()
    );

    if (response.getStatusCode() == 504) {
      return new LoadBalancerUpdateHolder(
        BaragonRequestState.UNKNOWN,
        Optional.of(
          String.format(
            "LB %s request %s timed out",
            request.getMethod(),
            loadBalancerRequestId
          )
        )
      );
    } else if (!JavaUtils.isHttpSuccess(response.getStatusCode())) {
      return new LoadBalancerUpdateHolder(
        onFailure,
        Optional.of(String.format("Response status code %s", response.getStatusCode()))
      );
    }

    BaragonResponse lbResponse = readResponse(response);

    return new LoadBalancerUpdateHolder(
      lbResponse.getLoadBalancerState(),
      lbResponse.getMessage().toJavaUtil()
    );
  } catch (TimeoutException te) {
    LOG.trace(
      "LB {} request {} timed out after waiting {}",
      request.getMethod(),
      loadBalancerRequestId,
      JavaUtils.durationFromMillis(loadBalancerTimeoutMillis)
    );
    return new LoadBalancerUpdateHolder(
      BaragonRequestState.UNKNOWN,
      Optional.of(
        String.format(
          "Timed out after %s",
          JavaUtils.durationFromMillis(loadBalancerTimeoutMillis)
        )
      )
    );
  } catch (Throwable t) {
    LOG.error(
      "LB {} request {} to {} threw error",
      request.getMethod(),
      loadBalancerRequestId,
      request.getUrl(),
      t
    );
    return new LoadBalancerUpdateHolder(
      BaragonRequestState.UNKNOWN,
      Optional.of(
        String.format("Exception %s - %s", t.getClass().getSimpleName(), t.getMessage())
      )
    );
  }
}