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, 09 Feb 2012 12:31:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/Exec">Exec</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~mkolev">Mitko
Kolev</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >*Available in Camel 2.3* <br>
<br></td></tr>
            <tr><td class="diff-changed-lines" >The {{exec}} component can be
used to execute <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">a</span>
system <span class="diff-changed-words">command<span class="diff-added-chars"style="background-color:
#dfd;">s</span>.</span> <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h3. Dependencies <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <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 system commands. </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">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
  <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
  <span class="code-tag">&lt;artifactId&gt;</span>camel-exec<span class="code-tag">&lt;/artifactId&gt;</span>
  <span class="code-tag">&lt;version&gt;</span>${camel-version}<span
class="code-tag">&lt;/version&gt;</span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</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">
<pre class="code-xml">
exec://executable[?options]
</pre>
</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"><div class='table-wrap'>
<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 execution has not completed 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 its output. If no <tt>outFile</tt> is set, the standard output
(stdout) of the executable will be used instead. </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" class="external-link" 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" class="external-link" rel="nofollow">Registry</a>
that customizes the command execution. The default command executor utilizes the <a href="http://commons.apache.org/exec/"
class="external-link" rel="nofollow">commons-exec library</a>, which adds a shutdown
hook for every executed command.</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>useStderrOnEmptyStdout</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> A boolean indicating that when <tt>stdout</tt>
is empty, this component will populate the Camel Message Body with <tt>stderr</tt>.
This behavior is disabled (<tt>false</tt>) by default. </td>
</tr>
</tbody></table>
</div>
</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"><div class='table-wrap'>
<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
<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'>Command-line arguments to pass to the executed process. The
arguments are used literally - no quoting is applied. Overrides any existing <tt>args</tt>
in the URI.</td>
</tr>
<tr>
<td class='confluenceTd'><tt>ExecBinding.EXEC_COMMAND_ARGS</tt></td>
<td class='confluenceTd'><tt>String</tt></td>
<td class='confluenceTd'><tt>in</tt></td>
<td class='confluenceTd'> <b>Camel 2.5:</b> The arguments of the executable
as a Single string where each argument is whitespace separated (see <tt>args</tt>
in URI option). The arguments are used literally, no quoting is applied. Overrides any 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 its output. Overrides any 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 any 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
any 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. Non-zero exit values typically indicate 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>
<tr>
<td class='confluenceTd'><tt>ExecBinding.EXEC_USE_STDERR_ON_EMPTY_STDOUT</tt></td>
<td class='confluenceTd'><tt>boolean</tt></td>
<td class='confluenceTd'><tt>in</tt></td>
<td class='confluenceTd'> Indicates that when <tt>stdout</tt> is empty,
this component will populate the Camel Message Body with <tt>stderr</tt>. This
behavior is disabled (<tt>false</tt>) by default. </td>
</tr>
</tbody></table>
</div>
</div>

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

<div class="confluenceTableSmall"><div class='table-wrap'>
<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>
</div>

<p>If an out file is specified (in the endpoint via <tt>outFile</tt> or
the message headers via <tt>ExecBinding.EXEC_COMMAND_OUT_FILE</tt>), converters
will return the content of the out file. If no out file is used, then this component will
convert the stdout of the process to the target type. For more details, please refer to the
<a href="#Exec-usage">usage examples</a> below.</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
to the standard output stream of <tt>wc</tt>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:exec"</span>)
.to(<span class="code-quote">"exec:wc?args=--words /usr/share/dict/words"</span>)
.process(<span class="code-keyword">new</span> Processor() {
     <span class="code-keyword">public</span> void process(Exchange exchange)
<span class="code-keyword">throws</span> Exception {
       <span class="code-comment">// By <span class="code-keyword">default</span>,
the body is ExecResult instance
</span>       assertIsInstanceOf(ExecResult.class, exchange.getIn().getBody());
       <span class="code-comment">// Use the Camel Exec <span class="code-object">String</span>
type converter to convert the ExecResult to <span class="code-object">String</span>
</span>       <span class="code-comment">// In <span class="code-keyword">this</span>
<span class="code-keyword">case</span>, the stdout is considered as output
</span>       <span class="code-object">String</span> wordCountOutput =
exchange.getIn().getBody(<span class="code-object">String</span>.class);
       <span class="code-comment">// <span class="code-keyword">do</span>
something with the word count
</span>     }
});
</pre>
</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">
<pre class="code-java">
from(<span class="code-quote">"direct:exec"</span>)
.to(<span class="code-quote">"exec:java?args=-server -version"</span>)
</pre>
</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">
<pre class="code-java">
from(<span class="code-quote">"direct:exec"</span>)
.to(<span class="code-quote">"exec:c:/program files/jdk/bin/java?args=-server -version
-Duser.name=Camel&amp;workingDir=c:/temp"</span>)
</pre>
</div></div>
<h4><a name="Exec-ExecutingAntscripts"></a>Executing Ant scripts</h4>
<p>The following example executes <a href="http://ant.apache.org/" class="external-link"
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">
<pre class="code-java">
from(<span class="code-quote">"direct:exec"</span>)
.to(<span class="code-quote">"exec:ant.bat?args=-f CamelExecBuildFile.xml"</span>)
</pre>
</div></div>
<p>In the next example, the <tt>ant.bat</tt> command redirects its output
to <tt>CamelExecOutFile.txt</tt> with <tt>-l</tt>. The file <tt>CamelExecOutFile.txt</tt>
is used as the 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">
<pre class="code-java">
from(<span class="code-quote">"direct:exec"</span>)
.to(<span class="code-quote">"exec:ant.bat?args=-f CamelExecBuildFile.xml -l CamelExecOutFile.txt&amp;outFile=CamelExecOutFile.txt"</span>)
.process(<span class="code-keyword">new</span> Processor() {
     <span class="code-keyword">public</span> void process(Exchange exchange)
<span class="code-keyword">throws</span> Exception {
        InputStream outFile = exchange.getIn().getBody(InputStream.class);
        assertIsInstanceOf(InputStream.class, outFile);
        <span class="code-comment">// <span class="code-keyword">do</span>
something with the out file here
</span>     }
  });
</pre>
</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="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Exec">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=17268767&revisedVersion=11&originalVersion=10">View
Changes</a>
                |
        <a href="https://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