Camel Rest DSL - AggregationStrategy奇怪的行为

时间:2017-01-14 22:24:32

标签: apache-camel

标题有点像一个文字布道者,如果它不起作用,那当然是我的错。应该这样做。

我想执行从rdbms到solr和mongo db的数据传输。 为此,我必须完成以下步骤(例如):

  • 让客户ID转移
  • 获取客户详细信息
  • 获取客户发票
  • 获取客户付款

然后,聚合并保存到mongo db和solr进行索引。

这是我的代码,但我无法让它工作:

from("seda:initial-data-transfer")
        .setProperty("recipientList", simple("direct:details,direct:invoices,direct:payments"))
        .setProperty("afterAggregate", simple("direct:mongodb,direct:solr"))
        .setBody(constant("{{query.initial-data-transfer.ids}}"))
        .to(jdbc)
        .process(new RowSetIdsProcessor())
        .split().tokenize(",", 1000) // ~200k ids - group by 1000 ids
        .to("direct:customers-ids");

from("direct:customers-ids")
        .recipientList(exchangeProperty("recipientList").tokenize(","))
        // ? .aggregationStrategy(new CustomerAggregationStrategy()).parallelProcessing()
        .aggregate(header("CamelCorrelationId"), new CustomerAggregationStrategy())             
        .completionPredicate(new CustomerAggregationPredicate()) // true if details + invoices + payments, etc ....
        // maybe a timeOut here ?
        .process(businessDataServiceProcessor)
        .recipientList(exchangeProperty("afterAggregate").tokenize(","));

from("direct:details")
        .setHeader("query", constant("{{query.details}}"))
        .bean(SqlTransform.class,"detailsQuery").to(jdbc)
        .process(new DetailsProcessor());

from("direct:invoices")
        .setHeader("query", constant("{{query.invoices}}"))
        .bean(SqlTransform.class,"invoicessQuery").to(jdbc)
        .process(new InvoicesProcessor());

我不明白AggregationStrategy是如何工作的。 有时,我可以执行2或3个1000个ID的块,并保存到mongo DB和Solr 但之后,在aggregationStrategy中所有交换都是空的......

我尝试了很多...但每次聚合都失败了。

感谢您的帮助

更新:

以下是CustomerAggregationStrategy的一部分:

public class CustomerAggregationStrategy implements AggregationStrategy {
    @Override
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        Message newIn = newExchange.getIn();

        CustomerDataCollector collector = null;
        if (oldExchange == null) {
            int completionSize = newExchange.getProperty("completionSize", Integer.class);
            collector = new CustomerDataCollector(completionSize);
            CollectData(collector, newIn, newExchange);
            newIn.setBody(collector);
            return newExchange;
        }
        collector = oldExchange.getIn().getBody(CustomerDataCollector.class);
        CollectData(collector, newIn, newExchange);
        return oldExchange;
    }

    private void CollectData(CustomerDataCollector collector, Message message, Exchange exchange) {
        String recipientListEndpoint = (String)exchange.getProperty(Exchange.RECIPIENT_LIST_ENDPOINT);
        switch (recipientListEndpoint){
            case "direct://details" :
                collector.setDetails(message.getBody(Map.class));
                break;
            case "direct://invoices" :
                collector.setInvoices(message.getBody(Map.class));
                break;
            case "direct://payments" :
                collector.setPayments(message.getBody(Map.class));
                break;
        }
    }
}

更新:

我可以在CustomerAggregationStrategy中记录这个:

String camelCorrelationId = (String)exchange.getProperty(Exchange.CORRELATION_ID);

[t-AggregateTask] .i.c.a.CustomerAggregationStrategy : CustomerAggregationStrategy.CollectData : direct://details  ID-UC-0172-50578-1484523575668-0-5
[t-AggregateTask] .i.c.a.CustomerAggregationStrategy : CustomerAggregationStrategy.CollectData : direct://invoices ID-UC-0172-50578-1484523575668-0-5
[t-AggregateTask] .i.c.a.CustomerAggregationStrategy : CustomerAggregationStrategy.CollectData : direct://payments ID-UC-0172-50578-1484523575668-0-5

CamelCorrelationId的值与预期相同。 我认为CamelCorrelationId是正确的。不是吗?

1 个答案:

答案 0 :(得分:0)

好的,现在好多了。

在tokeniszer之后,我像这样设置属性CustomCorrelationId。

    .split().tokenize(",", 1000)
    .setProperty("CustomCorrelationId",header("breadcrumbId"))
    .to("direct:customers-ids")

并像这样汇总这个值:

    from("direct:customers-ids")
            .recipientList(exchangeProperty("recipientList").tokenize(","))

    from("direct:details")
            .setHeader("query", constant("{{query.details}}"))
            .bean(SqlTransform.class,"detailsQuery").to(jdbc)
            .process(new DetailsProcessor())
            .to("direct:aggregate");      

...

    from("direct:aggregate").routeId("aggregate")
            .log("route : ${routeId}")
            .aggregate(property("CustomCorrelationId"), new CustomAggregationStrategy())
            .completionPredicate(new CustomerAggregationPredicate())
            .process(businessDataServiceProcessor)
            .recipientList(exchangeProperty("afterAggregate").tokenize(","));

此工作正常,数据已正确汇总。谢谢你的帮助。 你指出了方向。