2

我有一个用骆驼 2.10.6 编写的应用程序,它使用 EIP aggregator。它接收 2 个文件作为输入:一个以 . 结尾,-information另一个以-report. 我必须(分别)处理这两个文件,并且在处理后我必须(即aggregate)两者结合在一起。

为了实现这一点,我正在使用 EIP aggregator。我已经使用 spring DSL 定义了路由。我没有相关表达式。也就是说,我的表达式是常量true

        <route>
        <from uri="activemq:assemblingAll" />
        <log message="\n---------->>> @ASSEMBLEALL" />

        <aggregate strategyRef="myAggregator" completionSize="2">
            <correlationExpression>
                <constant>true</constant>
            </correlationExpression>

            <to uri="activemq:toBeValidated" />
        </aggregate>
    </route>

我想关联属于一起的文件,即具有相同前缀的文件(前缀是(resp. )之前 的字符串。我相信这也可以通过在标题中设置一些 ID 来实现。-information.report

我的问题是我不知道该怎么做。到目前为止,我所有的尝试都产生了org.apache.camel.CamelExchangeException: Invalid correlation key.

有什么提示吗?

提前致谢。

我的路线(简化)如下所示:

<camelContext xmlns="http://camel.apache.org/schema/spring">

    <!-- switchOnFilename" -->
    <route>
        <from uri="file:/inputdir" />

        <choice>
            <when>
                <simple>${header.CamelFileName} regex '.*-information$'</simple>
                <log message="\n---------->>> -information" />
                <to uri="activemq:information" />
            </when>

            <otherwise>
                <!-- The default case -->
                <log message="\n---------->>> DEFAULT CASE (*-report)" />
                <to uri="activemq:report" />
            </otherwise>
        </choice>
    </route>

    <route>
        <from uri="activemq:information" />
        <bean ref="myExtractInformation" />
        <to uri="xslt:ExtractInformation.xsl" />                    
        <to uri="activemq:assemblingAll" />
    </route>

    <route>
        <from uri="activemq:report" />
        <bean ref="myExtractReports" />         
        <to uri="xslt:extractReports.xsl" />
        <to uri="activemq:assemblingAll" />
    </route>

    <route>
        <from uri="activemq:assemblingAll" />
        <log message="\n---------->>> @ASSEMBLEALL" />
        <aggregate strategyRef="myAggregator" completionSize="2">
            <correlationExpression>
                <constant>true</constant>
            </correlationExpression>

            <to uri="activemq:toBeValidated" />
        </aggregate>
    </route>

    <route>
        <from uri="activemq:toBeValidated" />

        <doTry> 
            <to uri="validator:validator.xsd" />
            <to uri="activemq:insertUpdateDB" />
            <doCatch>
                <exception>org.apache.camel.ValidationException</exception>
                <log message="INVALID " />

                <to uri="mock:invalid" />
            </doCatch>
            <doFinally>
                <log message="Finalizer...." />
                <to uri="mock:finally" />
                <to uri="mock:invalid" />
            </doFinally>
        </doTry>
    </route>

    <route>
        <from uri="activemq:insertUpdateDB" />
        <setHeader headerName="CamelHttpMethod">
            <constant>PUT</constant>
        </setHeader>
        <setHeader headerName="Content-Type" inheritErrorHandler="true" id="setHeader3">
            <constant>application/xml</constant>
        </setHeader>
        <setHeader headerName="Content-Encoding">
            <constant>UTF-8</constant>
        </setHeader>
        <to uri="http4://localhost:8181/rest/insertUpdateDB" />
    </route>
4

1 回答 1

1

也许使用 bean 引用作为相关表达式。

<correlationExpression>
    <method ref="dataCorrelationExpression" method="getPrefixString" />
</correlationExpression>

然后写个bean来做,像这样

public class DataCorrelationExpression {
    public String getPrefixString(@Header("CamelFileName") String filename) throws Exception {
        String[] parts = filename.split("\\.");
        if (parts.length != 2) {
            throw new Exception("Oh no!");
        }
        return parts[0];
    }
}

确保 bean 在 Spring 上下文中实例化。

于 2014-08-25T04:14:28.637 回答