git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Math Operation in Loop


Hi Weiqiang,
the simple language can not do what you want, you can look at the
supported operations in the docs[1].

A best practice would be to create a standard Java method to perform
that and use the `method` DSL and set a header value, you can use also
dynamic expressions in Groovy or some other supported expression
language (look at that documentation).

For example:

public class Utilities {
    public static int myExpression(final Exchange exchange) {
        return exchange.getProperty("CamelLoopIndex", Integer.class) * 10 + 1;
    }
}

And in the route DSL:

    .loop(10)
      .setHeader("groovyExpressionValue",
GroovyLanguage.groovy("exchange.properties.CamelLoopIndex * 10 + 1"))
      .setHeader("methodExpressionValue", method(Utilities.class,
"myExpression"))
      .log("${header.groovyExpressionValue}")
      .log("${header.methodExpressionValue}")

zoran

[1] https://github.com/apache/camel/blob/master/camel-core/src/main/docs/simple-language.adoc

On Fri, Apr 20, 2018 at 11:24 PM, Weiqiang Wang <weiqiang@xxxxxxxx> wrote:
> Hi,
>
> I want to have some math operation in route loop based on the index:
>
> .loop(5)
> .log(String.valueOf(Integer.parseInt("${property.CamelLoopIndex}")*10+1));
>
> But I got error:
> error=For input string: "${property.CamelLoopIndex}"}
>
> If I change it to a constant string, it works. (e.g. "1").
> I also tried use simple without any luck.
> setheader and setbody would cause the same issue when retrieving value
> ${hearder.<name>}
>
> I want to create a variable outside dsl, but not sure how to assign
> variable value in the dsl.
>
> How to do a math calculation based on the loop index.
>
> --
> Weiqiang



-- 
Zoran Regvart