1. 程式人生 > >Java執行Shell指令碼超時控制

Java執行Shell指令碼超時控制

Java的Runtime可以執行命令列指令碼,某些特定場合需要對指令碼的執行時間進行控制,防止指令碼某些異常情況下,一直未能正確結束,導致Java主程序掛起。本文的程式對這一過程進行了控制

Java程式碼
  1. import org.slf4j.Logger;   
  2. import org.slf4j.LoggerFactory;   
  3. import java.io.BufferedReader;   
  4. import java.io.IOException;   
  5. import java.io.InputStreamReader;   
  6. publicclass CommandUtils {   
  7. private
    static Logger logger = LoggerFactory.getLogger(CommandUtils.class);   
  8. //default time out, in millseconds
  9. publicstaticfinalint DEFAULT_TIMEOUT = 10 * 1000;   
  10. publicstaticfinalint DEFAULT_INTERVAL = 1000;   
  11. /**  
  12.      * Executes the specified command in a separate process. The method then blocks until the process returned.
     
  13.      * If an error arises during the execution or if the exeecuted process returned an non-null return code,  
  14.      * the content of the process' stderr is returned to the caller. If the execution is fine, null is returned.  
  15.      *  
  16.      * @param command String  
  17.      * @return CommandResult  
  18.      */
  19. public
    static CommandResult exec(String command) {   
  20. long start = System.currentTimeMillis();   
  21. long last;   
  22.         CommandResult commandResult = new CommandResult();   
  23. try {   
  24.             Process process = Runtime.getRuntime().exec(command);   
  25.             process(process, commandResult);   
  26. if (process != null) {   
  27.                 process.destroy();   
  28.             }   
  29.             last = (System.currentTimeMillis() - start) / 1000;   
  30.             logger.info("Execute command [" + command + "], last [" + last + "] s.");   
  31.         } catch (Exception e) {   
  32.             last = (System.currentTimeMillis() - start) / 1000;   
  33.             String error = "Execute command [" + command + "] last [" + last + "] s, failed [" + e.getMessage() + "]";   
  34.             logger.error(error, e);   
  35.             commandResult.setExitValue(CommandResult.EXIT_VALUE_UNEXCEPTED);   
  36.             commandResult.setErrorOutput(error);   
  37.         }   
  38. return commandResult;   
  39.     }   
  40. privatestaticvoid process(Process process, CommandResult commandResult) {   
  41.         BufferedReader errorReader = null;   
  42.         BufferedReader inputReader = null;   
  43. try {   
  44.             errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));   
  45.             inputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));   
  46. //timeout control
  47. long start = System.currentTimeMillis();   
  48. boolean processFinished = false;   
  49. while (System.currentTimeMillis() - start < DEFAULT_TIMEOUT && !processFinished) {   
  50.                 processFinished = true;   
  51. try {   
  52.                     process.exitValue();   
  53.                 } catch (IllegalThreadStateException e) {   
  54. // process hasn't finished yet
  55.                     processFinished = false;   
  56. try {   
  57.                         Thread.sleep(DEFAULT_INTERVAL);   
  58.                     } catch (InterruptedException e1) {   
  59.                         logger.error("Process, failed [" + e.getMessage() + "]", e);   
  60.                     }   
  61.                 }   
  62.             }   
  63. if (!processFinished) {   
  64.                 commandResult.setExitValue(CommandResult.EXIT_VALUE_TIMEOUT);   
  65.                 commandResult.setErrorOutput("Command process timeout");   
  66. return;   
  67.             }   
  68.             commandResult.setExitValue(process.waitFor());   
  69.             StringBuffer sb;   
  70.             String line;   
  71. //parse error info
  72. if (errorReader.ready()) {   
  73.                 sb = new StringBuffer();   
  74. while ((line = errorReader.readLine()) != null) {   
  75.                     sb.append(line);   
  76.                 }   
  77.                 commandResult.setErrorOutput(sb.toString());   
  78.             }   
  79. //parse info
  80. if (inputReader.ready()) {   
  81.                 sb = new StringBuffer();   
  82. while ((line = inputReader.readLine()) != null) {   
  83.                     sb.append(line);   
  84.                 }   
  85.                 commandResult.setInfoOutput(sb.toString());   
  86.             }   
  87.         } catch (Exception e) {   
  88.             String error = "Command process, failed [" + e.getMessage() + "]";   
  89.             logger.error(error, e);   
  90.             commandResult.setExitValue(CommandResult.EXIT_VALUE_UNEXCEPTED);   
  91.             commandResult.setErrorOutput(error);   
  92.         } finally {   
  93. if (errorReader != null) {   
  94. try {   
  95.                     errorReader.close();   
  96.                 } catch (IOException e) {   
  97.                     logger.error("Close BufferedReader, failed [" + e.getMessage() + "]", e);   
  98.                 }   
  99.             }   
  100. if (inputReader != null) {   
  101. try {   
  102.                     inputReader.close();   
  103.                 } catch (IOException e) {   
  104.                     logger.error("Close BufferedReader, failed [" + e.getMessage() + "]", e);   
  105.                 }   
  106.             }   
  107.         }   
  108.     }   
  109. }  
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

 

public class CommandUtils {
    private static Logger logger = LoggerFactory.getLogger(CommandUtils.class);

    //default time out, in millseconds
    public static final int DEFAULT_TIMEOUT = 10 * 1000;
    public static final int DEFAULT_INTERVAL = 1000;

    /**
     * Executes the specified command in a separate process. The method then blocks until the process returned.
     * If an error arises during the execution or if the exeecuted process returned an non-null return code,
     * the content of the process' stderr is returned to the caller. If the execution is fine, null is returned.
     *
     * @param command String
     * @return CommandResult
     */
    public static CommandResult exec(String command) {
        long start = System.currentTimeMillis();
        long last;

        CommandResult commandResult = new CommandResult();

        try {
            Process process = Runtime.getRuntime().exec(command);
            process(process, commandResult);

            if (process != null) {
                process.destroy();
            }

            last = (System.currentTimeMillis() - start) / 1000;
            logger.info("Execute command [" + command + "], last [" + last + "] s.");

        } catch (Exception e) {
            last = (System.currentTimeMillis() - start) / 1000;
            String error = "Execute command [" + command + "] last [" + last + "] s, failed [" + e.getMessage() + "]";
            logger.error(error, e);

            commandResult.setExitValue(CommandResult.EXIT_VALUE_UNEXCEPTED);
            commandResult.setErrorOutput(error);
        }

        return commandResult;
    }

    private static void process(Process process, CommandResult commandResult) {
        BufferedReader errorReader = null;
        BufferedReader inputReader = null;

        try {
            errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            inputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            //timeout control
            long start = System.currentTimeMillis();
            boolean processFinished = false;

            while (System.currentTimeMillis() - start < DEFAULT_TIMEOUT && !processFinished) {
                processFinished = true;
                try {
                    process.exitValue();
                } catch (IllegalThreadStateException e) {
                    // process hasn't finished yet
                    processFinished = false;

                    try {
                        Thread.sleep(DEFAULT_INTERVAL);
                    } catch (InterruptedException e1) {
                        logger.error("Process, failed [" + e.getMessage() + "]", e);
                    }
                }
            }

            if (!processFinished) {
                commandResult.setExitValue(CommandResult.EXIT_VALUE_TIMEOUT);
                commandResult.setErrorOutput("Command process timeout");
                return;
            }

            commandResult.setExitValue(process.waitFor());

            StringBuffer sb;
            String line;

            //parse error info
            if (errorReader.ready()) {
                sb = new StringBuffer();
                while ((line = errorReader.readLine()) != null) {
                    sb.append(line);
                }
                commandResult.setErrorOutput(sb.toString());
            }

            //parse info
            if (inputReader.ready()) {
                sb = new StringBuffer();
                while ((line = inputReader.readLine()) != null) {
                    sb.append(line);
                }
                commandResult.setInfoOutput(sb.toString());
            }

        } catch (Exception e) {
            String error = "Command process, failed [" + e.getMessage() + "]";
            logger.error(error, e);

            commandResult.setExitValue(CommandResult.EXIT_VALUE_UNEXCEPTED);
            commandResult.setErrorOutput(error);

        } finally {
            if (errorReader != null) {
                try {
                    errorReader.close();
                } catch (IOException e) {
                    logger.error("Close BufferedReader, failed [" + e.getMessage() + "]", e);
                }
            }

            if (inputReader != null) {
                try {
                    inputReader.close();
                } catch (IOException e) {
                    logger.error("Close BufferedReader, failed [" + e.getMessage() + "]", e);
                }
            }
        }
    }
}