Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Simple" Expression Language - arithmetic operators?

I'm using this expression in a camel route:

.when(simple("${body.updateSeq} > ${property.PrevUpdateSeq} + 1"))
    .to("direct:someError")

However it is freaking out about the + symbol.

org.apache.camel.language.simple.types.SimpleIllegalSyntaxException: Unexpected token + at location 46
${body.updateSeq} > ${property.PrevUpdateSeq} + 1
                                              *

How can I construct this expression, giving that it takes a value from the getter getUpdateSeq of the POJO on the message body and compres it to a property on the Exchange (plus 1).

like image 776
vikingsteve Avatar asked Dec 12 '22 08:12

vikingsteve


2 Answers

The Simple Languate included in Apache Camel does not support a + operation per-se. It offers however an ++ increment operator which requires the left hand side to be a function.

The easiest solution would be to refactor the operation to a bean

public class NextValueService
{
    @Handler
    public Integer nextValue(Integer value)
    {
        return new Integer(value.intValue() + 1);
    }
}

and use it in your route as follows:

.when(simple("${body.updateSeq} > ${bean:nextValueService?method=nextValue(property.PrevUpdateSeq)"}))
.to("direct:someError")

Switching simple language with f.e. JavaScript or Groovy should also help on dealing with the problem.

like image 196
Roman Vottner Avatar answered Dec 24 '22 10:12

Roman Vottner


You can also write a predicate fairly easy to accomplish this.

public class MyPredicate implements Predicate { 
  public boolean matches(final Exchange exchange) {
    return (exchange.getIn().getBody(Map.class).get("updateSeq") > (exchange.getProperty("PrevUpdateSeq") + 1))
  }
}

This assumes the body is a map, if its a bean its easy enough to change the getBody(). Then use it like so:

.when(new MyPredicate()).to("direct:someError")

When you are doing custom code like this, you should try to keep it separate from routing. Simple expression language is very useful but can get godawful to read if overused.

Edit: you can even do it inline if you want:

.when(new Predicate() { 
  public boolean matches(final Exchange exchange) {
    return (exchange.getIn().getBody(Map.class).get("updateSeq") >
             (exchange.getProperty("PrevUpdateSeq") + 1))
  }).to("direct:someError")
like image 37
Robert Simmons Jr. Avatar answered Dec 24 '22 10:12

Robert Simmons Jr.