Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 驼峰聚合策略生成空体消息_Java_Nullpointerexception_Apache Camel_Aggregate_Enterprise Integration - Fatal编程技术网

Java 驼峰聚合策略生成空体消息

Java 驼峰聚合策略生成空体消息,java,nullpointerexception,apache-camel,aggregate,enterprise-integration,Java,Nullpointerexception,Apache Camel,Aggregate,Enterprise Integration,我目前有以下骆驼路线: <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="{{

我目前有以下骆驼路线:

<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" />
还有我的
后处理器
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));
        }
    }
}
当我运行这段代码时,我会看到来自
预处理器
bean的日志消息,表明它正在正确执行。我还看到,
MyAggregationStrategy
正确地“聚合”了消息,然后在第一条消息到达后让它传递给
后处理器(同样,因为
completionSize=1
)。但是,我在
后处理器中得到以下输出:

In PostProcessor...
    ...payload acquired...
Payload is NULL.

有人知道为什么
有效负载
会为空吗?
它不应该在
MyAggregationStrategy
中初始化吗?!?我很高兴发布更多代码,但我相信这是因为我错误地使用了
AggregationStrategy
API。

我相信您会对
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;
        }
    }
}

添加@hveiga已经提到的内容。 我有一个类似的问题,我通过在邮件中添加标题来解决。 然而,在您的例子中,我看到您没有使用splitter,并且已经定义了一个头。从克劳斯·伊布森那里得到的一条信息是,第一次交换的时间是空的,我们需要检查空对象

请参阅此以了解更多解释-

在此处跟踪完整的解释-

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