98.7. 使用例
98.7.1. ワードカウントの実行 (Linux)
以下の例では、wc (単語カウント、Linux) を実行して、ファイル /usr/share/dict/words 内の単語をカウントします。単語数 (出力) は、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
}
});98.7.2. java の実行
次の例では、java がシステムパスにある場合、-server と -version の 2 つの引数を指定して java を実行します。
from("direct:exec")
.to("exec:java?args=-server -version")
以下の例では、3 つの引数 -server、-version、およびシステムプロパティー user.name を使用して c:\temp で java を実行します。
from("direct:exec")
.to("exec:c:/program files/jdk/bin/java?args=-server -version -Duser.name=Camel&workingDir=c:/temp")98.7.3. Ant スクリプトの実行
次の例では、ant.bat がシステムパスにあり、CamelExecBuildFile.xml が現在のディレクトリーにある場合に、ビルドファイル CamelExecBuildFile.xml を使用して Apache Ant (Windows のみ) を実行します。
from("direct:exec")
.to("exec:ant.bat?args=-f CamelExecBuildFile.xml")
次の例では、ant.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
}
});98.7.4. echo の実行 (Windows)
echo や dir などのコマンドは、オペレーティングシステムのコマンドインタープリターでのみ実行できます。この例は、Windows でこのようなコマンド (echo) を実行する方法を示しています。
from("direct:exec").to("exec:cmd?args=/C echo echoString")