1. 程式人生 > >異構系統間的呼叫

異構系統間的呼叫

一.需求背景

       演算法團隊使用python作為開發語言,web系統的開發人員使用java,web系統提供了一些頁面操作,使用者點選

 按鈕之後,java呼叫python指令碼進行處理。

二.錯誤的選擇

      這個專案之前,筆者並沒有過java系統呼叫python的經驗,並且專案時間緊張,使用了最直接的呼叫方式,就是本地環境呼叫java指令碼。程式碼如下:

public static boolean  invokePython(String[] args){
    try {
        String line = null;
        Process proc = Runtime.getRuntime().exec(args);// 執行py檔案
        BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "utf-8"));   //gbk 避免漢字亂碼
        List<String> list = Lists.newArrayList();
        while ((line = in.readLine()) != null) {
            //logger.info(line);
            list.add(line);
        }
        in.close();
        int exitVal=proc.waitFor();
        logger.info("res:"+exitVal);
        /**Process exitValue: 0      呼叫python指令碼成功
          Process exitValue: 1      java呼叫python失敗
          Process exitValue: 2      python指令碼執行失敗**/
        if(exitVal==0){
           return true;
        }
        if(exitVal==2){
            logger.warn("java呼叫python失敗",JSON.toJSON(args));
            return  false;
        }
       else{
            logger.warn("python指令碼執行失敗",JSON.toJSON(args));
            return  false;
        }
    } catch (IOException e) {
        logger.warn("invokePython_fail_param_{}",JSON.toJSON(args),e);
        return  false;
    } catch (InterruptedException e) {
        logger.warn("invokePython_fail_param_{}",JSON.toJSON(args),e);
        System.out.println(e.getStackTrace());
        return  false;
    }
}

 這種方式有很多弊端,

 1.最明顯的就是python指令碼單獨執行並沒有問題,但是java呼叫時就報錯了,主要是專案路徑,專案字符集的問題

 2.本地開發環境配置複雜,python指令碼只能放在本地,所以java開發人員還要配置python的環境,大部分python第三方包還是很好裝,有的像tensorflow這種就會有不少問題,windows環境下,python3.5支援tensorflow,其他的3.6,3.7目前並不支援

3.除錯起來麻煩,java呼叫指令碼,指令碼一旦報錯的話,由於不能打斷點,就要在加一些print和try catch去判斷哪裡出問題,這種方式對於簡單程式碼還可以,複雜的話就要花不少時間。

由於以上幾個弊端,筆者對此種呼叫方式深惡痛絕

三.http介面呼叫的方式

      擺在面前的出路是python提供http服務,這就需要我們搭建一個python的框架,這裡我使用了Django,Django框架搭建起來

並不複雜,具體做法可以自行百度。

      搭建好Django之後,需要增加java傳送http請求的工具類,程式碼如下:

     

 public static String httpGetRequest(String url) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
//        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");
        // 建立UrlEncodedFormEntity物件
        HttpResponse response = httpClient.execute(httpGet);
        String html = extractContent(response);
        httpClient.close();
        return html;
    }

   

 public static String httpPostRequest(String url, Map<String, String> params) throws Exception {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        try{
//         HttpClient httpclient = new DefaultHttpClient();
            /** liupeng加入 讀取超時時間=3分鐘,預設=1分鐘 */
//           httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 3*60*1000);
//           httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3*60*1000);
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3*60*1000).setConnectTimeout(3*60*1000).build();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");
            httpPost.setConfig(requestConfig);
            List<NameValuePair> parameters = new ArrayList<NameValuePair>();
            for (String key : params.keySet()) {
                parameters.add(new BasicNameValuePair(key, params.get(key)));
            }
            // 建立UrlEncodedFormEntity物件
            UrlEncodedFormEntity formEntiry = new UrlEncodedFormEntity(parameters, "UTF-8");
            httpPost.setEntity(formEntiry);

            HttpResponse response = httpClient.execute(httpPost);
            if(response != null){
                logger.info("httpPostRequest11: " + response.toString());
            }else{
                logger.info("httpPostRequest22 ");
            }
            String html = extractContent(response);
            return html;
        }catch(Exception e){
            logger.error(" httpPostRequest ",e);
            throw new RuntimeException(e);
        } finally {
            if (null != httpClient) {
                httpClient.close();
            }
        }

    }

 

private static String extractContent(HttpResponse response) throws Exception {
    String htmStr = null;
    logger.info(" extractContentgetStatusCode " + response.getStatusLine().getStatusCode());
    logger.info(" content " + response.getEntity().getContent().toString());
    if (response.getStatusLine().getStatusCode() == 200) {
        if (response != null) {
            HttpEntity entity = response.getEntity();
            InputStream ins = entity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
            StringBuffer sbf = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                sbf.append(line);
            }
            br.close();
            // 處理內容
            htmStr = sbf.toString();
        }
    }else{
        if (response != null) {
            HttpEntity entity = response.getEntity();
            InputStream ins = entity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
            StringBuffer sbf = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                sbf.append(line);
            }
            br.close();
            // 處理內容
            htmStr = sbf.toString();
            logger.info(" 處理內容:" + htmStr);
        }
        return ""; //失敗時記錄日誌,但是結果返回空值
    }
    return htmStr;
}

  呼叫時發現報了Forbidden (CSRF cookie not set.): 錯誤:  

  臨時解決方案:修改settings.py檔案,註釋掉django.middleware.csrf.CsrfViewMiddleware'。

   post請求可以使用這種方式接收