Search in sources :

Example 11 with ConcurrentMap

use of j.util.ConcurrentMap in project JFramework by gugumall.

the class JObject method getStaticMap.

/**
 * @param key
 * @return
 */
public static ConcurrentMap getStaticMap(String key) {
    if (statics.containsKey(key)) {
        return (ConcurrentMap) statics.get(key);
    } else {
        ConcurrentMap s = new ConcurrentMap();
        statics.put(key, s);
        return s;
    }
}
Also used : ConcurrentMap(j.util.ConcurrentMap)

Example 12 with ConcurrentMap

use of j.util.ConcurrentMap in project JFramework by gugumall.

the class JRouterImpl method monitor.

/**
 * @param node
 */
private void monitor(Service node) {
    try {
        monitoring = true;
        ConcurrentMap servantsOfCluster = (ConcurrentMap) servantsOfClusters.get(node.clusterCode);
        ConcurrentMap httpsOfCluster = (ConcurrentMap) httpsOfClusters.get(node.clusterCode);
        if (servantsOfCluster == null) {
            servantsOfCluster = new ConcurrentMap();
            servantsOfClusters.put(node.clusterCode, servantsOfCluster);
        }
        if (httpsOfCluster == null) {
            httpsOfCluster = new ConcurrentMap();
            httpsOfClusters.put(node.clusterCode, httpsOfCluster);
        }
        boolean serviceRmiAvailable = true;
        if (node.rmi != null && !"".equals(node.rmi)) {
            Context context = null;
            try {
                Properties props = null;
                props = j.Properties.getProperties("rmi");
                props.put("java.naming.provider.url", node.rmi);
                context = new InitialContext(props);
            } catch (Exception e) {
                serviceRmiAvailable = false;
                context = null;
            // log.log(e,Logger.LEVEL_DEBUG);
            }
            ServiceBase servant = (ServiceBase) servantsOfCluster.get(node.uuid);
            if (serviceRmiAvailable) {
                try {
                    servant = (ServiceBase) context.lookup(node.uuid);
                    servantsOfCluster.put(node.uuid, servant);
                    if (!Constants.STATUS_OK.equals(servant.heartbeat())) {
                        throw new Exception("心跳不正常(rmi)");
                    }
                } catch (Exception e) {
                    servantsOfCluster.remove(node.uuid);
                    serviceRmiAvailable = false;
                    servant = null;
                // log.log(e,Logger.LEVEL_DEBUG);
                }
            } else {
                servantsOfCluster.remove(node.uuid);
                serviceRmiAvailable = false;
                servant = null;
            }
        } else {
            servantsOfCluster.remove(node.uuid);
            serviceRmiAvailable = false;
        }
        if (!serviceRmiAvailable) {
            log.log("service(rmi) " + node.clusterCode + "," + node.uuid + " is unavailable.", Logger.LEVEL_DEBUG);
        } else {
            log.log("service(rmi) " + node.clusterCode + "," + node.uuid + " is available(" + servantsOfCluster.size() + " nodes).", Logger.LEVEL_DEBUG);
        }
        boolean serviceHttpAvailable = true;
        if (node.http != null && !"".equals(node.http)) {
            try {
                String url = node.http;
                if (url.indexOf("?") > 0)
                    url += "&request=heartbeat";
                else
                    url += "?request=heartbeat";
                JHttpContext context = jhttp.get(null, jclient, url, SysConfig.sysEncoding);
                String result = context.getResponseText();
                context.finalize();
                context = null;
                if (!Constants.STATUS_OK.equals(result)) {
                    throw new Exception("心跳不正常(http)");
                } else {
                    httpsOfCluster.put(node.uuid, node);
                }
            } catch (Exception e) {
                httpsOfCluster.remove(node.uuid);
                serviceHttpAvailable = false;
            // log.log(e,Logger.LEVEL_DEBUG);
            }
        } else {
            httpsOfCluster.remove(node.uuid);
            serviceHttpAvailable = false;
        }
        if (!serviceHttpAvailable) {
            log.log("service(http) " + node.clusterCode + "," + node.uuid + " is unavailable.", Logger.LEVEL_DEBUG);
        } else {
            log.log("service(http) " + node.clusterCode + "," + node.uuid + " is available(" + httpsOfCluster.size() + " nodes).", Logger.LEVEL_DEBUG);
        }
        monitoring = false;
    } catch (Exception e) {
        monitoring = false;
        log.log(e, Logger.LEVEL_ERROR);
    }
}
Also used : InitialContext(javax.naming.InitialContext) JHttpContext(j.http.JHttpContext) Context(javax.naming.Context) JHttpContext(j.http.JHttpContext) ServiceBase(j.service.server.ServiceBase) ConcurrentMap(j.util.ConcurrentMap) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) RemoteException(java.rmi.RemoteException)

Example 13 with ConcurrentMap

use of j.util.ConcurrentMap in project JFramework by gugumall.

the class JRouterImpl method service.

/*
	 *  (non-Javadoc)
	 * @see j.service.router.RouterInterface#service(j.app.webserver.JSession, javax.servlet.http.HttpSession, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
public void service(JSession jsession, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws RemoteException {
    waitWhileMonitoring();
    String clientUuid = SysUtil.getHttpParameter(request, Constants.JSERVICE_PARAM_CLIENT_UUID);
    String clusterCode = SysUtil.getHttpParameter(request, Constants.JSERVICE_PARAM_SERVICE_CODE);
    String md54Routing = SysUtil.getHttpParameter(request, Constants.JSERVICE_PARAM_MD5_STRING_4ROUTER);
    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_ERROR);
            jsession.resultString = Constants.AUTH_FAILED;
            return;
        }
        String md5 = "";
        md5 += clientUuid;
        md5 += client.getKey();
        md5 = JUtilMD5.MD5EncodeToHex(md5);
        if (!md5.equalsIgnoreCase(md54Routing)) {
            jsession.resultString = Constants.AUTH_FAILED;
            return;
        }
    } else {
        // 未实现的隐私策略
        jsession.resultString = Constants.AUTH_FAILED;
        return;
    }
    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 httpsOfCluster = (ConcurrentMap) httpsOfClusters.get(codeOfUuid == null ? clusterCode : codeOfUuid);
    if (httpsOfCluster == null) {
        log.log("the service(http channel) " + clusterCode + " is not found.", Logger.LEVEL_WARNING);
        jsession.resultString = Constants.SERVICE_NOT_FOUND;
        return;
    }
    List nodes = httpsOfCluster.listValues();
    if (nodes.size() == 0) {
        log.log("no valid node(http channel) for the service " + clusterCode + ".", Logger.LEVEL_WARNING);
        jsession.resultString = Constants.SERVICE_NOT_AVAIL;
        return;
    }
    if (httpsOfCluster.containsKey(clusterCode)) {
        // 按uuid获得
        nodes.clear();
        nodes = null;
        jsession.resultString = ((Service) httpsOfCluster.get(clusterCode)).http;
        return;
    } else {
        String entrance = ((Service) nodes.get(JUtilRandom.nextInt(nodes.size()))).http;
        nodes.clear();
        nodes = null;
        jsession.resultString = entrance;
    }
}
Also used : ConcurrentMap(j.util.ConcurrentMap) List(java.util.List) ConcurrentList(j.util.ConcurrentList) Client(j.service.Client) HttpClient(org.apache.http.client.HttpClient)

Example 14 with ConcurrentMap

use of j.util.ConcurrentMap in project JFramework by gugumall.

the class JRouterImpl method getAllServiceNodeAvailable.

/*
	 * (non-Javadoc)
	 * @see j.service.router.JRouter#getAllServiceNode(j.app.webserver.JSession, javax.servlet.http.HttpSession, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
public void getAllServiceNodeAvailable(JSession jsession, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws RemoteException {
    waitWhileMonitoring();
    String clientUuid = SysUtil.getHttpParameter(request, Constants.JSERVICE_PARAM_CLIENT_UUID);
    String clusterCode = SysUtil.getHttpParameter(request, Constants.JSERVICE_PARAM_SERVICE_CODE);
    String md54Routing = SysUtil.getHttpParameter(request, Constants.JSERVICE_PARAM_MD5_STRING_4ROUTER);
    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_ERROR);
            jsession.resultString = Constants.AUTH_FAILED;
            return;
        }
        String md5 = "";
        md5 += clientUuid;
        md5 += client.getKey();
        md5 = JUtilMD5.MD5EncodeToHex(md5);
        if (!md5.equalsIgnoreCase(md54Routing)) {
            jsession.resultString = Constants.AUTH_FAILED;
            return;
        }
    } else {
        // 未实现的隐私策略
        jsession.resultString = Constants.AUTH_FAILED;
        return;
    }
    ConcurrentMap httpsOfCluster = (ConcurrentMap) httpsOfClusters.get(clusterCode);
    if (httpsOfCluster == null) {
        log.log("the service(http channel) " + clusterCode + " is not found.", Logger.LEVEL_WARNING);
        jsession.resultString = Constants.SERVICE_NOT_FOUND;
        return;
    }
    List nodes = httpsOfCluster.listValues();
    if (nodes.size() == 0) {
        log.log("no valid node(http channel) for the service " + clusterCode + ".", Logger.LEVEL_WARNING);
        jsession.resultString = Constants.SERVICE_NOT_AVAIL;
        return;
    }
    String[] entrances = new String[nodes.size()];
    for (int i = 0; i < nodes.size(); i++) {
        Service service = (Service) nodes.get(i);
        entrances[i] = service.http;
    }
    nodes.clear();
    nodes = null;
    try {
        jsession.resultString = JObject.serializable2String(entrances, false);
    } catch (Exception e) {
        log.log(e, Logger.LEVEL_ERROR);
        jsession.resultString = "ERR";
    }
}
Also used : 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 15 with ConcurrentMap

use of j.util.ConcurrentMap 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)

Aggregations

ConcurrentMap (j.util.ConcurrentMap)16 ConcurrentList (j.util.ConcurrentList)8 List (java.util.List)7 HttpClient (org.apache.http.client.HttpClient)7 Client (j.service.Client)5 JUtilString (j.util.JUtilString)5 RemoteException (java.rmi.RemoteException)4 JHttpContext (j.http.JHttpContext)3 ServiceBase (j.service.server.ServiceBase)3 JUtilList (j.util.JUtilList)3 Map (java.util.Map)3 HashMap (java.util.HashMap)2 CachedMap (j.cache.CachedMap)1 JObject (j.common.JObject)1 JUtilSorter (j.util.JUtilSorter)1 Iterator (java.util.Iterator)1 Properties (java.util.Properties)1 Context (javax.naming.Context)1 InitialContext (javax.naming.InitialContext)1