[英]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.