Search in sources :

Example 21 with JHttpContext

use of j.http.JHttpContext 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 22 with JHttpContext

use of j.http.JHttpContext in project JFramework by gugumall.

the class RouterAgent method run.

/**
 * 监视状态并提交服务注册、卸载请求给关联的路由节点
 */
public void run() {
    int loop = 0;
    while (!this.shutdown) {
        try {
            Thread.sleep(5000);
        } catch (Exception e) {
        }
        try {
            monitor();
        } catch (Exception e) {
            log.log(e, Logger.LEVEL_DEBUG);
        }
        try {
            // 每5分钟清除缓存
            if (loop > 60) {
                cache.clear();
                loop = 0;
            }
            loop++;
        } catch (Exception e) {
        }
        try {
            while (!tasks.isEmpty()) {
                if (!routerRmiAvailable && !routerHttpAvailable) {
                    // rmi与http接口均不可用
                    break;
                }
                String[] task = (String[]) tasks.remove(0);
                if ("register".equals(task[0])) {
                    // 注册
                    Object result = null;
                    if (routerRmiAvailable) {
                        // 优先使用rmi
                        result = servant.register(task[1], task[2], task[3], task[4], task[5], task[6], task[7]);
                    } else if (routerHttpAvailable) {
                        String url = this.routerConfig.getHttp().getEntrance();
                        if (url.indexOf("?") > 0)
                            url += "&request=register";
                        else
                            url += "?request=register";
                        url += "&" + Constants.JSERVICE_PARAM_CLIENT_UUID + "=" + task[1];
                        url += "&" + Constants.JSERVICE_PARAM_SERVICE_CODE + "=" + task[2];
                        url += "&" + Constants.JSERVICE_PARAM_SERVICE_UUID + "=" + task[3];
                        url += "&" + Constants.JSERVICE_PARAM_RMI_CHANNEL + "=" + JUtilString.encodeURI(task[4], SysConfig.sysEncoding);
                        url += "&" + Constants.JSERVICE_PARAM_HTTP_CHANNEL + "=" + JUtilString.encodeURI(task[5], SysConfig.sysEncoding);
                        url += "&" + Constants.JSERVICE_PARAM_INTERFACE_CLASS + "=" + task[6];
                        url += "&" + Constants.JSERVICE_PARAM_MD5_STRING_4ROUTER + "=" + task[7];
                        JHttpContext context = jhttp.get(null, jclient, url, SysConfig.sysEncoding);
                        result = context.getResponseText();
                        context.finalize();
                        context = null;
                        if (!Constants.AUTH_FAILED.equals(result) && !Constants.INVOKING_DONE.equals(result)) {
                            throw new Exception("通过调用http接口注册服务失败");
                        }
                    }
                    if (Constants.AUTH_FAILED.equals(result)) {
                        log.log("service " + task[1] + "," + task[2] + "," + task[3] + " has not been registered to " + this.routerConfig.getUuid() + " caused by auth failure.", Logger.LEVEL_DEBUG);
                    } else {
                        log.log("service " + task[1] + "," + task[2] + "," + task[3] + " has been registered to " + this.routerConfig.getUuid() + " successfully.", Logger.LEVEL_DEBUG);
                    }
                    task = null;
                } else if ("unregister".equals(task[0])) {
                    // 卸载
                    Object result = null;
                    if (routerRmiAvailable) {
                        // 优先使用rmi
                        result = servant.unregister(task[1], task[2], task[3], task[4]);
                    } else if (routerHttpAvailable) {
                        String url = this.routerConfig.getHttp().getEntrance();
                        if (url.indexOf("?") > 0)
                            url += "&request=unregister";
                        else
                            url += "?request=unregister";
                        url += "&" + Constants.JSERVICE_PARAM_CLIENT_UUID + "=" + task[1];
                        url += "&" + Constants.JSERVICE_PARAM_SERVICE_CODE + "=" + task[2];
                        url += "&" + Constants.JSERVICE_PARAM_SERVICE_UUID + "=" + task[3];
                        url += "&" + Constants.JSERVICE_PARAM_MD5_STRING_4ROUTER + "=" + task[4];
                        url += "&" + Constants.JSERVICE_PARAM_MACHINE_ID + "=" + SysConfig.getMachineID();
                        JHttpContext context = jhttp.get(null, jclient, url, SysConfig.sysEncoding);
                        result = context.getResponseText();
                        context.finalize();
                        context = null;
                        if (!Constants.AUTH_FAILED.equals(result) && !Constants.INVOKING_DONE.equals(result)) {
                            throw new Exception("通过调用http接口卸载服务失败");
                        }
                    }
                    if (Constants.AUTH_FAILED.equals(result)) {
                        // 认证失败
                        log.log("service " + task[1] + "," + task[2] + "," + task[3] + " has not been unregistered from " + this.routerConfig.getUuid() + " caused by auth failure.", Logger.LEVEL_DEBUG);
                    } else {
                        log.log("service " + task[1] + "," + task[2] + "," + task[3] + " has been unregistered from " + this.routerConfig.getUuid() + " successfully.", Logger.LEVEL_DEBUG);
                    }
                    task = null;
                }
            }
        } catch (Exception e) {
            log.log(e, Logger.LEVEL_WARNING);
        }
        try {
            Thread.sleep(5000);
        } catch (Exception e) {
        }
    }
}
Also used : JHttpContext(j.http.JHttpContext) JObject(j.common.JObject) JUtilString(j.util.JUtilString) RemoteException(java.rmi.RemoteException)

Example 23 with JHttpContext

use of j.http.JHttpContext in project JFramework by gugumall.

the class RouterAgent method monitor.

/**
 * 监视关联路由节点状态,并报告RouterManager
 * @throws RemoteException
 */
private void monitor() throws RemoteException {
    boolean available = true;
    if (this.routerConfig.getRmi() != null) {
        // 监测rmi接口状态
        try {
            synchronized (Constants.GLOBAL_LOCK) {
                initialNamingContext = new InitialContext(this.routerConfig.getRmi().getConfig());
            }
        } catch (Exception e) {
            available = false;
            initialNamingContext = null;
            log.log(e, Logger.LEVEL_DEBUG_ADV);
        }
        if (available) {
            try {
                servant = (JRouter) initialNamingContext.lookup(this.routerConfig.getUuid());
                String heartbeat = servant.heartbeat();
                if (!heartbeat.startsWith(Constants.STATUS_OK)) {
                    throw new Exception("心跳不正常");
                } else {
                    long upd = Long.parseLong(heartbeat.substring(heartbeat.indexOf(":") + 1));
                    if (upd > update) {
                        cache.clear();
                        update = upd;
                    }
                }
            } catch (Exception e) {
                available = false;
                servant = null;
                log.log(e, Logger.LEVEL_DEBUG_ADV);
            }
        }
    } else {
        available = false;
        servant = null;
    }
    routerRmiAvailable = available;
    available = true;
    if (this.routerConfig.getHttp() != null) {
        // 监测http接口状态
        String url = this.routerConfig.getHttp().getEntrance();
        if (url.indexOf("?") > 0)
            url += "&request=heartbeat";
        else
            url += "?request=heartbeat";
        JHttp jhttp = JHttp.getInstance();
        JHttpContext context = null;
        try {
            context = jhttp.get(null, null, url);
        } catch (Exception e) {
            available = false;
            log.log(e, Logger.LEVEL_DEBUG);
        }
        if (context == null || context.getStatus() != 200 || !context.getResponseText().startsWith(Constants.STATUS_OK)) {
            available = false;
        } else {
            String heartbeat = context.getResponseText();
            long upd = Long.parseLong(heartbeat.substring(heartbeat.indexOf(":") + 1));
            if (upd > update) {
                cache.clear();
                update = upd;
            }
        }
    } else {
        available = false;
    }
    routerHttpAvailable = available;
    log.log("rmi of router " + routerConfig.getUuid() + "," + routerConfig.getName() + " is " + (routerRmiAvailable ? "available" : "unavailable") + ".", Logger.LEVEL_DEBUG);
    log.log("http of router " + routerConfig.getUuid() + "," + routerConfig.getName() + " is " + (routerHttpAvailable ? "available" : "unavailable") + ".", Logger.LEVEL_DEBUG);
    if (routerRmiAvailable) {
        RouterManager.setAvailableRmi(this.routerConfig.getUuid(), true);
    } else {
        RouterManager.setAvailableRmi(this.routerConfig.getUuid(), false);
    }
    if (routerHttpAvailable) {
        RouterManager.setAvailableHttp(this.routerConfig.getUuid(), true);
    } else {
        RouterManager.setAvailableHttp(this.routerConfig.getUuid(), false);
    }
}
Also used : JHttpContext(j.http.JHttpContext) JHttp(j.http.JHttp) JUtilString(j.util.JUtilString) InitialContext(javax.naming.InitialContext) RemoteException(java.rmi.RemoteException)

Example 24 with JHttpContext

use of j.http.JHttpContext in project JFramework by gugumall.

the class Client method httpCallGet.

/**
 * @param jhttp
 * @param client
 * @param code
 * @param entrance
 * @param methodName
 * @param params
 * @return
 * @throws Exception
 */
public static String httpCallGet(JHttp jhttp, HttpClient client, String code, String entrance, String methodName, Map params) throws Exception {
    String url = entrance;
    if (url.indexOf("?") > 0)
        url += "&request=" + methodName;
    else
        url += "?request=" + methodName;
    String md54Service = md54Service(code, methodName);
    url += "&" + Constants.JSERVICE_PARAM_CLIENT_UUID + "=" + Manager.getClientNodeUuid();
    url += "&" + Constants.JSERVICE_PARAM_MD5_STRING_4SERVICE + "=" + md54Service;
    if (params != null) {
        for (Iterator keys = params.keySet().iterator(); keys.hasNext(); ) {
            Object key = keys.next();
            Object val = params.get(key);
            url += "&" + key + "=" + val;
        }
    }
    try {
        if (jhttp == null)
            jhttp = JHttp.getInstance();
        JHttpContext context = jhttp.get(null, client, url, SysConfig.sysEncoding);
        if (context == null || context.getStatus() != 200 || Constants.INVOKING_FAILED.equals(context.getResponseText())) {
            if (context != null) {
                context.finalize();
                context = null;
            }
            throw new Exception("failed to call service on " + url);
        }
        String responseText = context.getResponseText();
        context.finalize();
        context = null;
        return responseText;
    } catch (Exception ex) {
        log.log(ex, Logger.LEVEL_ERROR);
        throw ex;
    }
}
Also used : JHttpContext(j.http.JHttpContext) Iterator(java.util.Iterator) RemoteException(java.rmi.RemoteException)

Example 25 with JHttpContext

use of j.http.JHttpContext in project JFramework by gugumall.

the class Client method httpCallMultiPart.

/**
 * @param jhttp
 * @param client
 * @param code
 * @param entrance
 * @param methodName
 * @param params
 * @param parts
 * @return
 * @throws Exception
 */
public static String httpCallMultiPart(JHttp jhttp, HttpClient client, String code, String entrance, String methodName, Map params, Map parts) throws Exception {
    String url = entrance;
    if (url.indexOf("?") > 0)
        url += "&request=" + methodName;
    else
        url += "?request=" + methodName;
    if (params == null)
        params = new HashMap();
    String md54Service = md54Service(code, methodName);
    params.put(Constants.JSERVICE_PARAM_CLIENT_UUID, Manager.getClientNodeUuid());
    params.put(Constants.JSERVICE_PARAM_MD5_STRING_4SERVICE, md54Service);
    try {
        if (jhttp == null)
            jhttp = JHttp.getInstance();
        JHttpContext context = jhttp.postMultipartData(null, client, url, parts, params, SysConfig.sysEncoding);
        if (context == null || context.getStatus() != 200 || Constants.INVOKING_FAILED.equals(context.getResponseText())) {
            if (context != null) {
                context.finalize();
                context = null;
            }
            throw new Exception("failed to call service on " + url);
        }
        String responseText = context.getResponseText();
        context.finalize();
        context = null;
        return responseText;
    } catch (Exception ex) {
        log.log(ex, Logger.LEVEL_ERROR);
        throw ex;
    }
}
Also used : JHttpContext(j.http.JHttpContext) HashMap(java.util.HashMap) RemoteException(java.rmi.RemoteException)

Aggregations

JHttpContext (j.http.JHttpContext)26 JUtilString (j.util.JUtilString)16 RemoteException (java.rmi.RemoteException)8 JHttp (j.http.JHttp)6 HttpClient (org.apache.http.client.HttpClient)5 HashMap (java.util.HashMap)4 ConcurrentMap (j.util.ConcurrentMap)3 Map (java.util.Map)2 InitialContext (javax.naming.InitialContext)2 Client (j.app.sso.Client)1 JObject (j.common.JObject)1 JDFSFile (j.fs.JDFSFile)1 ServiceBase (j.service.server.ServiceBase)1 ConcurrentList (j.util.ConcurrentList)1 JUtilTextWriter (j.util.JUtilTextWriter)1 File (java.io.File)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Properties (java.util.Properties)1 Context (javax.naming.Context)1