Darius X.
Darius X.

Reputation: 2937

Extending Camel DSL

I have a route with a custom aggregator, it looks like this: enter image description here

I would like to wrap the details so that developers can just add a single line to their routes to get the functionality. Can I wrap those lines so that I have a class that extends ProcessorDefinition, and then add that ProcessorDefinition to routes that need it, so that it looks like an extension of the DSL? If yes, is the addOutput() method the way to do this?

Something like this:

from("file:" + FILE_PATH + "?noop=true")
        .log("Detected file")
        .split().tokenize("\n")
        .streaming()
        .unmarshal(bindy)

        .addProcessorDefinition(new MyCustomAggregation())

        .to("direct:handleAggregatedRecords");

Where

MyCustomerAggegation extends ProcessorDefinition

Upvotes: 1

Views: 259

Answers (2)

Marian Klühspies
Marian Klühspies

Reputation: 17627

I have a similar use-case and I don't think Camel has anything to offer out of the box to extend routes at a certain point with route fragments. However, you can break apart one route into multiple fragments

public abstract class ExtensibleRouterBuilder extends RouteBuilder {

  @Override
  public void configure() {
      ProcessorDefinition<?> routeFragmentToExtend= from("file:" + FILE_PATH + "?noop=true")
        .log("Detected file")
        .split().tokenize("\n")
        .streaming()
        .unmarshal(bindy);

      configure(routeFragmentToExtend);

      routeFragmentToExtend.to("direct:handleAggregatedRecords");
  }

   //"Users" of your API can implement and extend your route
   public abstract void configure(ProcessorDefinition<?> from);

}

But this can get messy, especially if you have several choice() calls, so I wish Camel enhances it's API one day.

Upvotes: 1

Ed Gomoliako
Ed Gomoliako

Reputation: 1040

To get a similar outcome, I use Lombok @ExtensionMethod in the following way:

@ExtensionMethod(MyRouteBuilder.Extensions.class)
public class MyRouteBuilder extends RouteBuilder {

  @Override
  public void configure() {
    from("direct:in")
      .debugLog("Incoming message ${body}");
  }

  @UtilityClass
  public static class Extensions {
    public static ProcessorDefinition<?> debugLog(
        @NonNull ProcessorDefinition<?> route, String logMessage) {

      return route
          .log(LoggingLevel.DEBUG, "debug-logger", logMessage);
    }
  }
}

Upvotes: 0

Related Questions