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 be in the system path.
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The Exec component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
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 | |
The timeout, in milliseconds, for the command to finish. When the timeout is reached, the process is destroyed (Process.destroy()) and the message continues with the exit value of the destroyed process: no exception is raised unless exitValues is set and does not contain that value. The value must be positive. When not set, there is no timeout. | long | ||
The working directory of the command. A relative path is resolved against the working directory of the JVM. When not set, the command runs in the working directory of the JVM. The execution fails with an ExecException when the directory does not exist. | String | ||
Whether the CamelExecCommandExecutable, CamelExecCommandArgs, CamelExecCommandOutFile, CamelExecCommandWorkingDir, CamelExecCommandTimeout, CamelExecExitValues, CamelExecUseStderrOnEmptyStdout and CamelExecCommandLogLevel headers override the matching endpoint options. When false (the default since Camel 4.20), these headers are ignored and left on the message, and a WARN is logged once per endpoint. When true, these headers are used and removed from the message. Enable only when the route sets these headers itself, not when they can come from an untrusted sender. | false | boolean | |
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 | |
A custom org.apache.camel.component.exec.ExecBinding that creates the command from the message and the endpoint options, and writes the ExecResult to the message. The default is DefaultExecBinding. | ExecBinding | ||
A custom org.apache.camel.component.exec.ExecCommandExecutor that runs the command. When not set, a new default executor is created for each message: it runs the command with the Apache Commons Exec DefaultExecutor, reads the standard output and the standard error into memory, and registers a JVM shutdown hook while the command runs, so that the process is destroyed when the JVM exits. | ExecCommandExecutor |
Endpoint Options
The Exec endpoint is configured using URI syntax:
exec:executable
With the following path and query parameters:
Path Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Required The executable to run: a command name, which the operating system looks up in the system path, or the file path of the executable. The command does not run in a shell, so a shell built-in command such as echo needs the shell as the executable, for example cmd with args=/C echo hello on Windows. | String |
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
The arguments to pass to the executable, separated by spaces. An argument enclosed in single or double quotes is kept as one argument, even if it contains spaces, and the quotes are removed. The arguments are passed to the process as they are: there is no shell, so wildcards, variables and redirections are not expanded. | String | ||
The logging level of the log lines that show the command before it runs and its exit value after it ran. A non-zero exit value is also logged at ERROR level, whatever this level is. Enum values:
| DEBUG | LoggingLevel | |
Comma-separated list of the exit values that count as success, for example 0,1. When the process exits with another value, the exchange fails with an ExecException that holds the standard output, the standard error and the exit value. When not set, the exit value is not checked: the message continues with any exit value, which is in the CamelExecExitValue header. | String | ||
The path of a file, written by the command, whose content is the output of the command: the ExecResult in the message body then converts to the content of this file instead of the standard output (stdout). A relative path is resolved against the working directory of the JVM, not against workingDir. The file is read when the body is converted, and the conversion fails if the file does not exist. | String | ||
The timeout, in milliseconds, for the command to finish. When the timeout is reached, the process is destroyed (Process.destroy()) and the message continues with the exit value of the destroyed process: no exception is raised unless exitValues is set and does not contain that value. The value must be positive. When not set, there is no timeout. | long | ||
Whether the ExecResult in the message body converts to the standard error (stderr) of the command when the command wrote nothing to the standard output (stdout). Has no effect when outFile is set. | false | boolean | |
The working directory of the command. A relative path is resolved against the working directory of the JVM. When not set, the command runs in the working directory of the JVM. The execution fails with an ExecException when the directory does not exist. | String | ||
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 | |
Whether the CamelExecCommandExecutable, CamelExecCommandArgs, CamelExecCommandOutFile, CamelExecCommandWorkingDir, CamelExecCommandTimeout, CamelExecExitValues, CamelExecUseStderrOnEmptyStdout and CamelExecCommandLogLevel headers override the matching endpoint options. When false (the default since Camel 4.20), these headers are ignored and left on the message, and a WARN is logged once per endpoint. When true, these headers are used and removed from the message. Enable only when the route sets these headers itself, not when they can come from an untrusted sender. | false | boolean | |
A custom org.apache.camel.component.exec.ExecBinding that creates the command from the message and the endpoint options, and writes the ExecResult to the message. The default is DefaultExecBinding. | ExecBinding | ||
A custom org.apache.camel.component.exec.ExecCommandExecutor that runs the command. When not set, a new default executor is created for each message: it runs the command with the Apache Commons Exec DefaultExecutor, reads the standard output and the standard error into memory, and registers a JVM shutdown hook while the command runs, so that the process is destroyed when the JVM exits. | ExecCommandExecutor |
Message Headers
The Exec component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelExecCommandExecutable (in) Constant: | The executable to run, instead of the executable in the endpoint URI. Requires allowControlHeaders=true on the endpoint or component (the default is false since Camel 4.20); otherwise the header is ignored. | String | |
| Constant: | The arguments to pass to the executable, instead of the args option. A java.util.List of String is used as is, one element per argument. Any other value is converted to a String and split at spaces like the args option. Requires allowControlHeaders=true on the endpoint or component (the default is false since Camel 4.20); otherwise the header is ignored. | List or String | |
| Constant: | The path of a file, written by the command, whose content is the output of the command, instead of the outFile option. Requires allowControlHeaders=true on the endpoint or component (the default is false since Camel 4.20); otherwise the header is ignored. | String | |
CamelExecCommandWorkingDir (in) Constant: | The working directory of the command, instead of the workingDir option. Requires allowControlHeaders=true on the endpoint or component (the default is false since Camel 4.20); otherwise the header is ignored. | String | |
| Constant: | The timeout, in milliseconds, for the command to finish, instead of the timeout option. Requires allowControlHeaders=true on the endpoint or component (the default is false since Camel 4.20); otherwise the header is ignored. | long | |
| Constant: | Comma-separated list of the exit values that count as success, instead of the exitValues option. Requires allowControlHeaders=true on the endpoint or component (the default is false since Camel 4.20); otherwise the header is ignored. | String | |
| Constant: | The standard error (stderr) of the command, as an InputStream, or null when the command wrote nothing to it. | InputStream | |
| Constant: | 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. | int | |
CamelExecUseStderrOnEmptyStdout (in) Constant: | Whether the ExecResult in the message body converts to the standard error (stderr) when the command wrote nothing to the standard output (stdout), instead of the useStderrOnEmptyStdout option. Requires allowControlHeaders=true on the endpoint or component (the default is false since Camel 4.20); otherwise the header is ignored. | boolean | |
| Constant: | The logging level of the log lines that show the command and its exit value (TRACE, DEBUG, INFO, WARN, ERROR or OFF), instead of the commandLogLevel option. Requires allowControlHeaders=true on the endpoint or component (the default is false since Camel 4.20); otherwise the header is ignored. | String |
Message Body
The message body at each side of the Exec component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Producer input |
| The standard input of the command, when the body converts to a stream: text, bytes, a stream or a file. Another body, or no body, runs the command without input. |
Producer output |
| The ExecResult, with the exit value and the standard error in headers. It converts to the text, bytes or stream of the standard output, of the outFile when set, or of the standard error with useStderrOnEmptyStdout. Not known when |
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
Usage
Control headers
Since Camel 4.20, the exec in-headers that override URI options (CamelExecCommand*, CamelExecExitValues, and CamelExecUseStderrOnEmptyStdout) are disabled by default. This prevents untrusted message sources from redirecting command execution. To use dynamic executable, arguments, working directory, or other command settings from headers, enable the opt-in flag on the endpoint or component:
-
Java
-
YAML
from("direct:run")
.setHeader("CamelExecCommandArgs", constant("ARGS-WORK"))
.to("exec:echo?allowControlHeaders=true"); - route:
from:
uri: direct:run
steps:
- setHeader:
name: CamelExecCommandArgs
constant: "ARGS-WORK"
- to:
uri: exec:echo
parameters:
allowControlHeaders: true When allowControlHeaders is false (the default), those in-headers on the exchange are ignored (they are not removed), and a WARN is logged once per exec endpoint. URI parameters such as args continue to work as before.
Message body
If the component receives an in message body that is convertible to java.io.InputStream, it is used to feed input to the executable via its standard input (stdin). After execution, the message body is the result of the execution. That is, an org.apache.camel.component.exec.ExecResult instance containing the stdout, stderr, exit value, and the 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), the 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. When the process writes nothing to its output, the String converter gives an empty string, and the byte [] converter an empty byte array. For more details, please refer to the usage examples below.
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:
-
Java
-
XML
-
YAML
// The body is an ExecResult instance, which can be auto-converted to String (stdout output)
from("direct:exec")
.to("exec:wc?args=--words /usr/share/dict/words")
.to("log:result"); <!-- The body is an ExecResult instance, which can be auto-converted to String (stdout output) -->
<route>
<from uri="direct:exec"/>
<to uri="exec:wc?args=--words /usr/share/dict/words"/>
<to uri="log:result"/>
</route> # The body is an ExecResult instance, which can be auto-converted to String (stdout output)
- route:
from:
uri: direct:exec
steps:
- to:
uri: exec:wc
parameters:
args: "--words /usr/share/dict/words"
- to:
uri: log:result Executing java
The example below executes java with two arguments: -server and -version, if java is in the system path.
-
Java
-
XML
-
YAML
from("direct:exec")
.to("exec:java?args=-server -version"); <route>
<from uri="direct:exec"/>
<to uri="exec:java?args=-server -version"/>
</route> - route:
from:
uri: direct:exec
steps:
- to:
uri: exec:java
parameters:
args: "-server -version" The example below executes java in c:\temp with three arguments: -server, -version and the system property user.name.
-
Java
-
XML
-
YAML
from("direct:exec")
.to("exec:c:/program files/jdk/bin/java?args=-server -version -Duser.name=Camel&workingDir=c:/temp"); <route>
<from uri="direct:exec"/>
<to uri="exec:c:/program files/jdk/bin/java?args=-server -version -Duser.name=Camel&workingDir=c:/temp"/>
</route> - route:
from:
uri: direct:exec
steps:
- to:
uri: "exec:c:/program files/jdk/bin/java"
parameters:
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.
-
Java
-
XML
-
YAML
from("direct:exec")
.to("exec:ant.bat?args=-f CamelExecBuildFile.xml"); <route>
<from uri="direct:exec"/>
<to uri="exec:ant.bat?args=-f CamelExecBuildFile.xml"/>
</route> - route:
from:
uri: direct:exec
steps:
- to:
uri: exec:ant.bat
parameters:
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.
-
Java
-
XML
-
YAML
// When outFile is specified, the ExecResult in the body converts to the content of that file
from("direct:exec")
.to("exec:ant.bat?args=-f CamelExecBuildFile.xml -l CamelExecOutFile.txt&outFile=CamelExecOutFile.txt")
.to("log:result"); <!-- When outFile is specified, the ExecResult in the body converts to the content of that file -->
<route>
<from uri="direct:exec"/>
<to uri="exec:ant.bat?args=-f CamelExecBuildFile.xml -l CamelExecOutFile.txt&outFile=CamelExecOutFile.txt"/>
<to uri="log:result"/>
</route> # When outFile is specified, the ExecResult in the body converts to the content of that file
- route:
from:
uri: direct:exec
steps:
- to:
uri: exec:ant.bat
parameters:
args: "-f CamelExecBuildFile.xml -l CamelExecOutFile.txt"
outFile: CamelExecOutFile.txt
- to:
uri: log:result Executing echo (Windows)
Commands such as echo and dir can be executed only with the command interpreter of the operating system. This example shows how to execute such a command - echo - in Windows.
-
Java
-
XML
-
YAML
from("direct:exec")
.to("exec:cmd?args=/C echo echoString"); <route>
<from uri="direct:exec"/>
<to uri="exec:cmd?args=/C echo echoString"/>
</route> - route:
from:
uri: direct:exec
steps:
- to:
uri: exec:cmd
parameters:
args: "/C echo echoString"