Apache Camel exec with arguments

When using the exec component, you can specify args inline strings instead of setting them to ExecBinding.EXEC_COMMAND_ARGS ?

For example, I have this route:

 from("seda:getPolicyListStart") .process(new Processor() { public void process(Exchange e) { ClientRequestBean requestBean = (ClientRequestBean)e.getIn().getBody(); List<String> args = new ArrayList<String>(); args.add(requestBean.getClient()); args.add(requestBean.getSort()); e.getOut().setHeader(ExecBinding.EXEC_COMMAND_ARGS, args); } }) .to("exec:some_command?useStderrOnEmptyStdout=true") .convertBodyTo(String.class) .log("Executed OS cmd and received: ${body}") 

However, I would have thought that I could use a simple expression language to simplify it like this:

 from("seda:getPolicyListStart") .to("exec:some_command?useStderrOnEmptyStdout=true&args=${body.client} ${body.sort}") .convertBodyTo(String.class) .log("Executed OS cmd and received: ${body}") 

Similar to how you use File Language (a subset of Simple) when you use the File component.

Is it possible? If not, can the first example be simplified?

UPDATE [solution]:

  from(requestNode) .routeId(routeId) .recipientList(simple("exec:"+osCmd+"?useStderrOnEmptyStdout=true&args=${body.client}")) .convertBodyTo(String.class) .log("Executed OS cmd and received: ${body}") .to(responseNode); 

Thanks.

+4
source share
2 answers

The answer is in the EIP templates. You need to use the dynamic recipient EIP pattern when calculating the final destination at runtime.

http://camel.apache.org/recipient-list.html

The recipient list accepts an expression that means you can use a simple language to create parameters at runtime

+6
source

It took me a lot more time to understand how this should be done, so for others who stumble and get confused.

In Spring XML, the above looks like

 <recipientList> <simple>exec:/usr/bin/php?args=yii individual-participant-report/runreport ${body[assessment_id]} ${body[scope_id]} ${body[participation_id]} ${body[participation_email]}&amp;workingDir={{reporting.folder}}</simple> </recipientList> 

In this example, I create a dynamic query to run some php (in particular, the yii 2 command), which is populated through the variables in the hashmap / $ element that was generated from the SQL query earlier on the route.

0
source

All Articles