org.apache.http.nio.client.methods.HttpAsyncMethods Java Examples

The following examples show how to use org.apache.http.nio.client.methods.HttpAsyncMethods. 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: HttpClient.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
public <T extends Response> void executeAsync(
        final Request request,
        final ResponseHandler<T> responseHandler,
        final HttpClientContext httpClientContext
) {

    HttpUriRequest clientRequest;
    try {
        clientRequest = createClientRequest(request);
    } catch (IOException e) {
        responseHandler.failed(e);
        return;
    }

    FutureCallback<HttpResponse> responseCallback = createCallback(responseHandler);
    getAsyncClient().execute(
            HttpAsyncMethods.create(clientRequest),
            asyncResponseConsumerFactory.create(),
            httpClientContext,
            responseCallback);
}
 
Example #2
Source File: AsyncClientHttpExchangeStreaming.java    From yunpian-java-sdk with MIT License 6 votes vote down vote up
public static void main(final String[] args) throws Exception {
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
    try {
        httpclient.start();
        Future<Boolean> future = httpclient.execute(HttpAsyncMethods.createGet("http://localhost:8080/"),
                new MyResponseConsumer(), null);
        Boolean result = future.get();
        if (result != null && result.booleanValue()) {
            System.out.println("Request successfully executed");
        } else {
            System.out.println("Request failed");
        }
        System.out.println("Shutting down");
    } finally {
        httpclient.close();
    }
    System.out.println("Done");
}
 
Example #3
Source File: AsyncNetwork.java    From jlitespider with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public void begin() throws InterruptedException {
	CloseableHttpAsyncClient httpclient = httpAsyncClientBuilder.build();
	httpclient.start();
	new Thread(() -> {
		while (true) {
			try {
				Url url = this.urlQueue.take();
				httpclient.execute(HttpAsyncMethods.createGet(url.url), new MyResponseConsumer(url), new MyFutureCallback(url));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}).start();

}
 
Example #4
Source File: KinesisVideoApacheHttpAsyncClient.java    From amazon-kinesis-video-streams-producer-sdk-java with Apache License 2.0 5 votes vote down vote up
public void executeRequest() {
    final HttpPost request = new HttpPost(mBuilder.mUri);
    for (Map.Entry<String, String> entry : mBuilder.mHeaders.entrySet()) {
        request.addHeader(entry.getKey(), entry.getValue());
    }
    final HttpEntity entity = new StringEntity(mBuilder.mContentInJson, mBuilder.mContentType);
    request.setEntity(entity);
    final HttpAsyncRequestProducer requestProducer = HttpAsyncMethods.create(request);
    this.mHttpClient.execute(requestProducer, ((Builder) mBuilder).mHttpAsyncResponseConsumer,
            ((Builder) mBuilder).mFutureCallback);
}
 
Example #5
Source File: HttpAsyncDownloader.java    From JMCCC with MIT License 5 votes vote down vote up
private void download() {
	if (Thread.interrupted() || isExceptional()) {
		lifecycle().cancelled();
		return;
	}

	FutureManager<T> manager = createFutureManager();
	DownloadRetryHandler retryHandler = new DownloadRetryHandler();
	DownloadSessionHandler<T> handler = new DownloadSessionHandler<>(task, DownloadCallbacks.group(DownloadCallbacks.fromCallback(manager), retryHandler));
	Future<T> downloadFuture = httpClient.execute(HttpAsyncMethods.createGet(task.getURI()), handler.consumer, handler.callback);
	manager.setFuture(downloadFuture);
}
 
Example #6
Source File: ImportRunner.java    From newts with Apache License 2.0 5 votes vote down vote up
public static Func1<String, Observable<ObservableHttpResponse>> postJSON(final String baseURL, final CloseableHttpAsyncClient httpClient) {

        final URI baseURI = URI.create(baseURL);

        return new Func1<String, Observable<ObservableHttpResponse>>() {
            @Override
            public Observable<ObservableHttpResponse> call(String json) {
                try {
                    return ObservableHttp.createRequest(HttpAsyncMethods.createPost(baseURI, json, ContentType.APPLICATION_JSON), httpClient).toObservable();
                } catch (UnsupportedEncodingException e) {
                    throw Exceptions.propagate(e);
                }
            }
        };
    }
 
Example #7
Source File: HttpClientFactory.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
protected HttpAsyncResponseConsumerFactory createHttpAsyncResponseConsumerFactory() {
    if (pooledResponseBuffersEnabled) {
        return new PoolingAsyncResponseConsumerFactory(createPool());
    }
    return () -> HttpAsyncMethods.createConsumer();
}
 
Example #8
Source File: AsyncHttpService.java    From Tenable.io-SDK-for-Java with MIT License 2 votes vote down vote up
/**
 * Retries given HTTP request. Called internally only, from the HttpFuture
 *
 * @param httpUriRequest the HttpUriRequest to retry
 * @param responseConsumer the response consumer
 * @param numRetry The retry count
 * @return the resulting Future<HttpResponse> instance
 */
Future<HttpResponse> retryOperation( HttpUriRequest httpUriRequest, HttpAsyncResponseConsumer<HttpResponse> responseConsumer, int numRetry ) {
    httpUriRequest.setHeader( "X-Tio-Retry-Count", Integer.toString( numRetry ) );
    return responseConsumer == null ? asyncClient.execute( httpUriRequest, null ) : asyncClient.execute( HttpAsyncMethods.create( httpUriRequest ), responseConsumer, null, null );
}