簡體   English   中英

如何包裝長行的 Apache Camel Java DSL 代碼

[英]How to wrap long lines of Apache Camel Java DSL code

在我的項目中,我們通過 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();

現在困擾我的是line length 閱讀和維護起來很不舒服,SonarCube 等不同的代碼分析工具對此提出了警告。 我想問一下您將如何包裝這一行以及您建議使用什么選項將此代碼放入 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();

這樣做的缺點是當您使用 Apache Camel Plugin for IntelliJ 時,它允許您通過單擊 with Ctrl 來快速進入方法實現 但它僅在包含方法和輸入參數的字符串參數是單行字符串時才有效。 因此,在上面的示例中,您將失去快速轉到指定方法的能力,但獲得了可讀性。 有沒有辦法以某種方式將兩者結合起來?

為什么不使用 Camel 參數綁定呢?

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

或者,您也可以定義第二種方法來“解包”駱駝原始消息:

@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")
        

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM