zoukankan      html  css  js  c++  java
  • 关于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来进行描述

     

    向前是金,向后是土 其实我只想自由飞翔
  • 相关阅读:
    交换机主要参数详解
    Linksys E 刷Tomato shibby
    802.11n 连接的建议设置是什么?
    Wi-Fi 协议和数率?
    windows xp 不支持Wap2加密方式
    电脑稳定性检测软件
    看懂影片标题,各种电影视频格式标题的含义
    【生活】生活点滴记录
    【自动化测试】关于UI自动化的疑问(记录ing)
    【学习】代码的编码规范
  • 原文地址:https://www.cnblogs.com/joqk/p/3456422.html
Copyright © 2011-2022 走看看