【问题标题】:How to set a Camel property as a List or Array in Spring XML如何在 Spring XML 中将 Camel 属性设置为列表或数组
【发布时间】:2018-08-16 21:31:02
【问题描述】:

我正在开发一个简单的处理器来验证路由中是否存在强制属性。 在调用处理器之前,我需要在路由定义中添加该列表。

    <route id="test1">
        <from uri="/v1/test1" />
        <setProperty propertyName="mandatoryProperties">
            <simple resultType="java.util.List">${[A,B,C]}</simple>
        </setProperty>
        <bean ref="propertiesProcessor" />
    </route>

我的处理器是:

@Component
public class MandatoryPropertiesProcessor {

@Handler
public void process(final Exchange exchange, final @Properties Map properties, final @ExchangeProperty("mandatoryProperties") List<String> mandatoryProperties) {

    List<String> missingPropertiesList = new ArrayList<>();

    if (!CollectionUtils.isEmpty(mandatoryProperties)) {

        for (String mandatoryProperty : mandatoryProperties) {

            if (!properties.containsKey(mandatoryProperty)) {
                missingPropertiesList.add(mandatoryProperty);
            }
        }
    }

    exchange.setProperty(MISSING_PROPERTIES, missingPropertiesList);
}

}

当我尝试调用这条路线时,我得到:

Caused by: org.apache.camel.language.simple.types.SimpleParserException: Unknown function: ["A","B","C"]
    at org.apache.camel.language.simple.ast.SimpleFunctionExpression.createSimpleExpression(SimpleFunctionExpression.java:216)
    at org.apache.camel.language.simple.ast.SimpleFunctionExpression.createExpression(SimpleFunctionExpression.java:40)
    at org.apache.camel.language.simple.ast.SimpleFunctionStart.doCreateLiteralExpression(SimpleFunctionStart.java:58)
    at org.apache.camel.language.simple.ast.SimpleFunctionStart.createExpression(SimpleFunctionStart.java:48)
    at org.apache.camel.language.simple.SimpleExpressionParser.createExpressions(SimpleExpressionParser.java:163)
    at org.apache.camel.language.simple.SimpleExpressionParser.doParseExpression(SimpleExpressionParser.java:86)
    at org.apache.camel.language.simple.SimpleExpressionParser.parseExpression(SimpleExpressionParser.java:53)

有可能在 XML 中实现吗?

【问题讨论】:

  • 你为什么用 $.删除那个
  • @pvpkiran 我已经删除了 $,现在我没有任何错误,但标题不存在。
  • 试试这个&lt;simple resultType="java.util.List"&gt;A,B,C&lt;/simple&gt;
  • 那个表达式还是不行。
  • 你想要一个 ArrayList 有 3 个值:A、B 和 C。你可以使用 groovy 或一些更强大的脚本语言来初始化这样的东西

标签: xml spring apache-camel spring-dsl


【解决方案1】:

您似乎使用的是 Spring Framework,并且强制属性似乎是一个静态列表,对吧?

如果是这样,只需使用 Spring 配置创建列表或任何其他强制属性集合并将其注入您的 MandatoryPropertiesProcessor bean。这部分根本没有骆驼。

如果列表发生变化,只有配置和 bean 发生变化,而不是您的 Camel 路由。这是合理的,因为通过您的路线实现的“处理工作流程”不会改变。

【讨论】:

  • 这似乎是个好主意,但使用此解决方案我无法在另一条路线中重用处理器,因为每个路线都有不同的强制参数。
  • 为什么?您可以使用 Spring 配置同一个 bean 类的多个实例,并将它们注入到相应的路由中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-25
  • 1970-01-01
  • 2022-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多