use of com.creditease.uav.helpers.connfailover.ConnectionFailoverMgr 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);
}
}
use of com.creditease.uav.helpers.connfailover.ConnectionFailoverMgr in project uavstack by uavorg.
the class AbstractSystemInvoker method uninstallServiceRoute.
/**
* uninstallServiceRoute
*
* @param serviceName
*/
protected void uninstallServiceRoute(String serviceName) {
if (StringHelper.isEmpty(serviceName)) {
return;
}
ConnectionFailoverMgr cfm = connectionMgrPool.remove(serviceName);
if (cfm == null) {
return;
}
ServiceDiscoveryState sds = serviceDiscoveryStates.get(serviceName);
if (sds != null) {
sds.recordDiscardTime();
sds.setState(ServiceDiscoveryState.State.DISCARD);
}
}
use of com.creditease.uav.helpers.connfailover.ConnectionFailoverMgr in project uavstack by uavorg.
the class AbstractSystemInvoker method installServiceRoute.
/**
* installServiceRoute
*
* @param serviceName
* @param urls
* @param retryInterval
*/
protected ConnectionFailoverMgr installServiceRoute(String serviceName, String[] urls, long retryInterval) {
if (StringHelper.isEmpty(serviceName) || urls == null || urls.length == 0) {
return null;
}
ConnectionFailoverMgr cfm = connectionMgrPool.get(serviceName);
if (cfm == null) {
cfm = new ConnectionFailoverMgr(Arrays.asList(urls), retryInterval);
connectionMgrPool.put(serviceName, cfm);
} else {
cfm.reset(urls);
cfm.setRetryInterval(retryInterval);
}
ServiceDiscoveryState sds = serviceDiscoveryStates.get(serviceName);
if (sds != null) {
sds.setState(ServiceDiscoveryState.State.WORK);
}
return cfm;
}
use of com.creditease.uav.helpers.connfailover.ConnectionFailoverMgr in project uavstack by uavorg.
the class AbstractSystemInvoker method getServiceRoute.
/**
* 所有AbstractSystemInvoker派生类应该使用该方法获取ConnectionFailoverMgr
*
* @param serviceName
* @return
*/
protected ConnectionFailoverMgr getServiceRoute(String serviceName) {
if (!serviceDiscoveryStates.containsKey(serviceName)) {
ServiceDiscoveryState sds = new ServiceDiscoveryState();
serviceDiscoveryStates.put(serviceName, sds);
}
ConnectionFailoverMgr cfm = this.connectionMgrPool.get(serviceName);
if (cfm != null) {
return cfm;
}
ServiceDiscoveryState sds = serviceDiscoveryStates.get(serviceName);
if (sds.canDoServiceDiscovery() == false) {
return null;
}
cfm = accessServiceRoute(serviceName);
if (sds.getState() == ServiceDiscoveryState.State.INIT && cfm == null) {
sds.recordDiscardTime();
sds.setState(ServiceDiscoveryState.State.DISCARD);
}
return cfm;
}
use of com.creditease.uav.helpers.connfailover.ConnectionFailoverMgr in project uavstack by uavorg.
the class AbstractSystemInvoker method accessServiceRoute.
/**
* 从心跳查询服务获取服务路由
*
* @param serviceName
* @return
*/
private ConnectionFailoverMgr accessServiceRoute(String serviceName) {
AgentFeatureComponent afc = (AgentFeatureComponent) this.getConfigManager().getComponent("hbclientagent", "HeartBeatClientAgent");
if (afc == null) {
return null;
}
ConnectionFailoverMgr cfm = null;
String[] urls = (String[]) afc.exchange("hbclientagent.service.discovery", serviceName);
if (urls != null) {
cfm = this.installServiceRoute(serviceName, urls, 5000);
}
return cfm;
}
Aggregations