Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Camel exec with args

Tags:

apache-camel

When using the exec component is it possible to specify args inline rather than having to set them in the 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 the Simple Expression Language to simplify it like so:

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 can 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.

like image 251
Damo Avatar asked Nov 05 '10 03:11

Damo


2 Answers

The answer is in the EIP patterns. You need to use the dynamic recipient list EIP pattern when you compute an endpoint destination at runtime.

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

The recipient list accepts an expression which means you can use the Simple language to construct the parameters at runtime

like image 145
Claus Ibsen Avatar answered Oct 18 '22 03:10

Claus Ibsen


This took me far longer to understand than it should of done, so for others that stumble here 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'm creating a dynamic request to run some php ( specifically a yii 2 command ) which is populated via variables in the hashmap/$body which was generated from a sql query earlier on in the route.

like image 22
Alan Hollis Avatar answered Oct 18 '22 03:10

Alan Hollis