我目前有以下Camel路线:
<camelContext id="my-camel-context" xmlns="http://camel.apache.org/schema/spring">
<propertyPlaceholder id="envProps" location="classpath:myapp.properties" />
<route id="my-camel-route">
<from uri="{{start.uri}}"/>
<setHeader headerName="id">
<constant>1</constant>
</setHeader>
<to uri="bean:preProcessor?method=process" />
<aggregate strategyRef="myAggregationStrategy" completionSize="1">
<correlationExpression>
<simple>${header.id} == 1</simple>
</correlationExpression>
<to uri="bean:postProcessor?method=process" />
</aggregate>
<to uri="bean:mailer?method=process" />
</route>
</camelContext>
<bean id="myAggregationStrategy" class="com.me.myapp.MyAggregationStrategy" />
<bean id="postProcessor" class="com.me.myapp.PostProcessor" />
<bean id="mailer" class="com.me.myapp.Mailer" />
目前,我并没有真正汇总任何有意义的内容(completionSize=1
),我只是在测试AggregationStrategy
。这是我的策略:
public class MyAggregationStrategy implements AggregationStrategy {
@Override
public Exchange aggregate(Exchange aggregatingExchange, Exchange incomingExchange) {
AppPayload payload = null;
if(aggregatingExchange == null)
payload = new AppPayload(); // This should prevent it from being NULL below in PostProcessor...
else
payload = (AppPayload)incomingExchange.getIn().getBody();
payload.setCargo((Order)incomingExchange.getIn().getBody());
if(aggregatingExchange == null) {
incomingExchange.getIn().setBody(payload);
return incomingExchange;
}
else
return aggregatingExchange;
}
}
还有我的postProcessor
bean:
public class PostProcessor implement Processor {
@Override
public void process(Exchange exchange) {
try {
System.out.println("In PostProcessor...");
AppPayload payload = (AppPayload)exchange.getIn().getBody();
System.out.println("\t...payload acquired...");
if(payload == null)
System.out.println("Payload is NULL.");
} catch(Throwable throwable) {
System.out.println(ExceptionUtils.getFullStackTrace(throwable));
}
}
}
当我运行此代码时,我看到来自preProcessor
bean的日志消息,表明它正在正确执行。我还看到MyAggregationStrategy
正确地“聚合”了该消息,然后在第一条消息到达后将其传递给postProcessor
(再次,因为completionSize=1
)。但是,我在postProcessor
中获得了以下输出:
In PostProcessor...
...payload acquired...
Payload is NULL.
任何人都可以看到为什么payload
为NULL?它是否应该在MyAggregationStrategy
内初始化?!?我很乐意发布更多代码,但我认为这源于我错误地使用AggregationStrategy
API。
答案 0 :(得分:1)
我相信你对aggregatingExchange
和incomingExchange
感到困惑。你能试试这个:
public class MyAggregationStrategy implements AggregationStrategy {
@Override
public Exchange aggregate(Exchange aggregatingExchange, Exchange incomingExchange) {
AppPayload payload = null;
if(aggregatingExchange == null) {
payload = new AppPayload(); // This should prevent it from being NULL below in PostProcessor...
} else {
payload = (AppPayload)aggregatingExchange.getIn().getBody();
}
payload.setCargo((Order)incomingExchange.getIn().getBody());
if(aggregatingExchange == null) {
incomingExchange.getIn().setBody(payload);
return incomingExchange;
} else {
return aggregatingExchange;
}
}
}
答案 1 :(得分:0)
添加@hveiga已经提到的内容。 我有一个类似的问题,我通过添加标题到我的消息解决。 但是在您的情况下,我发现您没有使用拆分器,并且您已经定义了标头。因此,从Clauss Ibssen得到的一条信息是第一次时间交换是空的,我们需要检查空对象。
有关详细说明,请参阅此处 - Apache Camel - Split and aggregate - Old Exchange is always null