Date: Tue, 19 Mar 2024 04:43:54 +0000 (UTC) Message-ID: <1983220573.54305.1710823434573@cwiki-he-fi.apache.org> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_54304_702540002.1710823434573" ------=_Part_54304_702540002.1710823434573 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Available in Camel 2.3
The exec
component can be used to execute system commands.<=
/p>
Maven users need to add the following dependency to their pom.xml<=
/code>
<dependenc= y> <groupId>org.apache.camel</groupId> <artifactId>camel-exec</artifactId> <version>${camel-version}</version> </dependency>
where ${camel-version
} must be replaced by the actual versi=
on of Camel (2.3.0 or higher).
exec://execut= able[?options]
where executable
is the name, or file path, of the system c=
ommand that will be executed. If executable name is used (e.g. exec:j=
ava
), the executable must in the system path.
Name |
Default value |
Description |
---|---|---|
|
|
The arguments of the executable. The argument=
s may be one or many |
|
|
The directory in which the command should be =
executed. If |
|
|
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. |
|
|
The name of a file, created by the executable=
, that should be considered as its output. If no |
|
a |
A reference to a |
|
a |
A reference to a |
|
|
A boolean indicating that when |
The supported headers are defined in org.apache.camel.component.ex=
ec.ExecBinding
.
Name |
Type |
Message |
Description |
---|---|---|---|
|
|
|
The name of the system command that will be e=
xecuted. Overrides |
|
|
|
Command-line arguments to pass to the execute=
d process. The arguments are used literally - no quoting is applied. Overri=
des any existing |
|
|
|
Camel 2.5: The arguments of =
the executable as a Single string where each argument is whitespace separat=
ed (see |
|
|
|
The name of a file, created by the executable=
, that should be considered as its output. Overrides any existing |
|
|
|
The timeout, in milliseconds, after which the=
executable should be terminated. Overrides any existing |
|
|
|
The directory in which the command should be =
executed. Overrides any existing |
|
|
|
The value of this header is the exit valu= e of the executable. Non-zero exit values typically indicate abnormal = termination. Note that the exit value is OS-dependent. |
|
|
|
The value of this header points to the standa=
rd error stream (stderr) of the executable. If no stderr is written, the va=
lue is |
|
|
|
Indicates that when |
If the Exec
component receives an in
message b=
ody that is convertible to java.io.InputStream
, it is used to =
feed input to the executable via its stdin. After execution, the message body is the result of the execution,- that is, an o=
rg.apache.camel.components.exec.ExecResult
instance containing the s=
tdout, stderr, exit value, and out file. This component supports the follow=
ing ExecResult
type converters for c=
onvenience:
From |
To |
---|---|
|
|
|
|
|
|
|
|
If an out file is specified (in the endpoint via outFile
or=
the message headers via ExecBinding.EXEC_COMMAND_OUT_FILE
), c=
onverters 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 ty=
pe. For more details, please refer to the usage exa=
mples below.
The example below executes wc
(word count, Linux) to count =
the words in file /usr/share/dict/words
. The word count (outpu=
t) is written to the standard output stream of wc
.
from("direct:= exec") .to("exec:wc?args=3D--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 ExecResul= t to String // In this case, the stdout is considered as output String wordCountOutput =3D exchange.getIn().getBody(String.class); // do something with the word count } });
java
The example below executes java
with 2 arguments: -se=
rver
and -version
, provided that java
is i=
n the system path.
from("direct:= exec") .to("exec:java?args=3D-server -version")
The example below executes java
in c:\temp
wit=
h 3 arguments: -server
, -version
and the sytem pr=
operty user.name
.
from("direct:= exec") .to("exec:c:/program files/jdk/bin/java?args=3D-server -version -Duser.name= =3DCamel&workingDir=3Dc:/temp")
The following example executes Apache Ant (Windows only) with the b=
uild file CamelExecBuildFile.xml
, provided that ant.bat<=
/code> is in the system path, and that
CamelExecBuildFile.xml
=
is in the current directory.
from("direct:= exec") .to("exec:ant.bat?args=3D-f CamelExecBuildFile.xml")
In the next example, the ant.bat
command redirects its outp=
ut to CamelExecOutFile.txt
with -l
. The file outFile=3D=
CamelExecOutFile.txt
. The example assumes that ant.bat
=
is in the system path, and that CamelExecBuildFile.xml
is in t=
he current directory.
from("direct:= exec") .to("exec:ant.bat?args=3D-f CamelExecBuildFile.xml -l CamelExecOutFile.txt&= amp;outFile=3DCamelExecOutFile.txt") .process(new Processor() { public void process(Exchange exchange) throws Exception { InputStream outFile =3D exchange.getIn().getBody(InputStream.class)= ; assertIsInstanceOf(InputStream.class, outFile); // do something with the out file here } });
echo
(Windows=
)Commands such as echo
and dir
can be executed =
only with the command interpreter of the operating system. This example sho=
ws how to execute such a command - echo
- in Windows.
from("direct:= exec").to("exec:cmd?args=3D/C echo echoString")