Search in sources :

Example 11 with HttpClientCallback

use of com.creditease.uav.httpasync.HttpClientCallback in project uavstack by uavorg.

the class HeartBeatClientReqWorker method run.

@Override
public void run() {
    /**
     * step 1: select a heartbeat server url
     */
    final String connectStr = cfmgr.getConnection();
    if (null == connectStr) {
        log.err(this, "Select HeartBeatServer URL Fail as no available HeartBeatServer.");
        return;
    }
    String hbserverURL = "http://" + connectStr + "/heartbeat";
    if (log.isDebugEnable()) {
        log.debug(this, "Selected HeartBeatServer URL is " + hbserverURL);
    }
    /**
     * step 2: run handlers for upstream request
     */
    final HeartBeatEventClientWorker hbEventClientWorker = (HeartBeatEventClientWorker) this.getConfigManager().getComponent(this.feature, "HeartBeatEventClientWorker");
    HeartBeatEvent reqevent = new HeartBeatEvent(Stage.CLIENT_OUT);
    hbEventClientWorker.runHandlers(reqevent);
    byte[] datab = null;
    try {
        datab = reqevent.toJSONString().getBytes("utf-8");
    } catch (UnsupportedEncodingException e) {
        log.err(this, "Convert HeartBeatClientEvent into bytes Fail.", e);
        return;
    }
    /**
     * step 3: send HeartBeatClientEvent out
     */
    final CountDownLatch cdl = new CountDownLatch(1);
    client.doAsyncHttpPost(hbserverURL, datab, "application/json", "utf-8", new HttpClientCallback() {

        @Override
        public void completed(HttpClientCallbackResult result) {
            /**
             * step 4: run handlers for downstream response
             */
            handleResponse(hbEventClientWorker, result);
            // CountDownLatch unlock
            cdl.countDown();
        }

        @Override
        public void failed(HttpClientCallbackResult result) {
            /**
             * mark this hbserver is NOT OK
             */
            cfmgr.putFailConnection(connectStr);
            /**
             * step 4: run handlers for downstream response
             */
            handleResponse(hbEventClientWorker, result);
            // CountDownLatch unlock
            cdl.countDown();
        }
    });
    /**
     * step 4: wait the async http invoking result
     */
    try {
        cdl.await(60000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
    // ignore
    }
}
Also used : HeartBeatEvent(com.creditease.agent.heartbeat.api.HeartBeatEvent) HttpClientCallback(com.creditease.uav.httpasync.HttpClientCallback) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientCallbackResult(com.creditease.uav.httpasync.HttpClientCallbackResult)

Example 12 with HttpClientCallback

use of com.creditease.uav.httpasync.HttpClientCallback in project uavstack by uavorg.

the class APMRestService method queryJavaThreadAnalysis.

@POST
@Path("jta/q")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public void queryJavaThreadAnalysis(String data, @Suspended final AsyncResponse response) {
    UAVHttpMessage msg = new UAVHttpMessage(data);
    this.doHttpPost("uav.app.apm.jta.http.addr", "/jta/q", msg, new HttpClientCallback() {

        @Override
        public void completed(HttpClientCallbackResult result) {
            response.resume(result.getReplyData());
        }

        @Override
        public void failed(HttpClientCallbackResult result) {
            String reStr = result.getReplyDataAsString();
            response.resume(reStr);
            logger.err(this, "query jta http server FAILED. retCode=" + result.getRetCode() + ", msg=" + reStr, result.getException());
        }
    });
}
Also used : HttpClientCallback(com.creditease.uav.httpasync.HttpClientCallback) UAVHttpMessage(com.creditease.agent.http.api.UAVHttpMessage) HttpClientCallbackResult(com.creditease.uav.httpasync.HttpClientCallbackResult) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 13 with HttpClientCallback

use of com.creditease.uav.httpasync.HttpClientCallback in project uavstack by uavorg.

the class HttpCallAction method run.

@Override
public boolean run(NotificationEvent event) {
    final AtomicBoolean bool = new AtomicBoolean(true);
    final String urlstr = event.getArg(cName);
    String[] urls = urlstr.split(",");
    final CountDownLatch cdl = new CountDownLatch(urls.length);
    for (final String url : urls) {
        try {
            final String eventStr = event.toJSONString();
            if (this.log.isDebugEnable()) {
                this.log.debug(this, "HttpCallAction START:url=" + url + ",event=" + eventStr);
            }
            client.doAsyncHttpPost(url, eventStr.getBytes("utf-8"), "application/json", "utf-8", new HttpClientCallback() {

                @Override
                public void completed(HttpClientCallbackResult result) {
                    bool.set(true & bool.get());
                    cdl.countDown();
                    if (log.isDebugEnable()) {
                        log.debug(this, "HttpCallAction END:url=" + url + ",event=" + eventStr);
                    }
                }

                @Override
                public void failed(HttpClientCallbackResult result) {
                    bool.set(false & bool.get());
                    cdl.countDown();
                    if (log.isTraceEnable()) {
                        log.err(this, "HttpCallAction FAIL:retCode=" + result.getRetCode() + ",url=" + url + ",event=" + eventStr);
                    }
                }
            });
        } catch (UnsupportedEncodingException e) {
        // ignore
        }
    }
    try {
        cdl.await(3000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
    // ignore
    }
    return bool.get();
}
Also used : HttpClientCallback(com.creditease.uav.httpasync.HttpClientCallback) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientCallbackResult(com.creditease.uav.httpasync.HttpClientCallbackResult)

Aggregations

HttpClientCallback (com.creditease.uav.httpasync.HttpClientCallback)13 HttpClientCallbackResult (com.creditease.uav.httpasync.HttpClientCallbackResult)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 UAVHttpMessage (com.creditease.agent.http.api.UAVHttpMessage)5 HttpAsyncException (com.creditease.uav.httpasync.HttpAsyncException)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 JSONObject (com.alibaba.fastjson.JSONObject)1 HeartBeatEvent (com.creditease.agent.heartbeat.api.HeartBeatEvent)1 ConnectionFailoverMgr (com.creditease.uav.helpers.connfailover.ConnectionFailoverMgr)1 ParaValuePair (com.creditease.uav.httpasync.api.ParaValuePair)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 SocketTimeoutException (java.net.SocketTimeoutException)1 Date (java.sql.Date)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1