Search in sources :

Example 1 with HeartBeatEvent

use of com.creditease.agent.heartbeat.api.HeartBeatEvent in project uavstack by uavorg.

the class HeartBeatServerListenWorker method adaptRequest.

@Override
protected HeartBeatEvent adaptRequest(HttpMessage message) {
    String payload = message.getRequestBodyAsString("utf-8");
    HeartBeatEvent event = new HeartBeatEvent(HeartBeatEvent.Stage.SERVER_IN, payload);
    this.runHandlers(event);
    event.setStage(HeartBeatEvent.Stage.SERVER_OUT);
    return event;
}
Also used : HeartBeatEvent(com.creditease.agent.heartbeat.api.HeartBeatEvent)

Example 2 with HeartBeatEvent

use of com.creditease.agent.heartbeat.api.HeartBeatEvent 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 3 with HeartBeatEvent

use of com.creditease.agent.heartbeat.api.HeartBeatEvent in project uavstack by uavorg.

the class HeartBeatClientReqWorker method handleResponse.

protected void handleResponse(HeartBeatEventClientWorker hbEventClientWorker, HttpClientCallbackResult result) {
    /**
     * step 4.1: check if there is exception
     */
    HttpAsyncException hae = result.getException();
    if (null != hae) {
        log.err(this, "Receive HeartBeatClientEvent Fail: StatusCode=" + result.getRetCode() + ",ExceptionEvent=" + hae.getExceptionEvent(), hae.getCause());
        return;
    }
    /**
     * step 4.2: put the HeartBeatClientEvent into downstream queue
     */
    String respStr = result.getReplyDataAsString();
    HeartBeatEvent respEvent = new HeartBeatEvent(Stage.CLIENT_IN, respStr);
    respEvent.setStage(Stage.CLIENT_IN);
    hbEventClientWorker.putData(respEvent);
}
Also used : HeartBeatEvent(com.creditease.agent.heartbeat.api.HeartBeatEvent) HttpAsyncException(com.creditease.uav.httpasync.HttpAsyncException)

Aggregations

HeartBeatEvent (com.creditease.agent.heartbeat.api.HeartBeatEvent)3 HttpAsyncException (com.creditease.uav.httpasync.HttpAsyncException)1 HttpClientCallback (com.creditease.uav.httpasync.HttpClientCallback)1 HttpClientCallbackResult (com.creditease.uav.httpasync.HttpClientCallbackResult)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1