How to wrap long lines of Apache Camel Java DSL code

140 Views Asked by At

In my project we are using Apache Camel via Java DSL

This is how a typical route looks:

    from("direct:MyPizzaRestaurant")
            .routeId("PizzaRoute")
            .log(LoggingLevel.INFO, LOG, LOG_IN_MESSAGE)
            .bean(veryImportandAndUniqueManagementService, "addTomatoesAndCheeseAndThenPutInTheOven(${in.headers.pizzaContextKey},${in.headers.httpHeaders[pizzaOrderIz]},${in.headers.httpHeaders[restaurantId]},${in.headers.httpHeaders[orderChannel]},${in.headers.customerId},${in.headers.httpHeaders[pizzaType]},${in.headers.httpHeaders[promo]})")
            .end();

Now what bothers me is the line length. It is uncomfortable to read and maintain, different code analyzing tools like SonarCube are raising warnings about that. I want to ask how would you wrap this line and what option would you recommend to fit this code into the 120 symbols width?

For example you could do this:

        from("direct:MyPizzaRestaurant")
                .routeId("PizzaRoute")
                .log(LoggingLevel.INFO, LOG, LOG_IN_MESSAGE)
                .bean(veryImportandAndUniqueManagementService,
                        "addTomatoesAndCheeseAndThenPutInTheOven(
                        "${in.headers.pizzaContextKey}," +
                        "${in.headers.httpHeaders[pizzaOrderIz]}," +
                        "${in.headers.httpHeaders[restaurantId]}," +
                        "${in.headers.httpHeaders[orderChannel]}," +
                        "${in.headers.customerId}," +
                        "${in.headers.httpHeaders[pizzaType]}," +
                        "${in.headers.httpHeaders[promo]})")
                .end();

The drawback of this is when you are using Apache Camel Plugin for IntelliJ, it allows you to quickly go inside the method implementation by Clicking on with with Ctrl. But it only works when string parameter containing method and input params is a single line string. So in the example above you will lose the ability to quickly travel to the specified method but gain readability. Is there a way to somehow combine both?

1

There are 1 best solutions below

0
TacheDeChoco On

Why not use Camel parameter binding instead ?

@Bean("veryImportandAndUniqueManagementService")
public ManagementServiceImpl {
  public Object addTomatoesAndCheeseAndThenPutInTheOven(
           @Header("pizzaContextKey") String ctxKey,
           @Header("custId") Long customerId, 
           etc...
         ) {
... 
}

Alternatively you could also define a second method to 'unwrap' Camel raw message:

@Bean("veryImportandAndUniqueManagementService")
public ManagementServiceImpl {
   public Object addTomatoesAndCheeseAndThenPutInTheOven(Exchange exchange) {
    return this.addTomatoesAndCheeseAndThenPutInTheOven(
     exchange.getMessage().getHeader("pizzaContextKey", String.class),
     exchange.getMessage().getHeader("custId", Long.class), 
     etc...
    );
    }
}

This way, the client code become much simpler:

from("direct:MyPizzaRestaurant")              
   .bean(veryImportandAndUniqueManagementService, "addTomatoesAndCheeseAndThenPutInTheOven")