Search in sources :

Example 1 with HttpAsyncResponseConsumer

use of org.apache.http.nio.protocol.HttpAsyncResponseConsumer in project uavstack by uavorg.

the class ApacheAsyncHttpClientIT method makeConsumer.

@SuppressWarnings("rawtypes")
public HttpAsyncResponseConsumer makeConsumer(final HttpAsyncResponseConsumer r) {
    return JDKProxyInvokeUtil.newProxyInstance(HttpContext.class.getClassLoader(), new Class<?>[] { HttpAsyncResponseConsumer.class }, new JDKProxyInvokeHandler<HttpAsyncResponseConsumer>(r, new JDKProxyInvokeProcessor<HttpAsyncResponseConsumer>() {

        @Override
        public void preProcess(HttpAsyncResponseConsumer t, Object proxy, Method method, Object[] args) {
        }

        @Override
        public Object postProcess(Object res, HttpAsyncResponseConsumer t, Object proxy, Method method, Object[] args) {
            if (method.getName().equals("responseReceived")) {
                HttpResponse response = (HttpResponse) args[0];
                Header sheader = response.getLastHeader("Server");
                if (sheader != null) {
                    targetServer = sheader.getValue();
                }
            }
            return null;
        }
    }));
}
Also used : JDKProxyInvokeProcessor(com.creditease.monitor.proxy.spi.JDKProxyInvokeProcessor) Header(org.apache.http.Header) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) HttpAsyncResponseConsumer(org.apache.http.nio.protocol.HttpAsyncResponseConsumer) Method(java.lang.reflect.Method)

Example 2 with HttpAsyncResponseConsumer

use of org.apache.http.nio.protocol.HttpAsyncResponseConsumer in project uavstack by uavorg.

the class ApacheAsyncHttpClientIT method doAsyncStart.

/**
 * for async http client
 *
 * @param args
 * @return
 */
@SuppressWarnings({ "rawtypes", "unused", "unchecked" })
public FutureCallback doAsyncStart(Object[] args) {
    HttpAsyncRequestProducer requestProducer = null;
    HttpAsyncResponseConsumer responseConsumer = null;
    HttpContext context = null;
    FutureCallback callback = null;
    Map mObj = null;
    if (args.length == 4) {
        requestProducer = (HttpAsyncRequestProducer) args[0];
        responseConsumer = (HttpAsyncResponseConsumer) args[1];
        context = (HttpContext) args[2];
        callback = (FutureCallback) args[3];
    } else if (args.length == 5) {
        requestProducer = (HttpAsyncRequestProducer) args[1];
        responseConsumer = (HttpAsyncResponseConsumer) args[2];
        context = (HttpContext) args[3];
        callback = (FutureCallback) args[4];
    }
    String httpAction = null;
    try {
        HttpRequest hr = requestProducer.generateRequest();
        /**
         * 呵呵,就是把UAV的客户端标记加到http header里面,接收方就知道是哪个东东调用的了,便于实现来源快速匹配,这个模式只适合直连模式
         *
         * 对于代理模式,只匹配代理源即可
         */
        hr.addHeader("UAV-Client-Src", MonitorServerUtil.getUAVClientSrc(this.applicationId));
        RequestLine rl = hr.getRequestLine();
        httpAction = rl.getMethod();
        targetURL = rl.getUri();
    } catch (IOException e) {
        // ignore thie exception
        return null;
    } catch (HttpException e) {
        // ignore thie exception
        return null;
    }
    Map<String, Object> params = new HashMap<String, Object>();
    params.put(CaptureConstants.INFO_CLIENT_REQUEST_URL, targetURL);
    params.put(CaptureConstants.INFO_CLIENT_REQUEST_ACTION, httpAction);
    params.put(CaptureConstants.INFO_CLIENT_APPID, this.applicationId);
    params.put(CaptureConstants.INFO_CLIENT_TYPE, "apache.http.AsyncClient");
    if (logger.isDebugable()) {
        logger.debug("Invoke START:" + targetURL + "," + httpAction + "," + this.applicationId, null);
    }
    /**
     * for async, as not in the same thread
     */
    ccMap = UAVServer.instance().runMonitorAsyncCaptureOnServerCapPoint(CaptureConstants.CAPPOINT_APP_CLIENT, Monitor.CapturePhase.PRECAP, params, null);
    // register invokechain adapter
    UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter", "registerAdapter", ApacheAsyncHttpClientAdapter.class);
    ccMap4Chain = (Map<String, Object>) UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.InvokeChainSupporter", "runCap", InvokeChainConstants.CHAIN_APP_CLIENT, InvokeChainConstants.CapturePhase.PRECAP, params, ApacheAsyncHttpClientAdapter.class, args);
    if (callback == null) {
        return null;
    }
    callback = JDKProxyInvokeUtil.newProxyInstance(HttpContext.class.getClassLoader(), new Class<?>[] { FutureCallback.class }, new JDKProxyInvokeHandler<FutureCallback>(callback, new FutureCallbackProxyInvokeProcessor(ccMap4Chain)));
    return callback;
}
Also used : HttpRequest(org.apache.http.HttpRequest) HashMap(java.util.HashMap) JDKProxyInvokeHandler(com.creditease.monitor.proxy.spi.JDKProxyInvokeHandler) HttpContext(org.apache.http.protocol.HttpContext) HttpAsyncResponseConsumer(org.apache.http.nio.protocol.HttpAsyncResponseConsumer) IOException(java.io.IOException) RequestLine(org.apache.http.RequestLine) HttpAsyncRequestProducer(org.apache.http.nio.protocol.HttpAsyncRequestProducer) HttpException(org.apache.http.HttpException) HashMap(java.util.HashMap) Map(java.util.Map) FutureCallback(org.apache.http.concurrent.FutureCallback)

Aggregations

HttpAsyncResponseConsumer (org.apache.http.nio.protocol.HttpAsyncResponseConsumer)2 HttpContext (org.apache.http.protocol.HttpContext)2 JDKProxyInvokeHandler (com.creditease.monitor.proxy.spi.JDKProxyInvokeHandler)1 JDKProxyInvokeProcessor (com.creditease.monitor.proxy.spi.JDKProxyInvokeProcessor)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Header (org.apache.http.Header)1 HttpException (org.apache.http.HttpException)1 HttpRequest (org.apache.http.HttpRequest)1 HttpResponse (org.apache.http.HttpResponse)1 RequestLine (org.apache.http.RequestLine)1 FutureCallback (org.apache.http.concurrent.FutureCallback)1 HttpAsyncRequestProducer (org.apache.http.nio.protocol.HttpAsyncRequestProducer)1