use of org.msec.net.NettyClient in project MSEC by Tencent.
the class ServiceFactory method callMethod.
// ������ʹ�ó����ӣ�������ʵ��SEQ���ƣ��Է�����
public static byte[] callMethod(String moduleName, String serviceMethodName, CustomPackageCodec packageCodec, int timeoutMillis) throws Exception {
byte[] ret = null;
NettyClient client = null;
try {
client = ClientManager.getClient(moduleName, true);
} catch (Exception e) {
log.error("get route for [" + moduleName + "] failed.", e);
return ret;
}
int pos = serviceMethodName.lastIndexOf('.');
if (pos == -1 || pos == 0 || pos == serviceMethodName.length() - 1) {
throw new Exception("Invalid serviceMethodName. Must be in format like *.*");
}
String serviceName = serviceMethodName.substring(0, pos);
String methodName = serviceMethodName.substring(pos + 1);
RpcRequest rpcRequest = new RpcRequest();
rpcRequest.setSeq(generateSequence());
rpcRequest.setServiceName(serviceName);
rpcRequest.setMethodName(methodName);
rpcRequest.setParameter(null);
RpcResponse rpcResponse = client.sendRequestAndWaitResponse(packageCodec, rpcRequest, timeoutMillis);
if (rpcResponse != null) {
if (rpcResponse.getErrno() == 0) {
ret = (byte[]) rpcResponse.getResultObj();
return ret;
} else if (rpcResponse.getError() != null) {
throw rpcResponse.getError();
} else {
throw new Exception("Rpc failed: errno = " + rpcResponse.getErrno());
}
}
return ret;
}
use of org.msec.net.NettyClient in project MSEC by Tencent.
the class ClientManager method getClient.
public static NettyClient getClient(String serviceName, boolean fromCache) throws Exception {
Route route = new Route();
if (!accessLB.getroutebyname(serviceName, route)) {
log.error("getRouteByName failed: " + serviceName);
AccessMonitor.add("frm.rpc route to " + serviceName + " failed");
return null;
}
AccessMonitor.add("frm.rpc route to " + serviceName + " succ.");
NettyClient client = null;
if (fromCache) {
client = clientCache.get(route);
}
log.info("getRouteByName " + serviceName + " addr: " + route.getIp() + ":" + route.getPort() + " incache: " + (client != null) + " connected: " + ((client != null) && client.isConnected()));
if (client == null) {
client = new NettyClient(route.getIp(), route.getPort());
clientCache.put(route, client);
}
if (!client.isConnected()) {
client.connect();
AccessMonitor.add("frm.rpc new tcp connection");
}
if (!client.isConnected()) {
AccessMonitor.add("frm.rpc new tcp connection failed.");
throw new RuntimeException("Connection failure to " + route.getIp() + ":" + route.getPort());
}
return client;
}
use of org.msec.net.NettyClient in project MSEC by Tencent.
the class ServiceFactory method callMethod.
// ������ʹ�ö�����
public static byte[] callMethod(String moduleName, String serviceMethodName, CustomPackageLengthChecker lengthChecker, byte[] requestData, int timeoutMillis) throws Exception {
byte[] ret = null;
NettyClient client = null;
try {
client = ClientManager.getClient(moduleName, false);
} catch (Exception e) {
log.error("get route for [" + moduleName + "] failed.", e);
return ret;
}
// System.out.println("AfterConnect: " + System.currentTimeMillis());
int pos = serviceMethodName.lastIndexOf('.');
if (pos == -1 || pos == 0 || pos == serviceMethodName.length() - 1) {
throw new Exception("Invalid serviceMethodName. Must be in format like *.*");
}
String serviceName = serviceMethodName.substring(0, pos);
String methodName = serviceMethodName.substring(pos + 1);
RpcRequest rpcRequest = new RpcRequest();
rpcRequest.setSeq(generateSequence());
rpcRequest.setServiceName(serviceName);
rpcRequest.setMethodName(methodName);
rpcRequest.setParameter(null);
RpcResponse rpcResponse = client.sendRequestAndWaitResponse(lengthChecker, requestData, rpcRequest, timeoutMillis);
// System.out.println("WaitFinished: " + System.currentTimeMillis());
client.close();
if (rpcResponse != null) {
if (rpcResponse.getErrno() == 0) {
ret = (byte[]) rpcResponse.getResultObj();
return ret;
} else if (rpcResponse.getError() != null) {
throw rpcResponse.getError();
} else {
throw new Exception("Rpc failed: errno = " + rpcResponse.getErrno());
}
}
return ret;
}
use of org.msec.net.NettyClient in project MSEC by Tencent.
the class ServiceFactory method callMethod.
public static MessageLite callMethod(String moduleName, String serviceMethodName, MessageLite request, MessageLite responseInstance, int timeoutMillis) throws Exception {
MessageLite ret = null;
NettyClient client = null;
// System.out.println("GetClient begin: " + System.currentTimeMillis());
try {
client = ClientManager.getClient(moduleName, true);
} catch (Exception e) {
log.error("get route for [" + moduleName + "] failed.", e);
return ret;
}
// System.out.println("AfterConnect: " + System.currentTimeMillis());
int pos = serviceMethodName.lastIndexOf('.');
if (pos == -1 || pos == 0 || pos == serviceMethodName.length() - 1) {
throw new Exception("Invalid serviceMethodName. Must be in format like *.*");
}
String serviceName = serviceMethodName.substring(0, pos);
String methodName = serviceMethodName.substring(pos + 1);
RpcRequest rpcRequest = new RpcRequest();
rpcRequest.setSeq(generateSequence());
rpcRequest.setServiceName(serviceName);
rpcRequest.setMethodName(methodName);
rpcRequest.setParameter(request);
RpcContext context = (RpcContext) RequestProcessor.getThreadContext("session");
if (context != null) {
rpcRequest.setFlowid(context.getRequest().getFlowid());
}
RpcResponse rpcResponse = client.sendRequestAndWaitResponse(rpcRequest, timeoutMillis);
if (rpcResponse != null) {
if (rpcResponse.getErrno() == 0) {
ret = responseInstance.getParserForType().parseFrom((byte[]) rpcResponse.getResultObj());
return ret;
} else if (rpcResponse.getError() != null) {
throw rpcResponse.getError();
} else {
throw new Exception("Rpc failed: errno = " + rpcResponse.getErrno());
}
}
return ret;
}
Aggregations