camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Exec
Date Thu, 15 Apr 2010 06:49:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1520/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/Exec">Exec</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <h2><a name="Exec-Execcomponent"></a>Exec component</h2>
<p><b>Available in Camel 2.3</b></p>

<p>The <tt>exec</tt> component can be used to execute a system command.
</p>

<h3><a name="Exec-Dependencies"></a>Dependencies</h3>
<p>Maven users need to add the following dependency to their <tt>pom.xml</tt></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: xml; gutter:
false"><![CDATA[
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
  &lt;artifactId&gt;camel-exec&lt;/artifactId&gt;
  &lt;version&gt;${camel-version}&lt;/version&gt;
&lt;/dependency&gt;
]]></script>
</div></div>
<p>where <tt>${camel-version</tt>} must be replaced by the actual version
of Camel (2.3.0 or higher). </p>

<h3><a name="Exec-URIformat"></a>URI format</h3>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: xml; gutter:
false"><![CDATA[
exec://executable[?options]
]]></script>
</div></div>
<p>where <tt>executable</tt> is the name, or file path, of the system command
that will be executed. If executable name is used (e.g. <tt>exec:java</tt>), the
executable must in the system path.</p>

<h3><a name="Exec-URIoptions"></a>URI options</h3>

<div class="confluenceTableSmall"><table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>args</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> The arguments of the executable. The arguments may be one
or many<br/>
whitespace-separated tokens, that can be quoted with <tt>"</tt>, e.g. <tt>args="arg
1" arg2</tt> will use two arguments <tt>arg 1</tt> and <tt>arg2</tt>.
To include the quotes use <tt>""</tt>, e.g. <tt>args=""arg 1"" arg2</tt>
will use the arguments <tt>"arg 1"</tt> and <tt>arg2</tt>.</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>workingDir</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'>The directory in which the command should be executed. If <tt>null</tt>,
the working directory of the current process will be used.</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>timeout</tt> </td>
<td class='confluenceTd'> <tt>Long.MAX_VALUE</tt></td>
<td class='confluenceTd'>The timeout, in milliseconds, after which the executable should
be terminated. If the executable has has not finished within the timeout, the component will
send a termination request.</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>outFile</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'>The name of a file, created by the executable, that should
be considered as output of the executable. If no <tt>outFile</tt> is set, the
standard output (stdout) of the executable will be considered as output. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>binding</tt> </td>
<td class='confluenceTd'> a <tt>DefaultExecBinding</tt> instance </td>
<td class='confluenceTd'> A reference to a <tt>org.apache.commons.exec.ExecBinding</tt>
in the <a href="http://camel.apache.org/registry.html" rel="nofollow">Registry</a>.</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>commandExecutor</tt> </td>
<td class='confluenceTd'>a <tt>DefaultCommandExecutor</tt> instance </td>
<td class='confluenceTd'> A reference to a <tt>org.apache.commons.exec.ExecCommandExecutor</tt>
in the <a href="http://camel.apache.org/registry.html" rel="nofollow">Registry</a>,
that customizes the command execution. The default command executor utilizes the <a href="http://commons.apache.org/exec/"
rel="nofollow">commons-exec library</a>. It adds a shutdown hook for every executed
command.</td>
</tr>
</tbody></table></div>

<h3><a name="Exec-Messageheaders"></a>Message headers</h3>
<p>The supported headers are defined in <tt>org.apache.camel.component.exec.ExecBinding</tt>.</p>
<div class="confluenceTableSmall"><table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>Name</th>
<th class='confluenceTh'>Type</th>
<th class='confluenceTh'>Message </th>
<th class='confluenceTh'>Description</th>
</tr>
<tr>
<td class='confluenceTd'><tt>ExecBinding.EXEC_COMMAND_EXECUTABLE</tt></td>
<td class='confluenceTd'><tt>String</tt></td>
<td class='confluenceTd'><tt>in</tt></td>
<td class='confluenceTd'> The name of the system command that will be executed. Overrides
the <tt>executable</tt> in the URI.</td>
</tr>
<tr>
<td class='confluenceTd'><tt>ExecBinding.EXEC_COMMAND_ARGS</tt></td>
<td class='confluenceTd'><tt>java.util.List&lt;String&gt;</tt></td>
<td class='confluenceTd'><tt>in</tt></td>
<td class='confluenceTd'>The arguments of the executable. The arguments are used literally,
no quoting is applied. Overrides existing <tt>args</tt> in the URI.</td>
</tr>
<tr>
<td class='confluenceTd'><tt>ExecBinding.EXEC_COMMAND_OUT_FILE</tt></td>
<td class='confluenceTd'><tt>String</tt></td>
<td class='confluenceTd'><tt>in</tt></td>
<td class='confluenceTd'>The name of a file, created by the executable, that should
be considered as output of the executable. Overrides existing <tt>outFile</tt>
in the URI.</td>
</tr>
<tr>
<td class='confluenceTd'><tt>ExecBinding.EXEC_COMMAND_TIMEOUT</tt></td>
<td class='confluenceTd'><tt>long</tt></td>
<td class='confluenceTd'><tt>in</tt></td>
<td class='confluenceTd'>The timeout, in milliseconds, after which the executable should
be terminated. Overrides existing <tt>timeout</tt> in the URI.</td>
</tr>
<tr>
<td class='confluenceTd'><tt>ExecBinding.EXEC_COMMAND_WORKING_DIR</tt></td>
<td class='confluenceTd'><tt>String</tt></td>
<td class='confluenceTd'><tt>in</tt></td>
<td class='confluenceTd'>The directory in which the command should be executed. Overrides
existing <tt>workingDir</tt> in the URI.</td>
</tr>
<tr>
<td class='confluenceTd'><tt>ExecBinding.EXEC_EXIT_VALUE</tt></td>
<td class='confluenceTd'><tt>int</tt></td>
<td class='confluenceTd'><tt>out</tt></td>
<td class='confluenceTd'>The value of this header is the <em>exit value</em>
of the executable. Typically not-zero exit values indicates abnormal termination. Note that
the exit value is OS-dependent.</td>
</tr>
<tr>
<td class='confluenceTd'><tt>ExecBinding.EXEC_STDERR</tt></td>
<td class='confluenceTd'><tt>java.io.InputStream</tt></td>
<td class='confluenceTd'><tt>out</tt></td>
<td class='confluenceTd'>The value of this header points to the standard error stream
(stderr) of the executable. If no stderr is written, the value is <tt>null</tt>.</td>
</tr>
</tbody></table></div>

<h3><a name="Exec-Messagebody"></a>Message body</h3>
<p>If the <tt>in</tt> message body, that that the <tt>Exec</tt>
component receives, is convertible to <tt>java.io.InputStream</tt>, it is used
to feed input of the executable via its stdin. After the execution, <a href="http://camel.apache.org/exchange.html"
rel="nofollow">the message body</a> is the result of the execution, that is <tt>org.apache.camel.components.exec.ExecResult</tt>
instance containing the stdout, stderr, exit value and out file. The component supports the
following <tt>ExecResult</tt> <a href="http://camel.apache.org/type-converter.html"
rel="nofollow">type converters</a> for convenience:</p>

<div class="confluenceTableSmall"><table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'>From</th>
<th class='confluenceTh'>To</th>
</tr>
<tr>
<td class='confluenceTd'><tt>ExecResult</tt> </td>
<td class='confluenceTd'> <tt>java.io.InputStream</tt></td>
</tr>
<tr>
<td class='confluenceTd'><tt>ExecResult</tt> </td>
<td class='confluenceTd'> <tt>String</tt></td>
</tr>
<tr>
<td class='confluenceTd'><tt>ExecResult</tt> </td>
<td class='confluenceTd'> <tt>byte []</tt></td>
</tr>
<tr>
<td class='confluenceTd'><tt>ExecResult</tt> </td>
<td class='confluenceTd'> <tt>org.w3c.dom.Document</tt></td>
</tr>
</tbody></table>
<p>If out file is used (the endpoint is configured with <tt>outFile</tt>,
or there is <tt>ExecBinding.EXEC_COMMAND_OUT_FILE</tt> header) the converters
return the content of the out file. If no out file is used, then the converters will use the
stdout of the process for conversion to the target type. For example refer to the <a href="#Exec-usage">usage
examples</a>.</p></div>

<p><a name="Exec-usage"></a></p>
<h3><a name="Exec-Usageexamples"></a>Usage examples</h3>
<h4><a name="Exec-Executingwordcount%28Linux%29"></a>Executing word count
(Linux)</h4>
<p>The example below executes <tt>wc</tt> (word count, Linux) to count the
words in file <tt>/usr/share/dict/words</tt>. The word count (output) is written
in the standart output stream of <tt>wc</tt>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: java; gutter:
false"><![CDATA[
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
     }
});
]]></script>
</div></div> 

<h4><a name="Exec-Executingjava"></a>Executing java</h4>
<p>The example below executes <tt>java</tt> with 2 arguments: <tt>-server</tt>
and <tt>-version</tt>, provided that <tt>java</tt> is in the system
path. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: java; gutter:
false"><![CDATA[
from("direct:exec")
.to("exec:java?args=-server -version")
]]></script>
</div></div>
<p>The example below executes <tt>java</tt> in <tt>c:/temp</tt>
with 3 arguments: <tt>-server</tt>, <tt>-version</tt> and the sytem
property <tt>user.name</tt>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: java; gutter:
false"><![CDATA[
from("direct:exec")
.to("exec:c:/program files/jdk/bin/java?args=-server -version -Duser.name=Camel&amp;workingDir=c:/temp")
]]></script>
</div></div>
<h4><a name="Exec-ExecutingAntscripts"></a>Executing Ant scripts</h4>
<p>The following example executes <a href="http://ant.apache.org/" rel="nofollow">Apache
Ant</a> (Windows only) with the build file <tt>CamelExecBuildFile.xml</tt>,
provided that <tt>ant.bat</tt> is in the system path, and that <tt>CamelExecBuildFile.xml</tt>
is in the current directory.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: java; gutter:
false"><![CDATA[
from("direct:exec")
.to(exec:ant.bat?args=-f CamelExecBuildFile.xml")
]]></script>
</div></div>
<p>In the next example, the <tt>ant.bat</tt> command, redirects the ant
output to <tt>CamelExecOutFile.txt</tt> with <tt>-l</tt>. The file
<tt>CamelExecOutFile.txt</tt> is used as out file with <tt>outFile=CamelExecOutFile.txt</tt>.
The example assumes that <tt>ant.bat</tt> is in the system path, and that <tt>CamelExecBuildFile.xml</tt>
is in the current directory.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: java; gutter:
false"><![CDATA[
from("direct:exec")
.to("exec:ant.bat?args=-f CamelExecBuildFile.xml -l CamelExecOutFile.txt&amp;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
     }
  });
]]></script>
</div></div>

<h3><a name="Exec-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Exec">View Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=17268767&revisedVersion=4&originalVersion=3">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/Exec?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message