Apache common exec包的相应使用总结
final Process p = Runtime.getRuntime().exec(command);
new Thread(new Runnable() {
@Override
public void run() {
while (true){
try {
p.exitValue();
break;
} catch (Exception e){
showInfo(System.err,p.getErrorStream());
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true){
try {
p.exitValue();
break;
} catch (Exception e){
showInfo(System.out,p.getInputStream());
}
}
}
}).start();
int exitValue = p.waitFor();
final Long executeSubTaskId = subTaskExecuteContext.getSubTaskExecuteId();
final Long taskStatusId = subTaskExecuteContext.getTaskExecuteContext().getTaskExecuteId(); ByteArrayOutputStream outputStream =
new MzByteArrayOutputStream(executeSubTaskId, taskStatusNotifyCenter, true);
ByteArrayOutputStream errorStream =
new MzByteArrayOutputStream(executeSubTaskId, taskStatusNotifyCenter, false);
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream);
taskThreadPoolExecutor.setStreamHandler(streamHandler); CommandLine commandLine = new CommandLine(new File(executeShellPath)); final TaskProcessInfo taskProcessInfo = new TaskProcessInfo(subTaskExecuteContext.getTaskExecuteContext().getTaskId(),
taskStatusId,
executeSubTaskId);
ProcessManagerDestroyer processManagerDestroyer =
new ProcessManagerDestroyer(
taskProcessInfo, processInfoManager);
taskThreadPoolExecutor.setProcessDestroyer(processManagerDestroyer);
try {
taskThreadPoolExecutor.execute(commandLine, new DefaultExecuteResultHandler() {
@Override
public void onProcessComplete(int exitValue) {
super.onProcessComplete(exitValue);
LOG.info(String.format("Task Process info: %s succeed!", taskProcessInfo));
taskStatusNotifyCenter.notifyEventChanged(new TaskStatusEvent(TaskStatusEventType.TASK_FINISHED,
new TaskEventObject(subTaskExecuteContext.getTaskExecuteContext().getTaskId(),
subTaskExecuteContext.getTaskExecuteContext().getTaskExecuteId(),
subTaskExecuteContext.getSubTaskExecuteId())));
} @Override
public void onProcessFailed(ExecuteException e) {
super.onProcessFailed(e);
LOG.error(e);
LOG.error(String.format("Task Process info: %s failed!", taskProcessInfo));
taskStatusNotifyCenter.notifyEventChanged(new TaskStatusEvent(TaskStatusEventType.TASK_FAILED,
new TaskEventObject(subTaskExecuteContext.getTaskExecuteContext().getTaskId(),
subTaskExecuteContext.getTaskExecuteContext().getTaskExecuteId(),
subTaskExecuteContext.getSubTaskExecuteId())));
}
});
} catch (IOException e) {
throw new BusinessException(e);
}
@Override
public synchronized void write(byte[] b, int off, int len) {
super.write(b, off, len);
writeTimes++;
writeLength += len;
if (writeLength >= MAX_WRITE_LENGTH || writeTimes >= MAX_WRITE_TIMES) {
updateStatus();
this.buf = new byte[32];
writeLength = 0;
writeTimes = 0;
} } @Override
public void flush() throws IOException {
super.flush();
updateStatus();
}
public class ProcessManagerDestroyer implements ProcessDestroyer {
private final ProcessInfoManager processInfoManager;
private final TaskProcessInfo taskProcessInfo;
public ProcessManagerDestroyer(TaskProcessInfo taskProcessInfo, ProcessInfoManager processInfoManager) {
this.taskProcessInfo = taskProcessInfo;
this.processInfoManager = processInfoManager;
}
@Override
public boolean add(Process process) {
processInfoManager.addProcess(taskProcessInfo, process);
return true;
}
@Override
public boolean remove(Process process) {
processInfoManager.removeProcess(taskProcessInfo);
return true;
}
@Override
public int size() {
return processInfoManager.taskCount();
}
process.destroy();
org.apache.commons.exec.ExecuteException: Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "/Users/mazhiqiang/Downloads/1.sh" (in directory "."): error=13, Permission denied)
at org.apache.commons.exec.DefaultExecutor$1.run(DefaultExecutor.java:205)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.IOException: Cannot run program "/Users/xxx/Downloads/1.sh" (in directory "."): error=13, Permission denied
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042)
at java.lang.Runtime.exec(Runtime.java:620)
at org.apache.commons.exec.launcher.Java13CommandLauncher.exec(Java13CommandLauncher.java:61)
at org.apache.commons.exec.DefaultExecutor.launch(DefaultExecutor.java:279)
at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:336)
at org.apache.commons.exec.DefaultExecutor.access$200(DefaultExecutor.java:48)
at org.apache.commons.exec.DefaultExecutor$1.run(DefaultExecutor.java:200)
... 1 more
Caused by: java.io.IOException: error=13, Permission denied
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:185)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)
... 7 more
file.executable()
!/bin/bash
Apache common exec包的相应使用总结的更多相关文章
- apache common包下的StringUtils的join方法
apache common包下的StringUtils的join方法: 关键字:java string array join public static String join(Iterator it ...
- Apache Common DbUtils
前段时间使用了Apache Common DbUtils这个工具,在此留个印,以备不时查看.大家都知道现在市面上的数据库访问层的框架很多,当然很多都是包含了OR-Mapping工作步骤的 例如大家常用 ...
- apache commons Java包简介
更多信息,请参考:http://commons.apache.org/ 一.Commons BeanUtils说明:针对Bean的一个工具集.由于Bean往往是有一堆get和set组成,所以BeanU ...
- apache commons io包基本功能
1. http://jackyrong.iteye.com/blog/2153812 2. http://www.javacodegeeks.com/2014/10/apache-commons-io ...
- WEB文件上传之apache common upload使用(一)
文件上传一个经常用到的功能,它有许多中实现的方案. 页面表单 + RFC1897规范 + http协议上传 页面控件(flash/html5/activeX/applet) + RFC1897规范 + ...
- Apache源码包安装和子配置文件介绍--update.2014-12-5
安装apache: 官网:http://httpd.apache.org/download.cgi#apache24 1.wget http://mirror.bit.edu.cn/apache//h ...
- golang中os/exec包用法
exec包执行外部命令,它将os.StartProcess进行包装使得它更容易映射到stdin和stdout,并且利用pipe连接i/o. 1.func LookPath(file string) ( ...
- org.apache.common.io-FileUtils详解
org.apache.common.io---FileUtils详解 getTempDirectoryPath():返回临时目录路径; public static String getTempDire ...
- Java网络编程:利用apache的HttpClient包进行http操作
本文介绍如何利用apache的HttpClient包进行http操作,包括get操作和post操作. 一.下面的代码是对HttpClient包的封装,以便于更好的编写应用代码. import java ...
随机推荐
- python之阶乘的小例子
现在自己写阶乘是这个样子的 def f(x): return x * f(x-1) if x >1 else 1 后来无意中看到耗子的一篇<Python程序员的进化>的文章, 感脚这 ...
- 企业号验证后携带URL参数跳转问题
http://report.dyxx.com/authorize/weixinlogin.aspx?url=http://user.dyxx.com/Xshow/lsWX.aspx?code=0000 ...
- C++实现线程同步的几种方式
线程同步是指同一进程中的多个线程互相协调工作从而达到一致性.之所以需要线程同步,是因为多个线程同时对一个数据对象进行修改操作时,可能会对数据造成破坏,下面是多个线程同时修改同一数据造成破坏的例子: # ...
- 修改程序版本工具(ResHacker)使用说明20140902
有时候我们需要自己修改dll版本号,那么ResHacker工具可以帮我们在不需要开发帮助下可以自己修改版本号: 工具:直接复制出来即可 1.双击打开工具 2.将dll文件或者exe文件拖拽进来,或者[ ...
- vue自定义指令v-scroll(directive)
vue开发中,很多地方如果说都用到了某一方法,我们就可以进行指令化封装,通过自定义指令来实现这里通过两个例子说明vue-Directive的使用 1.v-focus 主要用来实现页面加载进来的时候文本 ...
- ranch分析学习(二)
紧接上篇,今天我们来分析监督树的工作者,打工仔执行任务的人.废话不多少我们直接进入正题. 3.ranch_server.erl 整个文件的功能主要是存储tcp对应参数的的信息.信息的存储方式采用的 ...
- absolute的left和right的妙用
之前做了一个自定义鼠标右键的布局,做的过程中遇到了一个很有趣的问题,之前一直没有注意到. 目标样式如下: 期初并不知道文字内容需要随机,所以写的时候写“死”了. 所有的内容都是按照设计的四个文字走的, ...
- Android编程实例-获取当前进程名字
下面代码是根据进程id获取进程名字: /** * 根据Pid获取当前进程的名字,一般就是当前app的包名 * * @param context 上下文 * @param pid 进程的id * @re ...
- fedora22 安装fcitx 输入法
<h4>安装fcitx:</h4><blockquote>sudo yum install fcitx fcitx-pinyin fcitx-configtools ...
- HDU3833 YY's new problem 卡时间第一题
Given a permutation P of 1 to N, YY wants to know whether there exists such three elements P[i 1], P ...