Search in sources :

Example 6 with JHttp

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

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

the class Apple method main.

public static void main(String[] args) throws Exception {
    JHttp http = JHttp.getInstance();
    HttpClient client = http.createClient();
    JHttpContext context = new JHttpContext();
    String s1 = http.getResponse(context, client, "http://www.apple.com/cn/", "UTF-8");
    System.out.println(s1);
    context.addRequestHeader("Referer", "http://www.apple.com/cn/");
    String s2 = http.getResponse(context, client, "https://idmsa.apple.com/IDMSWebAuth/login.html?appIdKey=990d5c9e38720f4e832a8009a0fe4cad7dd151f99111dbea0df5e2934f267ec8&language=CN-zh&segment=R479&grpcode=g001&paramcode=h006&path=%2Fgeniusbar%2FR479%2Fsignin%2Fack&path2=%2Fgeniusbar%2FR479%2Fsignin%2Fack", "UTF-8");
    System.out.println(s2);
}
Also used : JHttpContext(j.http.JHttpContext) JHttp(j.http.JHttp) HttpClient(org.apache.http.client.HttpClient)

Aggregations

JHttp (j.http.JHttp)7 JHttpContext (j.http.JHttpContext)6 JUtilString (j.util.JUtilString)3 HttpClient (org.apache.http.client.HttpClient)3 JDFSFile (j.fs.JDFSFile)1 JUtilTextWriter (j.util.JUtilTextWriter)1 File (java.io.File)1 RemoteException (java.rmi.RemoteException)1 List (java.util.List)1 InitialContext (javax.naming.InitialContext)1 Document (org.dom4j.Document)1 Element (org.dom4j.Element)1