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 ...
随机推荐
- windows下创建文件夹链接
mklink百度百科https://baike.baidu.com/item/mklink/566760?fr=aladdin 创建文件夹链接mklink /d "C:\Users\Admi ...
- New Concept English three(16)
35w/m 43 Mary and her husband Dimitri lived in the tiny village of Perachora in southern Greece. One ...
- CDN加速的实现 --- varnish
一.什么是CDN cdn全称为内容分发网络(Content Delivery Network).基本思想是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,是内容传输地更快.更稳定.通过在 ...
- php-resque 简单的php消息队列
摘要: 消息队列是个好东西,各种×××MQ很多.然而看一下它们的文档,你得吓尿,什么鬼,我只是想用它触发个短信接口而已. 幸好也有简单的.这次是php-resque 安装 首先这货需要在linux下跑 ...
- Http权威指南(服务器、缓存)
对于web服务器(软件)大家应该不会陌生,常见的web服务器有Apache.IIS.Tomcat.Nginx.Jetty等等. 1.基本功能 几乎所有的web服务器都会执行以下几项同样的任务: 1.建 ...
- 如何将局域网中的 windows 硬盘挂载到 linux 系统中
1.共享windows上的E盘 2.linux上执行 mount //192.168.3.181/e /tmp/test -o username=dell,,password=abcdef 3.保证两 ...
- BZOJ5450: 轰炸(水题,Tarjan缩点求最长路)
5450: 轰炸 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 43 Solved:18[Submit][Status][Discuss] Desc ...
- JPA级联(一对一 一对多 多对多)注解【实际项目中摘取的】并非自己实际应用
下面把项目中的用户类中有个:一对一 一对多 多对多的注解对应关系列取出来用于学习 说明:项目运行正常 问题类:一对多.一对一.多对多 ============一对多 一方的设置 @One ...
- BZOJ1014 JSOI2008 火星人prefix 【非旋转Treap】*
BZOJ1014 JSOI2008 火星人prefix Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符 ...
- 定义文档兼容性、让IE按指定版本解析我们的页面
http://blog.useasp.net/archive/2013/05/29/x-UA-compatible-defining-document-compatibility-emulate-ie ...