Search in sources :

Example 6 with ServiceBase

use of j.service.server.ServiceBase in project JFramework by gugumall.

the class JRouterImpl method getAllServiceNodeAvailable.

/*
	 * (non-Javadoc)
	 * @see j.service.router.JRouter#getAllServiceNode(java.lang.String, java.lang.String, java.lang.String)
	 */
public ServiceBase[] getAllServiceNodeAvailable(String clientUuid, String clusterCode, String md54Routing) throws RemoteException {
    waitWhileMonitoring();
    if (Constants.PRIVICY_PUBLIC.equalsIgnoreCase(routerConfig.getPrivacy())) {
    // is public, nothing to do
    } else if (Constants.PRIVICY_MD5.equalsIgnoreCase(routerConfig.getPrivacy())) {
        Client client = routerConfig.getClient(clientUuid);
        if (client == null) {
            log.log("client " + clientUuid + " is not exists.", Logger.LEVEL_DEBUG);
            throw new RemoteException(Constants.AUTH_FAILED);
        }
        String md5 = "";
        md5 += clientUuid;
        md5 += client.getKey();
        md5 = JUtilMD5.MD5EncodeToHex(md5);
        if (!md5.equalsIgnoreCase(md54Routing)) {
            throw new RemoteException(Constants.AUTH_FAILED);
        }
    } else {
        // 未实现的隐私策略
        throw new RemoteException(Constants.AUTH_FAILED);
    }
    ConcurrentMap servantsOfCluster = (ConcurrentMap) servantsOfClusters.get(clusterCode);
    if (servantsOfCluster == null) {
        throw new RemoteException("the service(rmi) " + clusterCode + " is not found.");
    }
    List nodes = servantsOfCluster.listValues();
    if (nodes.size() == 0) {
        throw new RemoteException("no valid node for the service(rmi) " + clusterCode + ".");
    }
    return (ServiceBase[]) nodes.toArray(new ServiceBase[nodes.size()]);
}
Also used : ServiceBase(j.service.server.ServiceBase) ConcurrentMap(j.util.ConcurrentMap) List(java.util.List) ConcurrentList(j.util.ConcurrentList) Client(j.service.Client) HttpClient(org.apache.http.client.HttpClient) RemoteException(java.rmi.RemoteException)

Example 7 with ServiceBase

use of j.service.server.ServiceBase in project JFramework by gugumall.

the class JRouterImpl method service.

/*
	 *  (non-Javadoc)
	 * @see j.service.router.JRouter#service(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
public ServiceBase service(String clientUuid, String clusterCode, String md54Routing) throws RemoteException {
    waitWhileMonitoring();
    if (Constants.PRIVICY_PUBLIC.equalsIgnoreCase(routerConfig.getPrivacy())) {
    // is public, nothing to do
    } else if (Constants.PRIVICY_MD5.equalsIgnoreCase(routerConfig.getPrivacy())) {
        Client client = routerConfig.getClient(clientUuid);
        if (client == null) {
            log.log("client " + clientUuid + " is not exists.", Logger.LEVEL_DEBUG);
            throw new RemoteException(Constants.AUTH_FAILED);
        }
        String md5 = "";
        md5 += clientUuid;
        md5 += client.getKey();
        md5 = JUtilMD5.MD5EncodeToHex(md5);
        if (!md5.equalsIgnoreCase(md54Routing)) {
            throw new RemoteException(Constants.AUTH_FAILED);
        }
    } else {
        // 未实现的隐私策略
        throw new RemoteException(Constants.AUTH_FAILED);
    }
    String codeOfUuid = null;
    for (int i = 0; i < nodeInfoList.size(); i++) {
        // 按uuid获得
        String[] node = ((String) nodeInfoList.get(i)).split(",");
        if (node[0].equals(clusterCode)) {
            codeOfUuid = node[4];
            break;
        }
    }
    ConcurrentMap servantsOfCluster = (ConcurrentMap) servantsOfClusters.get(codeOfUuid == null ? clusterCode : codeOfUuid);
    if (servantsOfCluster == null) {
        throw new RemoteException("the service(rmi) " + clusterCode + " is not found.");
    }
    List nodes = servantsOfCluster.listValues();
    if (nodes.size() == 0) {
        throw new RemoteException("no valid node for the service(rmi) " + clusterCode + ".");
    }
    if (servantsOfCluster.containsKey(clusterCode)) {
        // 按uuid获得
        nodes.clear();
        nodes = null;
        return (ServiceBase) servantsOfCluster.get(clusterCode);
    } else {
        ServiceBase servant = (ServiceBase) nodes.get(JUtilRandom.nextInt(nodes.size()));
        nodes.clear();
        nodes = null;
        return servant;
    }
}
Also used : ServiceBase(j.service.server.ServiceBase) ConcurrentMap(j.util.ConcurrentMap) List(java.util.List) ConcurrentList(j.util.ConcurrentList) Client(j.service.Client) HttpClient(org.apache.http.client.HttpClient) RemoteException(java.rmi.RemoteException)

Example 8 with ServiceBase

use of j.service.server.ServiceBase in project JFramework by gugumall.

the class RouterAgent method serviceNodesRmi.

/**
 * RouterManager通过均衡策略选择本路由节点代理,调用此方法从关联路由节点获得服务入口
 * @param clientUuid
 * @param code
 * @param md54Routing
 * @return
 * @throws Exception
 */
protected ServiceBase[] serviceNodesRmi(String clientUuid, String code, String md54Routing) throws Exception {
    String cacheKey = "rmi|" + clientUuid + "|" + code + "|nodes";
    if (cache.containsKey(cacheKey)) {
        return (ServiceBase[]) cache.get(cacheKey);
    }
    if (this.routerConfig.getRmi() == null) {
        throw new Exception("router(rmi) is not supported.");
    }
    if (!routerRmiAvailable || servant == null) {
        throw new Exception("router(rmi) " + routerConfig.getUuid() + " is unavailable.");
    }
    ServiceBase[] obj = servant.getAllServiceNodeAvailable(clientUuid, code, md54Routing);
    cache.put(cacheKey, obj);
    return obj;
}
Also used : ServiceBase(j.service.server.ServiceBase) JUtilString(j.util.JUtilString) RemoteException(java.rmi.RemoteException)

Example 9 with ServiceBase

use of j.service.server.ServiceBase in project JFramework by gugumall.

the class Client method httpGetService.

/**
 * 获得服务入口
 * @param code
 * @return
 * @throws RemoteException
 */
public static String httpGetService(JHttp jhttp, HttpClient client, String code, boolean local) throws Exception {
    boolean callLocal = local ? true : false;
    if (!local) {
        if ("true".equalsIgnoreCase(Client.callLocalServiceIfExists))
            callLocal = true;
        else if ("random".equalsIgnoreCase(Client.callLocalServiceIfExists)) {
            int x = JUtilRandom.nextInt(3);
            if (x == 0)
                callLocal = true;
        }
    }
    if (callLocal) {
        // 调用本地服务
        ServiceContainer container = ServiceManager.getServiceContainer(code);
        if (container != null) {
            ServiceBase servant = container.getServantOfService(code);
            if (servant != null) {
                return servant.getServiceConfig().getHttp().getEntrance();
            }
        }
    }
    if (local)
        return null;
    String md54Routing = "";
    md54Routing += Manager.getClientNodeUuid();
    md54Routing += Manager.getClientKeyToRouter();
    md54Routing = JUtilMD5.MD5EncodeToHex(md54Routing);
    String entrance = RouterManager.serviceHttp(jhttp, client, Manager.getClientNodeUuid(), code, md54Routing);
    if (Constants.AUTH_FAILED.equals(entrance) || Constants.SERVICE_NOT_FOUND.equals(entrance) || Constants.SERVICE_NOT_AVAIL.equals(entrance)) {
        throw new Exception("无可用服务(http) - " + entrance + " - " + code);
    }
    if (entrance == null || !entrance.startsWith("http")) {
        throw new Exception("无可用服务(http) - " + entrance + " - " + code);
    }
    return entrance;
}
Also used : ServiceContainer(j.service.server.ServiceContainer) ServiceBase(j.service.server.ServiceBase) RemoteException(java.rmi.RemoteException)

Example 10 with ServiceBase

use of j.service.server.ServiceBase in project JFramework by gugumall.

the class Client method rmiGetService.

/**
 * 获得服务入口
 * @param code
 * @param local
 * @return
 * @throws Exception
 */
public static ServiceBase rmiGetService(String code, boolean local) throws Exception {
    boolean callLocal = local ? true : false;
    if (!local) {
        if ("true".equalsIgnoreCase(Client.callLocalServiceIfExists))
            callLocal = true;
        else if ("random".equalsIgnoreCase(Client.callLocalServiceIfExists)) {
            int x = JUtilRandom.nextInt(3);
            if (x == 0)
                callLocal = true;
        }
    }
    if (callLocal) {
        // 调用本地服务
        ServiceContainer container = ServiceManager.getServiceContainer(code);
        if (container != null) {
            ServiceBase servant = container.getServantOfService(code);
            if (servant != null) {
                return servant;
            }
        }
    }
    if (local)
        return null;
    String md54Routing = "";
    md54Routing += Manager.getClientNodeUuid();
    md54Routing += Manager.getClientKeyToRouter();
    md54Routing = JUtilMD5.MD5EncodeToHex(md54Routing);
    return (ServiceBase) RouterManager.serviceRmi(Manager.getClientNodeUuid(), code, md54Routing);
}
Also used : ServiceContainer(j.service.server.ServiceContainer) ServiceBase(j.service.server.ServiceBase)

Aggregations

ServiceBase (j.service.server.ServiceBase)10 RemoteException (java.rmi.RemoteException)6 ConcurrentMap (j.util.ConcurrentMap)3 SystemException (org.omg.CORBA.SystemException)3 InputStream (org.omg.CORBA.portable.InputStream)3 OutputStream (org.omg.CORBA.portable.OutputStream)3 Client (j.service.Client)2 ServiceContainer (j.service.server.ServiceContainer)2 ConcurrentList (j.util.ConcurrentList)2 JUtilString (j.util.JUtilString)2 UnexpectedException (java.rmi.UnexpectedException)2 List (java.util.List)2 HttpClient (org.apache.http.client.HttpClient)2 ApplicationException (org.omg.CORBA.portable.ApplicationException)2 RemarshalException (org.omg.CORBA.portable.RemarshalException)2 ServantObject (org.omg.CORBA.portable.ServantObject)2 JSession (j.app.webserver.JSession)1 JHttpContext (j.http.JHttpContext)1 Properties (java.util.Properties)1 Context (javax.naming.Context)1