如何 Package 长行的Apache Camel Java DSL代码

ih99xse1  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(189)

在我的项目中,我们通过Java DSL使用Apache Camel
以下是典型路线的外观:

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

现在让我感到困扰的是行的长度。阅读和维护它是不舒服的,不同的代码分析工具,如SonarCube,都提出了关于这一点的警告。我想问你会如何 Package 这一行,你会建议什么选项来适应这120个符号的宽度
例如,您可以执行以下操作:

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

这样做的缺点是当您使用用于IntelliJ的ApacheCamelPlugin时,它允许你***快速进入方法的实现,通过用Ctrl***点击。但是它只在包含方法和输入参数的字符串参数是单行字符串时有效。所以在上面的例子中,你将失去快速进入指定方法的能力,但是获得了可读性。有没有办法把两者结合起来呢?

djmepvbi

djmepvbi1#

为什么不改用Camel parameter binding

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

或者,您也可以定义第二个方法来“解包”Camel原始消息:

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

这样,客户端代码变得简单得多:

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

相关问题