Search in sources :

Example 1 with UAVHttpMessage

use of com.creditease.agent.http.api.UAVHttpMessage in project uavstack by uavorg.

the class ThreadAnalysisAction method doAction.

@Override
public void doAction(ActionContext context) throws Exception {
    try {
        UAVHttpMessage data = (UAVHttpMessage) context.getParam("msg");
        if (!controlConcurrency(data)) {
            data.putResponse("rs", "ERR");
            data.putResponse("msg", "ERR:THREAD DUMP IS RUNNING");
            return;
        }
        String user = data.getRequest("user");
        if (StringHelper.isEmpty(user)) {
            user = "UNKNOWN";
        }
        String url = data.getRequest("server") + SERVICE_POSTFIX;
        if (!url.startsWith("http")) {
            url = "http://" + url;
        }
        String param = data.getRequest("actparam");
        @SuppressWarnings("unchecked") Map<String, Object> paramMap = JSONHelper.toObject(param, Map.class);
        @SuppressWarnings("unchecked") List<Object> paramsList = (List<Object>) paramMap.get("param");
        paramsList.add(this.dumpFileDirectory);
        paramMap.put("param", paramsList);
        ActionContext ac = new ActionContext();
        ac.putParam("user", user);
        ac.putParam("url", url);
        ac.putParam("paramMap", paramMap);
        if ("true".equals(data.getRequest("multiple"))) {
            ac.putParam("multiple", true);
            int duration = DataConvertHelper.toInt(data.getRequest("duration"), 0);
            int interval = DataConvertHelper.toInt(data.getRequest("interval"), 5);
            int times = duration / interval + 1;
            ac.putParam("times", times);
            ac.putParam("suspendTime", interval * 1000);
        } else {
            ac.putParam("multiple", false);
            ac.putParam("times", 1);
            ac.putParam("suspendTime", 0);
        }
        ac = getActionEngineMgr().getActionEngine("JTAActionEngine").execute("DumpThreadAction", ac);
        String ret = (String) ac.getParam("msg");
        if (ret.contains("ERR:")) {
            data.putResponse("rs", "ERR");
            data.putResponse("msg", ret);
        } else {
            data.putResponse("rs", "OK");
            data.putResponse("msg", ret);
        }
    } catch (Exception e) {
        log.err(this, "do thread analysis FAILED.", e);
        throw e;
    }
}
Also used : List(java.util.List) UAVHttpMessage(com.creditease.agent.http.api.UAVHttpMessage) ActionContext(com.creditease.agent.spi.ActionContext)

Example 2 with UAVHttpMessage

use of com.creditease.agent.http.api.UAVHttpMessage in project uavstack by uavorg.

the class PushNotifyEventAction method run.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public boolean run(NotificationEvent event) {
    AbstractSystemInvoker invoker = this.getSystemInvokerMgr().getSystemInvoker(InvokerType.HTTP);
    UAVHttpMessage tmsg = new UAVHttpMessage();
    tmsg.setIntent("ntfpush");
    tmsg.putRequest("ntfevent", event.toJSONString());
    String serviceStr = this.getConfigManager().getFeatureConfiguration(this.feature, "push.services");
    if (StringHelper.isEmpty(serviceStr)) {
        return true;
    }
    String[] services = serviceStr.split(",");
    boolean check = true;
    for (String service : services) {
        try {
            Map<String, Object> res = (Map<String, Object>) invoker.invoke(service, tmsg, Map.class);
            if (res == null) {
                check = false;
                continue;
            }
            if (log.isTraceEnable()) {
                log.info(this, "Push Notification to Service[" + service + "] SUCCESS.");
            }
            return true;
        } catch (Exception e) {
            check = false;
        // ignore
        }
    }
    return check;
}
Also used : UAVHttpMessage(com.creditease.agent.http.api.UAVHttpMessage) Map(java.util.Map) AbstractSystemInvoker(com.creditease.agent.spi.AbstractSystemInvoker)

Example 3 with UAVHttpMessage

use of com.creditease.agent.http.api.UAVHttpMessage in project uavstack by uavorg.

the class NCHttpServerWorker method adaptRequest.

@Override
protected UAVHttpMessage adaptRequest(HttpMessage message) {
    String messageBody = message.getRequestBodyAsString("UTF-8");
    if (log.isDebugEnable()) {
        log.debug(this, "NCHttpServerWorker Request: " + messageBody);
    }
    UAVHttpMessage msg = new UAVHttpMessage(messageBody);
    return msg;
}
Also used : UAVHttpMessage(com.creditease.agent.http.api.UAVHttpMessage)

Example 4 with UAVHttpMessage

use of com.creditease.agent.http.api.UAVHttpMessage in project uavstack by uavorg.

the class HttpSystemInvoker method invoke.

/**
 * invoke
 *
 * @param serviceName
 *            服务名
 * @param serviceSubPath
 *            服务相对路径(可选)
 * @param msg
 *            提交msg对象
 * @param callback
 *            异步回调(可选)
 * @param syncTimeout
 *            同步等待超时(可选)
 * @param returnClass
 *            同步返回对象类型(可选)
 * @return
 */
private Object invoke(final String serviceName, final String serviceSubPath, final Object msg, final HttpClientCallback callback, final long syncTimeout, final Class<?> returnClass) {
    if (StringHelper.isEmpty(serviceName)) {
        return null;
    }
    // step 1: get service URI
    final ConnectionFailoverMgr cfm = this.getServiceRoute(serviceName);
    if (cfm == null) {
        throw new RuntimeException("NoSuchServiceExist:" + serviceName);
    }
    final String serviceURL = cfm.getConnection();
    if (serviceURL == null) {
        throw new RuntimeException("NoAvailableServiceInstance:" + serviceName);
    }
    String finalServiceURL = serviceURL;
    if (serviceSubPath != null) {
        finalServiceURL = serviceURL + serviceSubPath;
    }
    // step 2: convert request Object to byte
    byte[] data = null;
    // byte[]
    if (msg.getClass().isArray() && msg instanceof byte[]) {
        data = (byte[]) msg;
    } else // UAVHttpMessage
    if (UAVHttpMessage.class.isAssignableFrom(msg.getClass())) {
        try {
            data = JSONHelper.toString(msg).getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    } else // String
    if (String.class.isAssignableFrom(msg.getClass())) {
        try {
            data = msg.toString().getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
    final SyncResult syncResult = new SyncResult();
    // step 3: do post invoke
    httpAsyncClient.doAsyncHttpPost(finalServiceURL, data, "application/json", "utf-8", new HttpClientCallback() {

        @Override
        public void completed(HttpClientCallbackResult result) {
            if (callback != null) {
                callback.completed(result);
            }
            if (syncTimeout <= 0) {
                return;
            }
            if (returnClass != null) {
                syncResult.setSdata(result.getReplyDataAsString());
            } else {
                syncResult.setBdata(result.getResulDataAsByteArray());
            }
        }

        @Override
        public void failed(HttpClientCallbackResult result) {
            Exception exception = result.getException();
            /**
             * Auto Failover
             */
            if (getDefFailoverCondition(exception)) {
                cfm.putFailConnection(serviceURL);
                invoke(serviceName, serviceSubPath, msg, callback);
                return;
            }
            if (callback != null) {
                callback.failed(result);
            }
            if (syncTimeout <= 0) {
                return;
            }
            HttpAsyncException excep = result.getException();
            if (excep != null) {
                syncResult.setE(excep);
            }
        }
    });
    // step 4(optional): if sync mode, we should wait
    if (syncTimeout <= 0) {
        return null;
    }
    syncResult.waitSync(syncTimeout);
    if (syncResult.getE() != null) {
        throw new RuntimeException(syncResult.getE());
    }
    if (returnClass == null) {
        return syncResult.getBdata();
    }
    if (String.class.isAssignableFrom(returnClass)) {
        return syncResult.getSdata();
    } else if (UAVHttpMessage.class.isAssignableFrom(returnClass)) {
        return new UAVHttpMessage(syncResult.getSdata());
    } else {
        return JSONHelper.toObject(syncResult.getSdata(), returnClass);
    }
}
Also used : HttpClientCallback(com.creditease.uav.httpasync.HttpClientCallback) ConnectionFailoverMgr(com.creditease.uav.helpers.connfailover.ConnectionFailoverMgr) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UAVHttpMessage(com.creditease.agent.http.api.UAVHttpMessage) HttpClientCallbackResult(com.creditease.uav.httpasync.HttpClientCallbackResult) HttpAsyncException(com.creditease.uav.httpasync.HttpAsyncException) SocketTimeoutException(java.net.SocketTimeoutException) HttpAsyncException(com.creditease.uav.httpasync.HttpAsyncException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with UAVHttpMessage

use of com.creditease.agent.http.api.UAVHttpMessage in project uavstack by uavorg.

the class DoTestNodeOperCtrl method main.

public static void main(String[] args) {
    HttpAsyncClient.build(5, 5);
    UAVHttpMessage msg = new UAVHttpMessage();
    doTestSysPro(msg);
    installMOF(msg);
// try {
// System.in.read();
// }
// catch (IOException e) {
// 
// }
// 
// uninstallMOF(msg);
}
Also used : UAVHttpMessage(com.creditease.agent.http.api.UAVHttpMessage)

Aggregations

UAVHttpMessage (com.creditease.agent.http.api.UAVHttpMessage)58 Path (javax.ws.rs.Path)25 Produces (javax.ws.rs.Produces)25 POST (javax.ws.rs.POST)22 HashMap (java.util.HashMap)14 HttpClientCallback (com.creditease.uav.httpasync.HttpClientCallback)5 HttpClientCallbackResult (com.creditease.uav.httpasync.HttpClientCallbackResult)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 ApphubException (com.creditease.uav.exception.ApphubException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 IOException (java.io.IOException)3 SimpleDateFormat (java.text.SimpleDateFormat)3 GET (javax.ws.rs.GET)3 AbstractSystemInvoker (com.creditease.agent.spi.AbstractSystemInvoker)2 AgentFeatureComponent (com.creditease.agent.spi.AgentFeatureComponent)2 BASE64DecoderUrl (com.creditease.uav.helpers.url.BASE64DecoderUrl)2 Date (java.util.Date)2