如何在Spring XML中将Camel属性设置为List或Array

时间:2018-03-08 13:45:27

标签: xml spring apache-camel spring-dsl

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

    <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实现吗?

1 个答案:

答案 0 :(得分:0)

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

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

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