Search in sources :

Example 11 with HttpClientCallbackResult

use of com.creditease.uav.httpasync.HttpClientCallbackResult 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 HttpClientCallbackResult

use of com.creditease.uav.httpasync.HttpClientCallbackResult 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 HttpClientCallbackResult

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

the class AppHubBaseRestService method doHttpPost.

/**
 * 支持普通Http Post调用和智能的带FailOver的调用
 *
 * @param postUrl
 * @param subPath
 * @param data
 * @param contentType
 * @param encoding
 * @param callBack
 */
public void doHttpPost(String serverAddress, String subPath, byte[] data, String contentType, String encoding, HttpClientCallback callBack) {
    ConnectionFailoverMgr cfm = httpConnInvokeMgr.get(serverAddress);
    /**
     * Step 1: if there is no ConnectionFailoverMgr, take it as normal http post
     */
    if (cfm == null) {
        final String postURL = (subPath != null) ? (serverAddress + subPath) : serverAddress;
        httpAsyncClient.doAsyncHttpPost(postURL, data, contentType, encoding, callBack);
        return;
    }
    /**
     * Step 2: do smart http post
     */
    String url = cfm.getConnection();
    if (url == null) {
        String msg = "No Available Address for ServerAddressList[" + serverAddress + "]";
        this.getLogger().warn(this, msg);
        if (callBack != null) {
            HttpClientCallbackResult result = new HttpClientCallbackResult(null, null);
            result.setException(new HttpAsyncException(ExceptionEvent.REPLY_ERROR, new ConnectException(msg)));
            callBack.failed(result);
        }
        return;
    }
    String postURL = (subPath != null) ? (url + subPath) : url;
    getLogger().info(this, "doHttpPost URL :" + postURL);
    PostHttpCallback postHttpCb = new PostHttpCallback();
    postHttpCb.setCallBack(callBack);
    postHttpCb.setCfm(cfm);
    postHttpCb.setContentType(contentType);
    postHttpCb.setData(data);
    postHttpCb.setEncoding(encoding);
    postHttpCb.setPostURL(postURL);
    postHttpCb.setServerAddress(serverAddress);
    postHttpCb.setSubPath(subPath);
    postHttpCb.setUrl(url);
    httpAsyncClient.doAsyncHttpPost(postURL, data, contentType, encoding, postHttpCb);
}
Also used : ConnectionFailoverMgr(com.creditease.uav.helpers.connfailover.ConnectionFailoverMgr) HttpClientCallbackResult(com.creditease.uav.httpasync.HttpClientCallbackResult) HttpAsyncException(com.creditease.uav.httpasync.HttpAsyncException) ConnectException(java.net.ConnectException)

Example 14 with HttpClientCallbackResult

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

the class AppHubInit method initApp.

private void initApp(final String id, final String appName) {
    try {
        Map<String, Object> modify = new HashMap<String, Object>();
        Map<String, Object> where = new HashMap<String, Object>();
        where.put("appid", id);
        modify.put("where", where);
        byte[] modifyData = getRequestData(manageTypeApp, createHttpMapRequest(bussinessTypeModify, modify));
        run(manageTypeApp, modifyData, new ManageTestCallback() {

            @Override
            public void completed(HttpClientCallbackResult result) {
                logger.info(this, "AppHubInit ADD APP:uavapp_" + appName);
                Map<String, Object> app1 = new HashMap<String, Object>();
                String appUrlStr = appName;
                app1.put("appid", id);
                app1.put("appurl", appUrlStr);
                app1.put("state", 1);
                app1.put("createtime", ctime);
                app1.put("operationtime", ctime);
                app1.put("operationuser", operationuser);
                byte[] addData = getRequestData(manageTypeApp, createHttpMapRequest(bussinessTypeCreate, app1));
                run(manageTypeApp, addData, new ManageTestCallback());
            }
        });
    } catch (Exception e) {
        logger.err(this, e.getMessage(), e);
        throw new ApphubException(e);
    }
}
Also used : HashMap(java.util.HashMap) ApphubException(com.creditease.uav.exception.ApphubException) HttpClientCallbackResult(com.creditease.uav.httpasync.HttpClientCallbackResult) HashMap(java.util.HashMap) Map(java.util.Map) ApphubException(com.creditease.uav.exception.ApphubException)

Example 15 with HttpClientCallbackResult

use of com.creditease.uav.httpasync.HttpClientCallbackResult 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

HttpClientCallbackResult (com.creditease.uav.httpasync.HttpClientCallbackResult)15 HttpClientCallback (com.creditease.uav.httpasync.HttpClientCallback)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)3 HashMap (java.util.HashMap)3 ConnectionFailoverMgr (com.creditease.uav.helpers.connfailover.ConnectionFailoverMgr)2 IOException (java.io.IOException)2 Map (java.util.Map)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 JSONObject (com.alibaba.fastjson.JSONObject)1 HeartBeatEvent (com.creditease.agent.heartbeat.api.HeartBeatEvent)1 ApphubException (com.creditease.uav.exception.ApphubException)1 ParaValuePair (com.creditease.uav.httpasync.api.ParaValuePair)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 ConnectException (java.net.ConnectException)1 SocketTimeoutException (java.net.SocketTimeoutException)1