简体   繁体   English

如何包装长行的 Apache Camel Java DSL 代码

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

In my project we are using Apache Camel via Java DSL在我的项目中,我们通过 Java DSL 使用 Apache Camel

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 .现在困扰我的是line length It is uncomfortable to read and maintain, different code analyzing tools like SonarCube are raising warnings about that.阅读和维护起来很不舒服,SonarCube 等不同的代码分析工具对此提出了警告。 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 ?我想问一下您将如何包装这一行以及您建议使用什么选项将此代码放入 120 个符号宽度中

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 .这样做的缺点是当您使用 Apache Camel Plugin for IntelliJ 时,它允许您通过单击 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?有没有办法以某种方式将两者结合起来?

Why not use Camel parameter binding instead ?为什么不使用 Camel 参数绑定呢?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM