Search in sources :

Example 1 with NettyClient

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;
}
Also used : NettyClient(org.msec.net.NettyClient) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with NettyClient

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;
}
Also used : NettyClient(org.msec.net.NettyClient) Route(api.lb.msec.org.Route)

Example 3 with NettyClient

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;
}
Also used : NettyClient(org.msec.net.NettyClient) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with NettyClient

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;
}
Also used : NettyClient(org.msec.net.NettyClient) MessageLite(com.google.protobuf.MessageLite) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

NettyClient (org.msec.net.NettyClient)4 IOException (java.io.IOException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Route (api.lb.msec.org.Route)1 MessageLite (com.google.protobuf.MessageLite)1