99.7. 使用示例
99.7.1. 执行字计数(Linux) 复制链接链接已复制到粘贴板!
以下示例执行 wc (字数、Linux)来统计文件 /usr/share/dict/words 中的词语。单词 count (输出)写入 wc 的标准输出流。
from("direct:exec")
.to("exec:wc?args=--words /usr/share/dict/words")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
// By default, the body is ExecResult instance
assertIsInstanceOf(ExecResult.class, exchange.getIn().getBody());
// Use the Camel Exec String type converter to convert the ExecResult to String
// In this case, the stdout is considered as output
String wordCountOutput = exchange.getIn().getBody(String.class);
// do something with the word count
}
});
99.7.2. 执行 java 复制链接链接已复制到粘贴板!
以下示例执行带有 2 参数: -server 和 -version 的 java,只要 java 在系统路径中。
from("direct:exec")
.to("exec:java?args=-server -version")
以下示例在 c:\temp 中使用 3 参数执行 java : -server,-version 和 sytem property user.name。
from("direct:exec")
.to("exec:c:/program files/jdk/bin/java?args=-server -version -Duser.name=Camel&workingDir=c:/temp")
99.7.3. 执行 Ant 脚本 复制链接链接已复制到粘贴板!
以下示例使用构建文件 CamelExecBuildFile.xml 执行 Apache Ant (仅限 Windows),只要 ant.bat 在系统路径中,并且 CamelExecBuildFile.xml 位于当前目录中。
from("direct:exec")
.to("exec:ant.bat?args=-f CamelExecBuildFile.xml")
在下一个示例中,t.bat 命令将其输出重定向到 带有 -l 的 CamelExecOutFile.txt。文件 CamelExecOutFile.txt 用作 outFile=CamelExecOutFile.txt 文件。该示例假定 ant.bat 位于系统路径中,并且 CamelExecBuildFile.xml 位于当前目录中。
from("direct:exec")
.to("exec:ant.bat?args=-f CamelExecBuildFile.xml -l CamelExecOutFile.txt&outFile=CamelExecOutFile.txt")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
InputStream outFile = exchange.getIn().getBody(InputStream.class);
assertIsInstanceOf(InputStream.class, outFile);
// do something with the out file here
}
});
99.7.4. 执行 echo (Windows) 复制链接链接已复制到粘贴板!
echo 和 dir 等命令只能通过操作系统的命令解释器来执行。本例演示了如何在 Windows 中执行此类命令 - echo -。
from("direct:exec").to("exec:cmd?args=/C echo echoString")