Search in sources :

Example 6 with ConnectionFailoverMgr

use of com.creditease.uav.helpers.connfailover.ConnectionFailoverMgr in project uavstack by uavorg.

the class EsRestServletCallBack method service.

@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // 获取请求资源
    String proName = req.getServletContext().getContextPath();
    String requestSource = req.getRequestURI();
    requestSource = requestSource.substring(requestSource.indexOf(proName) + proName.length());
    if (requestSource.startsWith("/es")) {
        requestSource = requestSource.substring(3);
    }
    /**
     * get url
     */
    Map<String, ConnectionFailoverMgr> connectionMgrPool = EsResource.getConnMgr();
    String forwarUrl = connectionMgrPool.get("es.info.forwar.url").getConnection() + requestSource;
    /**
     * get method
     */
    String method = req.getMethod();
    /**
     * get body
     */
    ServletInputStream input = req.getInputStream();
    EsResource.getHttpAsyncClient().doAsyncHttpMethodWithReqAsync(method, forwarUrl, null, input, null, null, "application/json", "utf-8", new EsRestServletCallBack(), req);
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) ConnectionFailoverMgr(com.creditease.uav.helpers.connfailover.ConnectionFailoverMgr)

Example 7 with ConnectionFailoverMgr

use of com.creditease.uav.helpers.connfailover.ConnectionFailoverMgr in project uavstack by uavorg.

the class AppHubBaseRestService method doHttpPost.

/**
 * 支持普通Http Post调用和智能的带FailOver的调用
 *
 * @param postUrl
 * @param subPath
 * @param data
 * @param contentType
 * @param encoding
 * @param callBack
 */
public void doHttpPost(String serverAddress, String subPath, byte[] data, String contentType, String encoding, HttpClientCallback callBack) {
    ConnectionFailoverMgr cfm = httpConnInvokeMgr.get(serverAddress);
    /**
     * Step 1: if there is no ConnectionFailoverMgr, take it as normal http post
     */
    if (cfm == null) {
        final String postURL = (subPath != null) ? (serverAddress + subPath) : serverAddress;
        httpAsyncClient.doAsyncHttpPost(postURL, data, contentType, encoding, callBack);
        return;
    }
    /**
     * Step 2: do smart http post
     */
    String url = cfm.getConnection();
    if (url == null) {
        String msg = "No Available Address for ServerAddressList[" + serverAddress + "]";
        this.getLogger().warn(this, msg);
        if (callBack != null) {
            HttpClientCallbackResult result = new HttpClientCallbackResult(null, null);
            result.setException(new HttpAsyncException(ExceptionEvent.REPLY_ERROR, new ConnectException(msg)));
            callBack.failed(result);
        }
        return;
    }
    String postURL = (subPath != null) ? (url + subPath) : url;
    getLogger().info(this, "doHttpPost URL :" + postURL);
    PostHttpCallback postHttpCb = new PostHttpCallback();
    postHttpCb.setCallBack(callBack);
    postHttpCb.setCfm(cfm);
    postHttpCb.setContentType(contentType);
    postHttpCb.setData(data);
    postHttpCb.setEncoding(encoding);
    postHttpCb.setPostURL(postURL);
    postHttpCb.setServerAddress(serverAddress);
    postHttpCb.setSubPath(subPath);
    postHttpCb.setUrl(url);
    httpAsyncClient.doAsyncHttpPost(postURL, data, contentType, encoding, postHttpCb);
}
Also used : ConnectionFailoverMgr(com.creditease.uav.helpers.connfailover.ConnectionFailoverMgr) HttpClientCallbackResult(com.creditease.uav.httpasync.HttpClientCallbackResult) HttpAsyncException(com.creditease.uav.httpasync.HttpAsyncException) ConnectException(java.net.ConnectException)

Aggregations

ConnectionFailoverMgr (com.creditease.uav.helpers.connfailover.ConnectionFailoverMgr)7 HttpAsyncException (com.creditease.uav.httpasync.HttpAsyncException)2 HttpClientCallbackResult (com.creditease.uav.httpasync.HttpClientCallbackResult)2 UAVHttpMessage (com.creditease.agent.http.api.UAVHttpMessage)1 HttpClientCallback (com.creditease.uav.httpasync.HttpClientCallback)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ConnectException (java.net.ConnectException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ServletInputStream (javax.servlet.ServletInputStream)1