gpt4 book ai didi

java - 带有关联表达式的 Camel 聚合器

转载 作者:太空宇宙 更新时间:2023-11-04 15:24:11 24 4
gpt4 key购买 nike

我有一个用camel 2.10.6编写的应用程序,它使用EIP聚合器。它接收 2 个文件作为输入:一个以 -information 结尾,另一个以 -report 结尾。我必须(单独)处理这两个文件,处理后我必须将它们合并(即聚合)在一起。

为了实现这一目标,我使用了 EIP聚合器。我已经使用 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>

我想要关联属于一起的文件,即具有相同前缀的文件(前缀是之前-information (分别为 .report)。我相信通过在 header 中设置一些 ID 也可以实现这一点。

我的问题是我不知道该怎么做。到目前为止,我的所有尝试都生成了org.apache.camel.CamelExchangeException:无效的相关键

有什么提示吗?

提前致谢。

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

<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>

最佳答案

也许使用 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 上下文中实例化。

关于java - 带有关联表达式的 Camel 聚合器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20050043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com