Luca Burgazzoli created CAMEL-18172:
---------------------------------------

             Summary: Eagerly resolve ref language
                 Key: CAMEL-18172
                 URL: https://issues.apache.org/jira/browse/CAMEL-18172
             Project: Camel
          Issue Type: Improvement
          Components: camel-core
            Reporter: Luca Burgazzoli
             Fix For: 3.18.0


The default RefLanguage component lazily resolve the ref expression hence it 
does not work with kamelets as the route template engine leverages a 
ThreadLocal bean repository to reify the route template, then such repo si 
cleared out and not more known by the routing engine.

A way to workaround it is to create a custom ref language, as example:

{code:java}
import org.apache.camel.Expression;
import org.apache.camel.Predicate;
import org.apache.camel.language.ref.RefLanguage;
import org.apache.camel.support.PredicateToExpressionAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EagerRefLanguage extends RefLanguage {
    private static final Logger LOGGER = 
LoggerFactory.getLogger(EagerRefLanguage.class);

    @Override
    public Expression createExpression(final String expression) {
        Expression exp = 
getCamelContext().getRegistry().lookupByNameAndType(expression, 
Expression.class);
        if (exp != null) {
            LOGGER.debug("Found and instance of {} expression in the registry 
{}", expression, exp);
            exp.init(getCamelContext());
            return exp;
        }

        Predicate pred = 
getCamelContext().getRegistry().lookupByNameAndType(expression, 
Predicate.class);
        if (pred != null) {
            LOGGER.debug("Found and instance of {} predicate in the registry 
{}", expression, pred);
            pred.init(getCamelContext());
            return PredicateToExpressionAdapter.toExpression(pred);
        }

        return super.createExpression(expression);
    }
}
{code}

And bind it to the registry so camel will use as beans from the registry ahve 
priority over discovery form service loader.

This is clearly an hack but we may want to bring such optimization to camel 
core, so i.e. if a ref expression is not dynamic, then we can load it when the 
ref is reified.




--
This message was sent by Atlassian Jira
(v8.20.7#820007)

Reply via email to