Exec
Since Camel 2.3
Only producer is supported
The Exec component can be used to execute system commands.
Dependencies
Maven users need to add the following dependency to their pom.xml
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-exec</artifactId>
<version>${camel-version}</version>
</dependency>
where ${camel-version
} must be replaced by the actual version of Camel.
URI format
exec://executable[?options]
where executable
is the name, or file path, of the system command that
will be executed. If executable name is used (e.g. exec:java
), the
executable must in the system path.
URI options
The Exec component supports 2 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
lazyStartProducer (producer) |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
boolean |
autowiredEnabled (advanced) |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. |
true |
boolean |
The Exec endpoint is configured using URI syntax:
exec:executable
with the following path and query parameters:
Path Parameters (1 parameters):
Name | Description | Default | Type |
---|---|---|---|
executable |
Required Sets the executable to be executed. The executable must not be empty or null. |
String |
Query Parameters (9 parameters):
Name | Description | Default | Type |
---|---|---|---|
args (producer) |
The arguments may be one or many whitespace-separated tokens. |
String |
|
binding (producer) |
A reference to a org.apache.commons.exec.ExecBinding in the Registry. |
ExecBinding |
|
commandExecutor (producer) |
A reference to a org.apache.commons.exec.ExecCommandExecutor in the Registry that customizes the command execution. The default command executor utilizes the commons-exec library, which adds a shutdown hook for every executed command. |
ExecCommandExecutor |
|
commandLogLevel (producer) |
Logging level to be used for commands during execution. The default value is DEBUG. Possible values are TRACE, DEBUG, INFO, WARN, ERROR or OFF. (Values of ExecCommandLogLevelType enum). There are 6 enums and the value can be one of: TRACE, DEBUG, INFO, WARN, ERROR, OFF |
DEBUG |
LoggingLevel |
lazyStartProducer (producer) |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
boolean |
outFile (producer) |
The name of a file, created by the executable, that should be considered as its output. If no outFile is set, the standard output (stdout) of the executable will be used instead. |
String |
|
timeout (producer) |
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. |
long |
|
useStderrOnEmptyStdout (producer) |
A boolean indicating that when stdout is empty, this component will populate the Camel Message Body with stderr. This behavior is disabled (false) by default. |
false |
boolean |
workingDir (producer) |
The directory in which the command should be executed. If null, the working directory of the current process will be used. |
String |
Message headers
The supported headers are defined in
org.apache.camel.component.exec.ExecBinding
.
Name | Type | Message | Description |
---|---|---|---|
|
|
|
The name of the system command that will be executed. Overrides
|
|
|
|
Command-line arguments to pass to the executed process. The arguments
are used literally - no quoting is applied. Overrides any existing
|
|
|
|
The arguments of the executable as a Single string where
each argument is whitespace separated (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 value 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 standard error stream (stderr) of
the executable. If no stderr is written, the value is |
|
|
|
Indicates that when |
|
|
|
Logging level to be used for commands during execution. The default value is DEBUG. Possible values are TRACE, DEBUG, INFO, WARN, ERROR or OFF (Values of LoggingLevel enum) |
Message body
If the Exec
component receives an in
message body 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
org.apache.camel.components.exec.ExecResult
instance containing the
stdout, stderr, exit value, and out file. This component supports the
following ExecResult
type
converters for convenience:
From | To |
---|---|
|
|
|
|
|
|
|
|
If an out file is specified (in the endpoint via outFile
or the
message headers via ExecBinding.EXEC_COMMAND_OUT_FILE
), 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 usage examples below.
Usage examples
Executing word count (Linux)
The example below executes wc
(word count, Linux) to count the words
in file /usr/share/dict/words
. The word count (output) is written to
the standard output stream of 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
}
});
Executing java
The example below executes java
with 2 arguments: -server
and
-version
, provided that java
is in the system path.
from("direct:exec")
.to("exec:java?args=-server -version")
The example below executes java
in c:\temp
with 3 arguments:
-server
, -version
and the sytem property user.name
.
from("direct:exec")
.to("exec:c:/program files/jdk/bin/java?args=-server -version -Duser.name=Camel&workingDir=c:/temp")
Executing Ant scripts
The following example executes Apache Ant
(Windows only) with the build file CamelExecBuildFile.xml
, provided
that ant.bat
is in the system path, and that CamelExecBuildFile.xml
is in the current directory.
from("direct:exec")
.to("exec:ant.bat?args=-f CamelExecBuildFile.xml")
In the next example, the ant.bat
command redirects its output to
CamelExecOutFile.txt
with -l
. The file CamelExecOutFile.txt
is
used as the out file with outFile=CamelExecOutFile.txt
. The example
assumes that ant.bat
is in the system path, and that
CamelExecBuildFile.xml
is in the current directory.
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
}
});
Spring Boot Auto-Configuration
When using exec with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-exec-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
The component supports 3 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
camel.component.exec.autowired-enabled |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. |
true |
Boolean |
camel.component.exec.enabled |
Whether to enable auto configuration of the exec component. This is enabled by default. |
Boolean |
|
camel.component.exec.lazy-start-producer |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
Boolean |