Search in sources :

Example 1 with Rmi

use of j.service.Rmi in project JFramework by gugumall.

the class RouterManager method load.

/**
 */
public static void load() {
    try {
        loading = true;
        List currentRouters = new LinkedList();
        // 文件是否存在
        File file = new File(Properties.getConfigPath() + "service.router.xml");
        if (!file.exists()) {
            throw new Exception("找不到配置文件:" + file.getAbsolutePath());
        }
        Document document = JUtilDom4j.parse(Properties.getConfigPath() + "service.router.xml", "UTF-8");
        Element root = document.getRootElement();
        List routerEles = root.elements("router");
        for (int i = 0; i < routerEles.size(); i++) {
            Element routerEle = (Element) routerEles.get(i);
            RouterConfig config = new RouterConfig();
            config.setServerUuid(routerEle.attributeValue("server-uuid"));
            config.setUuid(routerEle.elementText("uuid"));
            config.setName(routerEle.elementText("name"));
            config.setPrivacy(routerEle.elementText("privacy"));
            List routerProps = routerEle.elements("property");
            for (int j = 0; j < routerProps.size(); j++) {
                Element pEle = (Element) routerProps.get(j);
                config.addConfig(pEle.attributeValue("key"), pEle.attributeValue("value"));
            }
            Element rmiEle = routerEle.element("rmi");
            if (rmiEle != null && "true".equalsIgnoreCase(rmiEle.attributeValue("available"))) {
                Rmi rmi = new Rmi(Properties.getProperties("rmi"));
                List props = rmiEle.elements("property");
                for (int j = 0; j < props.size(); j++) {
                    Element pEle = (Element) props.get(j);
                    rmi.addConfig(pEle.attributeValue("key"), pEle.attributeValue("value"));
                }
                config.setRmi(rmi);
            }
            Element httpEle = routerEle.element("http");
            if (httpEle != null && "true".equalsIgnoreCase(httpEle.attributeValue("available"))) {
                Http http = new Http();
                List props = httpEle.elements("property");
                for (int j = 0; j < props.size(); j++) {
                    Element pEle = (Element) props.get(j);
                    http.addConfig(pEle.attributeValue("key"), pEle.attributeValue("value"));
                }
                config.setHttp(http);
            }
            List clients = root.elements("client");
            for (int j = 0; j < clients.size(); j++) {
                Element clientEle = (Element) clients.get(j);
                Client client = new Client();
                client.setUuid(clientEle.attributeValue("uuid"));
                client.setName(clientEle.attributeValue("name"));
                client.setKey(clientEle.attributeValue("key"));
                config.addClient(client);
            }
            currentRouters.add(config);
        }
        root = null;
        document = null;
        // 停止已启动服务
        List olds = routerConfigs.listValues();
        for (int i = 0; i < olds.size(); i++) {
            RouterConfig config = (RouterConfig) olds.get(i);
            RouterContainer containter = (RouterContainer) routerContainers.get(config.getUuid());
            if (containter != null && containter.getStarted()) {
                containter.shutdown();
            }
            RouterAgent agent = (RouterAgent) agents.get(config.getUuid());
            agent.shutdown();
        }
        JUtilList.clear_AllNull(olds);
        routerConfigs.clear();
        routerContainers.clear();
        agents.clear();
        uuidOfRouters.clear();
        uuidOfRoutersAvailableHttp.clear();
        // 处理最新服务
        for (int i = 0; i < currentRouters.size(); i++) {
            RouterConfig routerConfig = (RouterConfig) currentRouters.get(i);
            routerConfigs.put(routerConfig.getUuid(), routerConfig);
            uuidOfRouters.add(routerConfig.getUuid());
            if (routerConfig.getServerUuid().equals(Manager.getRouterNodeUuid())) {
                // 如果是路由节点,启动路由服务
                // 托管至nvwa
                NvwaObject nvwaObject = Nvwa.entrust(routerConfig.getRelatedHttpHandlerPath(), routerConfig.getClassName(), true);
                nvwaObject.setFiled("routerConfig", "ref", true);
                JRouter router = (JRouter) Nvwa.entrustCreate(routerConfig.getRelatedHttpHandlerPath(), routerConfig.getClassName(), true);
                router.setRouterConfig(routerConfig);
                RouterContainer container = new RouterContainer(routerConfig, router);
                routerContainers.put(routerConfig.getUuid(), container);
                container.startup();
            }
            // 启动路由代理
            RouterAgent agent = new RouterAgent(routerConfig);
            agents.put(routerConfig.getUuid(), agent);
            agent.startup();
        }
        currentRouters.clear();
        currentRouters = null;
        // 处理最新服务 END
        // 配置文件最近修改时间
        File configFile = new File(Properties.getConfigPath() + "service.router.xml");
        configLastModified = configFile.lastModified();
        configFile = null;
        loading = false;
    } catch (Exception e) {
        loading = false;
        log.log(e, Logger.LEVEL_FATAL);
    }
}
Also used : Element(org.dom4j.Element) NvwaObject(j.nvwa.NvwaObject) JHttp(j.http.JHttp) Http(j.service.Http) Document(org.dom4j.Document) LinkedList(java.util.LinkedList) RemoteException(java.rmi.RemoteException) Rmi(j.service.Rmi) List(java.util.List) ConcurrentList(j.util.ConcurrentList) JUtilList(j.util.JUtilList) LinkedList(java.util.LinkedList) Client(j.service.Client) HttpClient(org.apache.http.client.HttpClient) File(java.io.File)

Example 2 with Rmi

use of j.service.Rmi in project JFramework by gugumall.

the class ServiceManager method load.

/**
 * 初始化
 */
public static void load() {
    try {
        loading = true;
        servicesOfClusters.clear();
        List currentServices = new LinkedList();
        // 文件是否存在
        File file = new File(Properties.getConfigPath() + "service.server.xml");
        if (!file.exists()) {
            throw new Exception("找不到配置文件:" + file.getAbsolutePath());
        }
        Document document = JUtilDom4j.parse(Properties.getConfigPath() + "service.server.xml", "UTF-8");
        Element root = document.getRootElement();
        List servs = root.elements("service");
        for (int i = 0; i < servs.size(); i++) {
            Element serv = (Element) servs.get(i);
            List nodes = serv.elements("node");
            for (int n = 0; n < nodes.size(); n++) {
                Element nodeE = (Element) nodes.get(n);
                ServiceConfig service = new ServiceConfig();
                String _toString = "";
                // 公共信息
                service.setCode(serv.elementText("code"));
                service.setName(serv.elementText("name"));
                service.setPrivacy(serv.elementText("privacy"));
                _toString += serv.elementText("code");
                _toString += " - " + serv.elementText("name");
                _toString += " - " + serv.elementText("privacy");
                List serviceProps = serv.elements("property");
                for (int j = 0; j < serviceProps.size(); j++) {
                    Element pEle = (Element) serviceProps.get(j);
                    service.addConfig(pEle.attributeValue("key"), pEle.attributeValue("value"));
                    _toString += " - {" + pEle.attributeValue("key") + "=" + pEle.attributeValue("value") + "}";
                }
                List methods = serv.elements("method");
                for (int j = 0; j < methods.size(); j++) {
                    Element methodEle = (Element) methods.get(j);
                    Method method = new Method();
                    method.setName(methodEle.elementText("name"));
                    method.setPrivacy(methodEle.elementText("privacy"));
                    _toString += " - [" + methodEle.elementText("name") + ";" + methodEle.elementText("privacy") + "]";
                    service.addMethod(method);
                }
                List clients = serv.elements("client");
                for (int j = 0; j < clients.size(); j++) {
                    Element clientEle = (Element) clients.get(j);
                    Client client = new Client();
                    client.setUuid(clientEle.attributeValue("uuid"));
                    client.setName(clientEle.attributeValue("name"));
                    client.setKey(clientEle.attributeValue("key"));
                    _toString += " - <" + clientEle.attributeValue("uuid") + ";" + clientEle.attributeValue("name") + ";" + clientEle.attributeValue("key") + ">";
                    service.addClient(client);
                }
                // 公共信息 END
                // 节点信息
                service.setServerUuid(nodeE.attributeValue("server-uuid"));
                _toString += " - " + nodeE.attributeValue("server-uuid");
                service.setUuid(nodeE.elementText("uuid"));
                _toString += " - " + nodeE.elementText("uuid");
                Element rmiEle = nodeE.element("rmi");
                if (rmiEle != null && "true".equalsIgnoreCase(rmiEle.attributeValue("available"))) {
                    Rmi rmi = new Rmi(Properties.getProperties("rmi"));
                    List props = rmiEle.elements("property");
                    for (int j = 0; j < props.size(); j++) {
                        Element pEle = (Element) props.get(j);
                        rmi.addConfig(pEle.attributeValue("key"), pEle.attributeValue("value"));
                        _toString += " - <" + pEle.attributeValue("key") + "=" + pEle.attributeValue("value") + ">";
                    }
                    service.setRmi(rmi);
                }
                Element httpEle = nodeE.element("http");
                if (httpEle != null && "true".equalsIgnoreCase(httpEle.attributeValue("available"))) {
                    Http http = new Http();
                    List props = httpEle.elements("property");
                    for (int j = 0; j < props.size(); j++) {
                        Element pEle = (Element) props.get(j);
                        http.addConfig(pEle.attributeValue("key"), pEle.attributeValue("value"));
                        _toString += " - <" + pEle.attributeValue("key") + "=" + pEle.attributeValue("value") + ">";
                    }
                    service.setHttp(http);
                }
                service.setToString(_toString);
                // 节点信息 END
                currentServices.add(service);
                // 分组保存
                ConcurrentList ls = (ConcurrentList) servicesOfClusters.get(service.getCode());
                if (ls == null) {
                    ls = new ConcurrentList();
                    servicesOfClusters.put(service.getCode(), ls);
                }
                ls.add(service);
            }
        }
        root = null;
        document = null;
        // 停止已启动服务
        List olds = services.listValues();
        for (int i = 0; i < olds.size(); i++) {
            ServiceConfig service = (ServiceConfig) olds.get(i);
            ServiceContainer containter = (ServiceContainer) serviceContainers.get(service.getUuid());
            if (containter != null && containter.getStarted()) {
                ServiceConfig _new = null;
                for (int j = 0; j < currentServices.size(); j++) {
                    ServiceConfig s = (ServiceConfig) currentServices.get(j);
                    if (s.getUuid().equals(service.getUuid())) {
                        _new = s;
                        break;
                    }
                }
                if (_new != null && _new.equals(service)) {
                    log.log("重启服务(停止阶段)- " + service.getCode() + " - " + service.getName() + " 没有变动,不需要重启", -1);
                } else {
                    log.log("重启服务(停止阶段)- " + service.getCode() + " - " + service.getName() + " 有变动,需要重启,正尝试停止", -1);
                    containter.shutdown();
                }
            }
        }
        services.clear();
        serviceContainers.clear();
        // 处理最新服务
        for (int i = 0; i < currentServices.size(); i++) {
            ServiceConfig serviceConfig = (ServiceConfig) currentServices.get(i);
            // 保存服务,可通过uuid找到
            services.put(serviceConfig.getUuid(), serviceConfig);
            if (serviceConfig.getServerUuid().equals(Manager.getServerNodeUuid())) {
                // 本地服务才启动
                ServiceConfig _old = null;
                for (int j = 0; j < olds.size(); j++) {
                    ServiceConfig s = (ServiceConfig) olds.get(j);
                    if (s.getUuid().equals(serviceConfig.getUuid())) {
                        _old = s;
                        break;
                    }
                }
                if (_old != null && _old.equals(serviceConfig)) {
                    log.log("重启服务(启动阶段) - " + serviceConfig.getCode() + " - " + serviceConfig.getName() + " 没有变动,不需要重启", -1);
                } else {
                    if (_old != null) {
                        log.log("重启服务(启动阶段)- " + serviceConfig.getCode() + " - " + serviceConfig.getName() + " 有变动,需要重启,正尝试启动", -1);
                    } else {
                        log.log("启动服务 - " + serviceConfig.getCode() + " - " + serviceConfig.getName() + ",正尝试启动", -1);
                    }
                    // 启动服务
                    ServiceContainer container = new ServiceContainer(serviceConfig);
                    serviceContainers.put(serviceConfig.getUuid(), container);
                    serviceContainers.put(serviceConfig.getCode(), container);
                    container.startup();
                }
            }
        }
        currentServices.clear();
        currentServices = null;
        JUtilList.clear_AllNull(olds);
        // 处理最新服务 END
        // 配置文件最近修改时间
        File configFile = new File(Properties.getConfigPath() + "service.server.xml");
        configLastModified = configFile.lastModified();
        configFile = null;
        loading = false;
    } catch (Exception e) {
        loading = false;
        log.log(e, Logger.LEVEL_FATAL);
    }
}
Also used : Element(org.dom4j.Element) Http(j.service.Http) Document(org.dom4j.Document) LinkedList(java.util.LinkedList) Rmi(j.service.Rmi) ConcurrentList(j.util.ConcurrentList) List(java.util.List) ConcurrentList(j.util.ConcurrentList) JUtilList(j.util.JUtilList) LinkedList(java.util.LinkedList) Client(j.service.Client) File(java.io.File)

Aggregations

Client (j.service.Client)2 Http (j.service.Http)2 Rmi (j.service.Rmi)2 ConcurrentList (j.util.ConcurrentList)2 JUtilList (j.util.JUtilList)2 File (java.io.File)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Document (org.dom4j.Document)2 Element (org.dom4j.Element)2 JHttp (j.http.JHttp)1 NvwaObject (j.nvwa.NvwaObject)1 RemoteException (java.rmi.RemoteException)1 HttpClient (org.apache.http.client.HttpClient)1