下面列出了怎么用org.apache.http.nio.protocol.BasicAsyncRequestProducer的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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);
}