关于java调用linux shell 的问题

问题的提出:

  1. shell脚本要做离线的数据处理任务
  2. java调用脚本,将这种处理任务封装成webservice

特点

  1. shell处理单个时间长
  2. 每次要处理文件量大

这里目前只做调用分析:

原来的:

private  void  runShell(String cmd){
                try{

                logger.info("the command "+cmd);
                // create a process for the shell
                ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
                pb.redirectErrorStream(true); // use this to capture messages sent to stderr
                Process shell = pb.start();
                InputStream shellIn = shell.getInputStream(); // this captures the output from the command
                int shellExitStatus = 0;
                try {
                     shellExitStatus = shell.waitFor();
                } catch (InterruptedException e) {
                        e.printStackTrace();
                } // wait for the shell to finish and get the return code
                
//                // at this point you can process the output issued by the command
//                // for instance, this reads the output and writes it to System.out:
                int c;
                while ((c = shellIn.read()) != -1) {
                        logger.info("shell read value:"+c);
                       }
                // close the stream
                        shellIn.close();
                        logger.info(" *** End *** "+shellExitStatus);
                
                logger.info("pb command "+pb.command());
                logger.info("pb command  dir  "+pb.directory());
                logger.info(pb.environment());
                logger.info(System.getenv());
                logger.info(System.getProperties());
                }
                catch (IOException ignoreMe)
                {
                        ignoreMe.printStackTrace();
                }
        }
View Code

 

修改之后的代码:

 //变为成员变量方式
 protected volatile Process process;

 private void runShell(String cmd) {
        try {
            logger.info("the command " + cmd);
            // create a process for the shell
            ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
            process = pb.start();
            final InputStream inputStream = process.getInputStream();
            final InputStream errorStream = process.getErrorStream();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try{
                        BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
                        String line;
                        while((line=reader.readLine())!=null){
//                            logConsole(line);
                            logger.debug(line);
                        }
                    }catch(Exception e){
//                        log(e);
                        e.printStackTrace();
                        logger.debug("接收日志出错,推出日志接收");
                    }
                }
            },"one").start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        BufferedReader reader=new BufferedReader(new InputStreamReader(errorStream));
                        String line;
                        while((line=reader.readLine())!=null){
//                                logConsole(line);
                                logger.debug(line);
                        }
                    } catch (Exception e) {
//                            log(e);
                            e.printStackTrace();
                            logger.debug("接收日志出错,推出日志接收");
                        }
                }
            },"error").start();
//            InputStream shellIn = process.getInputStream(); // this captures the output from the command
            int shellExitStatus = 0;
            try {
                //等待程序结果
                shellExitStatus = process.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally{
                process=null;
            } 
        } catch (IOException ignoreMe) {
            ignoreMe.printStackTrace();
        }
    }
View Code

 

实验结果:

同时处理100条语音。

第一个用时50min。

第二个用时25min。

优势:

  1. 使用processbuild 构造基于os的进程
  2. 使用process作为类变量并且使用volatile来进行描述

 

posted @ 2013-12-03 20:06  joqk  阅读(330)  评论(0编辑  收藏  举报