You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Weiqiang Wang <we...@uber.com> on 2018/04/20 21:24:01 UTC

Math Operation in Loop

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

Re: Math Operation in Loop

Posted by Weiqiang Wang <we...@uber.com>.
Thank you Zoran

On Sun, Apr 22, 2018, 5:30 AM Zoran Regvart <zo...@regvart.com> wrote:

> 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 <we...@uber.com> 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
>

Re: Math Operation in Loop

Posted by Zoran Regvart <zo...@regvart.com>.
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 <we...@uber.com> 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