Uploaded image for project: 'Camel'
  1. Camel
  2. CAMEL-18172

camel-core - Eagerly resolve ref language when static to optimize

Agile BoardAttach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • None
    • 3.18.0
    • camel-core
    • None
    • Unknown

    Description

      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:

      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);
          }
      }
      

      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.

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            davsclaus Claus Ibsen
            lb Luca Burgazzoli
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment