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