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
}
}
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());
}
});
}
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);
}
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);
}
}
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();
}
Aggregations