Apache commonsexec提供一些常用的方法用来执行外部进程。Apache commons exec库提供了监视狗Watchdog来设监视进程的执行超时,同时也还实现了同步和异步功能。
Apache commonsexec涉及到多线程,比如新启动一个进程,Java中需要再开三个线程来处理进程的三个数据流,分别是标准输入,标准输出和错误输出。
需要使用该功能需要引入commons-exec-1.3.jar包,目前最新的版本为1.3版本。
在日常工作和生活中,我们经常需要用到网络,网络有时候会出现不稳定的情况,这个时候我们可以用ping命令查看网络情况,如果需要自动化检测网络,那么下面这个例子可以参考。
- packagetest.ffm83.commons.exec;
- importorg.apache.commons.exec.CommandLine;
- importorg.apache.commons.exec.DefaultExecutor;
- importorg.apache.commons.exec.PumpStreamHandler;
- importorg.apache.commons.io.output.ByteArrayOutputStream;
- /**
- * 通过commons exec启动ping命令来查看网络情况
- * @author 范芳铭
- */
- public classEasyExecPing {
- public static void main(String[] args){
- EasyExecPing exec = newEasyExecPing();
- exec.ping("127.0.0.1");
- }
- public void ping(String ip) {
- String command = "ping" + ip;
- try {
- ByteArrayOutputStreamoutputStream = new ByteArrayOutputStream();
- ByteArrayOutputStreamerrorStream = new ByteArrayOutputStream();
- //命令行处理
- CommandLinecommandline = CommandLine.parse(command);
- //进行执行体
- DefaultExecutor exec= new DefaultExecutor();
- exec.setExitValues(null);
- PumpStreamHandlerstreamHandler = new PumpStreamHandler(
- outputStream,errorStream);
- exec.setStreamHandler(streamHandler);
- exec.execute(commandline);//执行
- String out =outputStream.toString("gbk");
- String error =errorStream.toString("gbk");
- System.out.println(out);
- System.err.println(error);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
运行情况如下:
正在 Ping 127.0.0.1 具有 32 字节的数据:
来自 127.0.0.1 的回复: 字节=32 时间<1msTTL=64
来自 127.0.0.1 的回复: 字节=32 时间<1msTTL=64
来自 127.0.0.1 的回复: 字节=32 时间<1msTTL=64
来自 127.0.0.1 的回复: 字节=32 时间<1msTTL=64
127.0.0.1 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 0ms,平均 = 0ms
结果非常不错。但是ping命令还有其他写法,比如ping 127.0.0.1 –t ,这个表示一直查看。加到我们的程序中试一下。
我泡了一杯茶,看了下,程序还在执行;
召集了一次项目例会,会开完了,程序还在执行;
看下时间,从开始运行到现在已经过去了3小时。
一不小心就搞出了死循环了,赶紧想办法吧。
开头的地方提到,有监视狗Watchdog可以控制超时,我们试一下吧。
加入监控狗的ping代码:
- packagetest.ffm83.commons.exec;
- importorg.apache.commons.exec.CommandLine;
- importorg.apache.commons.exec.DefaultExecutor;
- importorg.apache.commons.exec.ExecuteWatchdog;
- importorg.apache.commons.exec.PumpStreamHandler;
- importorg.apache.commons.io.output.ByteArrayOutputStream;
- /**
- * 通过commons exec启动ping命令来查看网络情况
- * @author 范芳铭
- */
- public classEasyExecPing {
- public static void main(String[] args){
- EasyExecPing exec = newEasyExecPing();
- exec.ping("127.0.0.1-t");
- }
- public void ping(String ip) {
- String command = "ping" + ip;
- try {
- ByteArrayOutputStreamoutputStream = new ByteArrayOutputStream();
- ByteArrayOutputStreamerrorStream = new ByteArrayOutputStream();
- //命令行处理
- CommandLinecommandline = CommandLine.parse(command);
- //进行执行体
- DefaultExecutor exec= new DefaultExecutor();
- exec.setExitValues(null);
- //利用监视狗来设置超时
- ExecuteWatchdogwatchdog = new ExecuteWatchdog(60000);
- exec.setWatchdog(watchdog);
- PumpStreamHandlerstreamHandler = new PumpStreamHandler(
- outputStream,errorStream);
- exec.setStreamHandler(streamHandler);
- exec.execute(commandline);//执行
- String out =outputStream.toString("gbk");
- String error =errorStream.toString("gbk");
- System.out.println(out);
- System.err.println(error);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
运行结果如下:
正在 Ping 127.0.0.1 具有 32 字节的数据:
来自 127.0.0.1 的回复: 字节=32 时间<1msTTL=64
来自 127.0.0.1 的回复: 字节=32 时间<1msTTL=64
来自 127.0.0.1 的回复: 字节=32 时间<1msTTL=64
来自 127.0.0.1 的回复: 字节=32 时间<1msTTL=64
……
来源: http://www.phpxs.com/code/1002394/