Search code examples
apache-camelexpressiondocumentationthrottling

How to create a complex custom expression for a throttle Correlation Expression in Apache Camel


I'm trying to do the following:

Based on a nullable field of an object within an exchange property that must be accessed via a name stored within a Java constant, I need to either throttle or not.

However, I have a tough time understanding how to build more complex expressions with Apache Camel and their documentation is of no great help in this regard.

My preferred solution would be to not use all these expression languages like "simple" that Camel provides, but rather do it via plain code.

Let's say this is the object carrying the value that needs to be evaluated:

public class MyObject {
   private String group;

   public boolean hasGroup(){
      return group != null;
   }
}

What I've tried so far, however, is this:

1. Via ExpressionBuilder

.throttle(1)
.correlationExpression(ExpressionBuilder
    .exchangePropertyExpression(MY_OBJECT_PROPERTY_NAME_CONSTANT))

But then what? What does this expression say and how would I build up my conditions from there?

Found no docs for the promising ExpressionBuilder at all, no clue how to work with it

2. Via custom Expression implementation

.throttle(1)
.correlationExpression(new Expression() {
    @Override 
    public<T> T evaluate(Exchange exchange, Class<T> type) {
        var myObject = exchange.getProperty(MY_OBJECT_PROPERTY_NAME_CONSTANT, MyObject.class);
  

        return null;
    }
})

But what is the return type here and how would you implement it?

The documentation provides absolute no value and no examples

enter image description here

What is a reliable way to cover my conditions within one expression?


Solution

  • Found out how to work with the Expression Interface

    While I was confused about the return type of the evaluate method

    <T> T evaluate(Exchange exchange, Class<T> type);
    

    it is as simple as that, just returning Object

    .correlationExpression(new Expression() {
        @Override
        @SuppressWarnings("unchecked")
        public Object evaluate(Exchange exchange, Class asType) {
             return exchange.getProperty(MY_PROPERTY, MyObject.class)
                     .getGroupId();
        }
    });