org.apache.http.nio.protocol.BasicAsyncRequestProducer Java Examples

The following examples show how to use org.apache.http.nio.protocol.BasicAsyncRequestProducer. 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: JsonRpcHttpAsyncClient.java    From jsonrpc4j with MIT License 5 votes vote down vote up
/**
 * Invokes the given method with the given arguments and invokes the
 * {@code JsonRpcCallback} with the result cast to the given
 * {@code returnType}, or null if void. The {@code extraHeaders} are added
 * to the request.
 *
 * @param methodName   the name of the method to invoke
 * @param argument     the arguments to the method
 * @param extraHeaders extra headers to add to the request
 * @param returnType   the return type
 * @param callback     the {@code JsonRpcCallback}
 */
@SuppressWarnings("unchecked")
private <T> Future<T> doInvoke(String methodName, Object argument, Class<T> returnType, Map<String, String> extraHeaders, JsonRpcCallback<T> callback) {
	
	String path = serviceUrl.getPath() + (serviceUrl.getQuery() != null ? "?" + serviceUrl.getQuery() : "");
	int port = serviceUrl.getPort() != -1 ? serviceUrl.getPort() : serviceUrl.getDefaultPort();
	HttpRequest request = new BasicHttpEntityEnclosingRequest("POST", path);
	
	addHeaders(request, headers);
	addHeaders(request, extraHeaders);
	
	try {
		writeRequest(methodName, argument, request);
	} catch (IOException e) {
		callback.onError(e);
	}
	
	HttpHost target = new HttpHost(serviceUrl.getHost(), port, serviceUrl.getProtocol());
	BasicAsyncRequestProducer asyncRequestProducer = new BasicAsyncRequestProducer(target, request);
	BasicAsyncResponseConsumer asyncResponseConsumer = new BasicAsyncResponseConsumer();
	
	RequestAsyncFuture<T> futureCallback = new RequestAsyncFuture<>(returnType, callback);
	
	BasicHttpContext httpContext = new BasicHttpContext();
	requester.execute(asyncRequestProducer, asyncResponseConsumer, pool, httpContext, futureCallback);
	
	return (callback instanceof JsonRpcFuture ? (Future<T>) callback : null);
}