问题的提出:

  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();
}
}

修改之后的代码:

 //变为成员变量方式
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();
}
}

实验结果:

同时处理100条语音。

第一个用时50min。

第二个用时25min。

优势:

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

 

关于java调用linux shell 的问题的更多相关文章

  1. java 调用bash shell脚本阻塞的小问题的解决

    java  调用bash shell脚本阻塞的小问题的解决 背景 使用java实现的web端,web端相应用户的界面操作,使用java调用bash实现的shell脚本进行实际的操作,操作完成返回执行结 ...

  2. Java调用Linux命令(cd的处理)

    一.Java调用Linux系统的命令非常简单 这是一个非常常用的调用方法示例: public String executeLinuxCmd(String cmd) { System.out.print ...

  3. Java调用Linux命令执行

    调用方式 Java调用linux命令执行的方式有两种,一种是直接调用linux命令,一种是将linux命令写到.sh脚本中,然后调用脚本执行. 详细说明 直接调用:使用java中lang包下面的Run ...

  4. java调用Linux执行Python爬虫,并将数据存储到elasticsearch--(环境脚本搭建)

    java调用Linux执行Python爬虫,并将数据存储到elasticsearch中 一.以下博客代码使用的开发工具及环境如下: 1.idea: 2.jdk:1.8 3.elasticsearch: ...

  5. java调用linux下的so库

    1.编写java类 public class Abc { static { System.loadLibrary("abc"); } public native static St ...

  6. java调用Linux命令报错:java.io.IOException: Cannot run program "ps": CreateProcess error=2, ?????????

    在idea里面,java代码:Runtime.getRuntime().exec("ps -aux") 是因为默认是用windows平台运行了,所以报错,得改成调用Linux平台运 ...

  7. python 调用Linux shell

    有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的.那么我们使用Python如何调用Linux的Shell命令?下面来介绍几种常用的方法: 1. os 模块 ...

  8. Java调用Linux下的shell命令并将结果以流的形式返回

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

  9. python 之调用Linux shell命令及相关高级应用

    最近根据老大要求,将数据进行同步备份,结合第三方提供的工具.第三方服务其实是有python demo的,本想研究下实际的python sdk搞个demo开发的,但是发现有些组建装起来确实头大,而且本公 ...

随机推荐

  1. Delphi7中编译提示“Unsafe type 'PChar'”的原因及处理办法

    delphi7中加入了对.net的支持 在.net中是没有指针的(托管环境中),所以指针都是不安全的,不符合.net规范 所以d7里有警告,可以不管它 DELPHI7已经考虑到了移植到点NET的问题, ...

  2. Python之Redis操作

    1.安装驱动 输入命令:pip install redis 2.直接使用驱动 import redis r = redis.StrictRedis(host='127.0.0.1', port=637 ...

  3. AngularJs记录学习03

    AngularJs的路由是一个组件,需要自己额外添加,在目录/src/ngRoute中 三个文件route.js,routeParams.js,ngView.js <html> <h ...

  4. ROS多个master消息互通

    需求 有时候我们需要有几个不同的master, 他们之间要交换topic的内容,这时候就不能使用ros自带的设置同一个master的方法. 我们的处理方法是,构造一个client和一个server,他 ...

  5. DB2递归查询

    斐波纳契数列,又称黄金分割数列,指的是这样一个数列:1.1.2.3.5.8.13.21.……在数学上,斐波纳契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=F(n-1)+F(n-2)(n&g ...

  6. 记录bigdesk中ElasticSearch的性能参数

    定时采集bigdesk中的Elasticsearch性能参数,并保存到数据库或ELK,以便于进行长期监控. 基于python脚本实现,脚本如下: #coding=gbk import httplibi ...

  7. R语言连接MYSQL

    操作系统:centos 6.4 64bit R语言可以使用RMySQL来连接Mysql数据库,直接使用数据库里面的数据生成图像. 这个是RMYSQL的说明: http://cran.r-project ...

  8. MVC4.0 解决Controllers与Areas中控制器不能同名问题

    在使用MVC4.0的时候,难免会遇到在根目录下的Controllers中添加的控制器名称可能会跟在Areas中的某个区域下的控制器名称一样.这个时候访问Areas下面的Controller/Actio ...

  9. homework-01 "最大子数组之和"的解决过程

    看到这个题目,我首先想到就是暴力解决 求出所有的子数组的和,取出最大值即可 但其中是可以有优化的 如 子数组[3:6]可以用[3:5]+[6]来计算 即可以将前面的计算结果保留下来,减少后面的重复计算 ...

  10. 权重轮询调度算法(Weighted Round-Robin Scheduling)-C#实现

    在多台机器实现负载均衡的时候,存在调度分配的问题. 如果服务器的配置的处理能力都一致的话,平均轮询分配可以直接解决问题,然而有些时候机器的处理能力是不一致的. 假如有2台机器 A和B , A的处理能力 ...