【问题标题】:Streaming output with commons-exec?使用 commons-exec 流式输出?
【发布时间】:2010-04-23 22:11:54
【问题描述】:

谁能给我一个示例,说明如何流式传输使用DefaultExecutor 执行的外部程序的输出?我没有找到任何描述如何执行此操作的文档。

我的外部进程将运行几个小时,因此仅获取所有输出数据是不可行的;它必须是流式传输的。

【问题讨论】:

    标签: java streaming apache-commons-exec


    【解决方案1】:

    注意:此解决方案是同步的,因此不会流式传输。您需要在单独的线程中阅读,或者使用异步版本的执行命令。

    private InputStream getStream() {
    
     String dataParsingCommand = "java";
    
    PipedOutputStream output = new PipedOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(output);
    
    CommandLine cl = CommandLine.parse(command);
    cl.addArgument("-jar");
    cl.addArgument(dataParserPath);
    
    DefaultExecutor exec = new DefaultExecutor();
    DataInputStream is = null;
    try {
        is = new DataInputStream(new PipedInputStream(output));
        exec.setStreamHandler(psh);
        exec.execute(dataParserCommandLine);
    } catch (ExecuteException ex) {
    } catch (IOException ex) {
    }
    
    return is;
    }
    

    【讨论】:

    【解决方案2】:

    以下是一些使用 Runtime.exec 的示例代码。使其适应您的使用会很简单。 来自http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4

    import java.util.*;
    import java.io.*;
    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)
                    System.out.println(type + ">" + line);    
                } catch (IOException ioe)
                  {
                    ioe.printStackTrace();  
                  }
        }
    }
    
    
    public class GoodWindowsExec
    {
        public static void main(String args[])
        {
            if (args.length < 1)
            {
                System.out.println("USAGE: java GoodWindowsExec <cmd>");
                System.exit(1);
            }
    
            try
            {            
                String osName = System.getProperty("os.name" );
                String[] cmd = new String[3];
                if( osName.equals( "Windows NT" ) )
                {
                    cmd[0] = "cmd.exe" ;
                    cmd[1] = "/C" ;
                    cmd[2] = args[0];
                }
                else if( osName.equals( "Windows 95" ) )
                {
                    cmd[0] = "command.com" ;
                    cmd[1] = "/C" ;
                    cmd[2] = args[0];
                }
    
                Runtime rt = Runtime.getRuntime();
                System.out.println("Execing " + cmd[0] + " " + cmd[1] 
                                   + " " + cmd[2]);
                Process proc = rt.exec(cmd);
                // any error message?
                StreamGobbler errorGobbler = new 
                    StreamGobbler(proc.getErrorStream(), "ERROR");            
    
                // any output?
                StreamGobbler outputGobbler = new 
                    StreamGobbler(proc.getInputStream(), "OUTPUT");
    
                // kick them off
                errorGobbler.start();
                outputGobbler.start();
    
                // any error???
                int exitVal = proc.waitFor();
                System.out.println("ExitValue: " + exitVal);        
            } catch (Throwable t)
              {
                t.printStackTrace();
              }
        }
    }  
    

    编辑:这并不能完全回答问题,因为它使用 JDK 类,但它可以工作。

    【讨论】:

    • 问题是如何使用 commons-exec 完成进程调用,它封装了此答案中显示的大部分内容。
    猜你喜欢
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    相关资源
    最近更新 更多