下面列出了怎么用org.apache.http.concurrent.FutureCallback的API类实例代码及写法,或者点击链接到github查看源代码。
@Advice.OnMethodEnter(suppress = Throwable.class)
private static void onBeforeExecute(@Advice.Argument(value = 0, readOnly = false) HttpAsyncRequestProducer requestProducer,
@Advice.Argument(2) HttpContext context,
@Advice.Argument(value = 3, readOnly = false) FutureCallback futureCallback,
@Advice.Local("span") @Nullable Span span,
@Advice.Local("wrapped") boolean wrapped) {
if (tracer == null || tracer.getActive() == null) {
return;
}
final AbstractSpan<?> parent = tracer.getActive();
span = parent.createExitSpan();
if (span != null) {
span.withType(HttpClientHelper.EXTERNAL_TYPE)
.withSubtype(HttpClientHelper.HTTP_SUBTYPE)
.activate();
ApacheHttpAsyncClientHelper<HttpAsyncRequestProducer, FutureCallback<?>, HttpContext, HttpRequest> asyncHelper =
asyncHelperManager.getForClassLoaderOfClass(HttpAsyncRequestProducer.class);
TextHeaderSetter<HttpRequest> headerSetter = headerSetterHelperClassManager.getForClassLoaderOfClass(HttpRequest.class);
if (asyncHelper != null && headerSetter != null) {
requestProducer = asyncHelper.wrapRequestProducer(requestProducer, span, headerSetter);
futureCallback = asyncHelper.wrapFutureCallback(futureCallback, context, span);
wrapped = true;
}
}
}
@Override
public void fire(GetCheckpointRequest req, FutureCallback<GetCheckpointResponse> cb) {
LogUtil.logBeforeExecution(tracer, retry);
TunnelServiceApi.GetCheckpointResponse defaultResponse =
TunnelServiceApi.GetCheckpointResponse.getDefaultInstance();
asyncInvokePost(
uri,
null,
TunnelProtocolBuilder.buildGetCheckpointRequest(req),
tracer,
new GetCheckpointResponseConsumer(
ResultParserFactory.createFactory().createProtocolBufferResultParser(
defaultResponse, tracer.getTraceId()), tracer, retry, lastResult),
cb);
}
/***
* post请求
*
* @param path
* 请求路径
* @param json
* 请求内容,json格式z
* @param httpCallback
* 回调
* @return
*/
public Future<HttpResponse> post(String path, String json, FutureCallback<HttpResponse> httpCallback) {
log.debug("发送post请求,路径:{},请求内容:{}", path, json);
HttpPost httpPost = new HttpPost(getUrl(path));
if (StringUtils.isNoneBlank(json)) {
httpPost.addHeader("Content-Type", "application/json");
httpPost.setEntity(generateStringEntity(json));
}
FutureCallback<HttpResponse> responseCallback = null;
if (httpCallback != null) {
log.debug("等待完成的任务数:{}", unCompletedTaskNum.incrementAndGet());
responseCallback = new GracefulCloseFutureCallBack(unCompletedTaskNum, httpCallback);
}
return client.execute(httpPost, responseCallback);
}
private void executeCallback(HttpEntityEnclosingRequestBase request, String json, FutureCallback<HttpResponse> httpCallback) {
if (json != null && json.length() > 0) {
request.addHeader("Content-Type", "application/json");
if (!this.httpCompress) {
request.setEntity(generateStringEntity(json));
} else {
request.addHeader("Accept-Encoding", "gzip, deflate");
request.setEntity(generateGZIPCompressEntity(json));
}
}
if (authType != null && !authType.trim().equals("")) {
setAuthHeader(request);
}
FutureCallback<HttpResponse> responseCallback = null;
if (httpCallback != null) {
unCompletedTaskNum.incrementAndGet();
responseCallback = this.httpResponseCallbackFactory.wrapUpBaseHttpFutureCallback(httpCallback);
}
httpclient.execute(request, responseCallback);
}
public FutureCallback<HttpResponse> createBatchPutDataCallback(
final String address,
final AbstractBatchPutCallback<?> batchPutCallback,
final List<Point> pointList,
final Config config
) {
FutureCallback<HttpResponse> httpCallback = new BatchPutHttpResponseCallback(
address,
hitsdbHttpclient,
batchPutCallback,
pointList,
config,
config.getBatchPutRetryCount()
);
return httpCallback;
}
public FutureCallback<HttpResponse> createNoLogicBatchPutHttpFutureCallback(
final String address,
final List<Point> pointList,
final Config config,
final int batchPutRetryTimes
) {
FutureCallback<HttpResponse> httpCallback =
new BatchPutHttpResponseCallback(
address,
hitsdbHttpclient,
null,
pointList,
config,
batchPutRetryTimes
);
return httpCallback;
}
public FutureCallback<HttpResponse> createMultiFieldBatchPutDataCallback(
final String address,
final AbstractMultiFieldBatchPutCallback<?> batchPutCallback,
final List<MultiFieldPoint> pointList,
final Config config
) {
FutureCallback<HttpResponse> httpCallback = new MultiFieldBatchPutHttpResponseCallback(
address,
hitsdbHttpclient,
batchPutCallback,
pointList,
config,
config.getBatchPutRetryCount()
);
return httpCallback;
}
public FutureCallback<HttpResponse> createMultiFieldNoLogicBatchPutHttpFutureCallback(
final String address,
final List<MultiFieldPoint> pointList,
final Config config,
final int batchPutRetryTimes
) {
FutureCallback<HttpResponse> httpCallback =
new MultiFieldBatchPutHttpResponseCallback(
address,
hitsdbHttpclient,
null,
pointList,
config,
batchPutRetryTimes
);
return httpCallback;
}