<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-124  評論-194  文章-0  trackbacks-0
    今天搞了一天,JAVA調用一個PERL程序,得不得就退不出,千試萬試,LOG精細到逐行,知道在哪停住了,但打死不知道為什么。
    后來吃個飯都放棄了,居然又找到答案,要沒看到它,那真以為里面有鬼了。

    大概原因是,調用Runtime.getRuntime().exec后,如果不及時捕捉進程的輸出,會導致JAVA掛住,看似被調用進程沒退出。所以,解決辦法是,啟動進程后,再啟動兩個JAVA線程及時的把被調用進程的輸出截獲。

    一下子,整個世界清爽多了。。。多謝這么仁兄,下面轉一下:



    轉自:http://pudding.sharera.com/blog/BlogTopic/31232.htm

    碰到一個項目需要從Java中運行Perl程序,這個Perl程序調用客戶的Web service,每次發送一個請求,接受一個響應。Java程序中包含多個請求,需要多次調用Perl程序,并且接受和解析響應(這個爛設計可不是我干 的,我實在不明白強大的Java Web Service為什么要弄成這樣,不過客戶是老大)。使用Java Runtime的exec()方法,發現運行一段時間后,進程就被掛起了(之前的響應完全正確)。于是分析原因,發現我在運行exec()方法后,立刻執 行了Process的waitFor()方法,這里出了問題。在網上找到一篇文章講述這個問題:
    地址:http://brian.pontarelli.com/2005/11/11/java-runtime-exec-can-hang/

    Java Runtime exec can hang

    November 11, 2005 on 4:40 pm | In Java |

    The next version of Savant is going to focus heavily on the stand-alone runtime and support for dialects and plugins. Supporting all that is largely handled by using a simple executor framework I wrote around Java 1.4 and lower’s Runtime.exec method. A few things to keep in mind when using this:

    1. Always read from the streams prior to calling waitFor. Otherwise you could end up waiting forever on Windows and other OS platforms whose I/O buffers can’t store enough from standard out and standard error to ensure the program has finished. These platforms will pause the execution of whatever is running until something reads the buffered content from standard out and standard error. I would imagine all platforms suffer from this, but some platforms have larger buffers than others. Needless to say, always read from the streams first.
    2. Always read from standard error first. I ran across a bug where some OS platforms will always open standard out, but never close it. What this means is that if you read from standard out first and the process only writes to standard error, you’ll hang forever waiting to read. If you read from standard error first, you’ll always be okay on these platforms because the OS seems to shutdown standard error. I think however, that the best way to handle all cases is to check both standard error and standard out for readiness and only read from them if they have something to offer. The downside I could see here is that error isn’t ready, but eventually will be.
    可以看出:
    • 永遠要在調用waitFor()方法之前讀取數據流
    • 永遠要先從標準錯誤流中讀取,然后再讀取標準輸出流
    于是將waitFor()方法放在讀取數據流后調用,目前沒有發現什么問題。


    正好解決了我心中的疑問,非常感謝!

    我們的程序一開始就是exec完了接著waitFor(),但bat文件執行不完整:

    Process proc = Runtime.getRuntime().exec(cmd);
                    proc.waitFor();

    后面的build中在waitFor()之前讀取了數據流,bat文件就可以完整執行了:

    Process proc = Runtime.getRuntime().exec(cmd);
         StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "Error");           
                     StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "Output");
                     errorGobbler.start();
                     outputGobbler.start();

         proc.waitFor();

    class StreamGobbler extends Thread {
     InputStream is;

     String type;

     StreamGobbler(InputStream is, String type) {
      this.is = is;
      this.type = type;
     }

     public void run() {
      try {
       InputStreamReader isr = new InputStreamReader(is);
       BufferedReader br = new BufferedReader(isr);
       String line = null;
       while ((line = br.readLine()) != null) {
        if (type.equals("Error"))
         LogManager.logError(line);
        else
         LogManager.logDebug(line);
       }
      } catch (IOException ioe) {
       ioe.printStackTrace();
      }
     }
    }

    TestPrint.bat:

    echo P1=%1  >D:"2.1.2env"2.1.2home"CompuSet"output"TestPrint.log
    echo P2=%2 >>D:"2.1.2env"2.1.2home"CompuSet"output"TestPrint.log
    echo P3=%3 >>D:"2.1.2env"2.1.2home"CompuSet"output"TestPrint.log
    echo P4=%4 >>D:"2.1.2env"2.1.2home"CompuSet"output"TestPrint.log
    echo P5=%5 >>D:"2.1.2env"2.1.2home"CompuSet"output"TestPrint.log
    echo P6=%6 >>D:"2.1.2env"2.1.2home"CompuSet"output"TestPrint.log

    Bad_TestPrint.log:

    P1=C:"xPression"CompuSet"output"MartyTestOut1.afp 
    P2=Literal1
    P3="Rick Skinner"
    P4=Parameter3

    Good_TestPrint.log

    P1=C:"xPression"CompuSet"output"MartyTestOut1.afp 
    P2=Literal1
    P3="Rick Skinner"
    P4=Parameter3
    P5=Parameter4
    P6=Parameter5



    posted on 2009-05-15 21:04 我愛佳娃 閱讀(14244) 評論(4)  編輯  收藏 所屬分類: Perl

    評論:
    # re: 哭了:整一天Java Runtime exec的掛死(不退出)問題,原來是醬子 2009-05-30 13:25 | 墻頭草
    # re: 哭了:整一天Java Runtime exec的掛死(不退出)問題,原來是醬子 2009-07-09 22:18 | 大仙
    我好像也遇到過類似的問題
    是在調用 一個hsqldb的時候,發生過
    好像1.5 才會,  回復  更多評論
      
    # re: 哭了:整一天Java Runtime exec的掛死(不退出)問題,原來是醬子 2013-07-29 06:10 | cheligeer
    同樣發現這類問題,謝謝樓主指明。  回復  更多評論
      
    # re: 哭了:整一天Java Runtime exec的掛死(不退出)問題,原來是醬子[未登錄] 2016-04-16 17:40 | 123
    原來是這樣,多謝樓主  回復  更多評論
      
    主站蜘蛛池模板: 亚洲国产精品精华液| 亚洲中久无码不卡永久在线观看| 亚洲嫩草影院久久精品| xxxxxx日本处大片免费看| 免费国产在线观看| 丁香花免费完整高清观看| 久久久久久久久亚洲| 97人妻精品全国免费视频| 不卡一卡二卡三亚洲| 亚洲一区二区三区四区在线观看| 怡红院免费的全部视频| 亚洲va中文字幕无码久久不卡| 中文成人久久久久影院免费观看| 亚洲AV中文无码字幕色三| 亚洲免费视频网站| 亚洲天堂福利视频| 国产福利在线免费| 亚洲欧洲国产综合AV无码久久| 永久中文字幕免费视频网站| 国产成人综合亚洲绿色| 久久久久久AV无码免费网站| 日韩亚洲Av人人夜夜澡人人爽 | 国产亚洲精品a在线观看app| 亚洲男人天堂2018av| 中国一级毛片视频免费看| 亚洲级αV无码毛片久久精品| 午夜视频免费在线观看| 亚洲精品国产精品国自产网站| 在线a人片天堂免费观看高清| 日韩大片在线永久免费观看网站| 亚洲国产精品激情在线观看| 免费高清国产视频| 麻豆狠色伊人亚洲综合网站| 免费jjzz在线播放国产| 成人A片产无码免费视频在线观看| 97久久精品亚洲中文字幕无码| 最近中文字幕mv免费高清视频7 | 亚洲中文字幕在线第六区| 精品一卡2卡三卡4卡免费视频| 亚洲中文无码av永久| 免费国产综合视频在线看|