문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top