Camel Component: Exec - 6.3

Talend ESB Mediation Developer Guide

EnrichVersion
6.3
EnrichProdName
Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
task
Design and Development
EnrichPlatform
Talend ESB

The exec component can be used to execute system commands. For this component, Maven users will need to add the following dependency to their pom.xml file:

<dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-exec</artifactId>
   <version>${camel-version}</version>
</dependency>

replacing ${camel-version} with the precise version used.

This component has URI format of:

exec://executable[?options]

where executable is the name, or file path, of the system command that will be executed. If executable name is used (for example, exec:java), the executable must be in the system path.

URI options

Name

Default value

Description

args

null

The arguments of the executable- they may be one or many whitespace-separated tokens, that can be quoted with ", for example, args="arg 1" arg2 will use two arguments arg 1 and arg2. To include the quotes, enclose them in another set of quotes; for example, args=""arg 1"" arg2 will use the arguments "arg 1" and arg2.

workingDir

null

The directory in which the command should be executed. If null, the working directory of the current process will be used.

timeout

Long.MAX_VALUE

The timeout, in milliseconds, after which the executable should be terminated. If execution has not completed within this period, the component will send a termination request.

outFile

null

The name of a file, created by the executable, that should be considered as output of the executable. If no outFile is set, the standard output (stdout) of the executable will be used instead.

binding

a DefaultExecBinding instance

A reference to an org.apache.commons. exec.ExecBinding in the Registry .

commandExecutor

a DefaultCommand-Executor instance

A reference to an 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.

useStderrOnEmpty-Stdout

false

A boolean which dictates when stdin is empty, it should fallback and use stderr in the Camel Message Body. This option is default false .

Message headers

The supported headers are defined in org.apache.camel.component.exec.ExecBinding .

Name

Message

Description

ExecBinding. EXEC_COMMAND_EXECUTABLE

in

The name of the system command that will be executed. Overrides the executable in the URI.

Type: String

ExecBinding.EXEC_COMMAND_ARGS

in

The arguments of the executable. The arguments are used literally, no quoting is applied. Overrides existing args in the URI.

Type: java.util.List<String>

ExecBinding.EXEC_COMMAND_ARGS

in

The arguments of the executable as a single string where each argument is whitespace separated (see args in URI option). The arguments are used literally, no quoting is applied. Overrides existing args in the URI.

Type: String

ExecBinding. EXEC_COMMAND_OUT_FILE

in

The name of a file, created by the executable, that should be considered as output of the executable. Overrides existing outFile in the URI.

Type: String

ExecBinding. EXEC_COMMAND_TIMEOUT

in

The timeout, in milliseconds, after which the executable should be terminated. Overrides existing timeout in the URI.

Type: long

ExecBinding. EXEC_COMMAND_WORKING_DIR

in

The directory in which the command should be executed. Overrides existing workingDir in the URI.

Type: String

ExecBinding.EXEC_EXIT_VALUE

out

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.

Type: int

ExecBinding.EXEC_STDERR

out

The value of this header points to the standard error stream (stderr) of the executable. If no stderr is written, the value is null.

Type: java.io.InputStream

ExecBinding. EXEC_USE_STDERR_ON_EMPTY_STDOUT

in

Indicates when the stdin is empty, should we fallback and use stderr as the body of the Camel message. By default this option is false.

Type: boolean

Message body

If the in message body, that the Exec component receives is convertible to java.io.InputStream, it is used to feed the input of the executable via its stdin. After the execution, the message body is the result of the execution, that is org.apache.camel.components.exec.ExecResult instance containing the stdout, stderr, exit value and out file. The component supports the following ExecResult type converters for convenience:

From

To

ExecResult

java.io.InputStream

ExecResult

String

ExecResult

byte []

ExecResult

org.w3c.dom.Document

If out file is used (the endpoint is configured with outFile, or there is ExecBinding.EXEC_COMMAND_OUT_FILE 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 an example, the below executes wc (word count, Linux) to count the words in file /usr/share/dict/words . The word count (output) is written in 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
      ...
   }
});