-1

From the camel book, section 'Using pollEnrich to merge additional data with an existing message', it shows that you can merge the oldExchange(from the quarz) with the new one (from ftp).

The problem is that I have a file from a topic(old Exchange) and I use pollEnrich to get a new file from a ftp server and I want to merge this too. I am interested in set some headers from oldExchange to the newExchange.

The problem that I am facing is that the oldExchange is all the time null. I have read the examples from camel book, for aggregator and there said: "The first message arrives for the first group. == null". I don't understand, then where is my oldExchange? the one from the topic. Why only at the second iteration the exchange is not null (for the same group).

from("myTopic")
    .pollEnrich()
    .simple("ftp://myUrl&fileName=${in.headers.test}")
    .aggregate((Exchange oldExchange, Exchange newExchange) -> {
        final String oldHeader = oldExchange.getIn().getHeader("test", String.class);
        newExchange.getIn().setHeader("test", oldHeader);
        return newExchange;
    })

I have read this: http://camel.465427.n5.nabble.com/Split-and-Aggregate-Old-Exchange-is-null-everytime-in-AggregationStrategy-td5746365.html#a5746405 and still I don't understand how can both messages belong to the same group.

agata
  • 481
  • 2
  • 9
  • 29

2 Answers2

3

The first message arrives for the first group. == null. I don't understand ...

This is true for a standard aggregation where you aggregate for example multiple incoming messages to one. In this case, on the first incoming message the aggregator is still empty and therefore the oldExchange (aggregator content) is null. You have to wait for another (second) message to be able to aggregate something.

However, in your case (enrich) the oldExchange should not be null because the first message, i.e. the message from your topic, is already there.

Have you tried to inspect the message from the topic in the debugger or log it out before it reaches the enricher? Just to make sure it is not empty.

Added after a test

This is fascinating, I tried this with a unit test and when I define the pollEnrich as you do, I get the inverse result: My consumed message routed by .from(...) is the oldExchange and my newExchange is always null.

However, if I define the pollEnrich "inline", it works fine

.pollEnrich("URI", Timeout, (AggregationStrategy))

I suspect that this is explainable if you analyze what the DSL does with these two definitions, but from my quick test perspective it looks a bit strange.

burki
  • 6,741
  • 1
  • 15
  • 31
  • Yes, I logged the message. I can see the whole body in the console. I don’t understand where it “disappears”. Before sending the message to my topic it goes through a processor where I set the “test” header and then to my direct topic. And simple evaluates the header from the old exchange to be able to poll the specific file from my server. So the old exchange is there, but still in my aggregation it is null. – agata Aug 05 '19 at 16:31
  • I extended my answer after doing a quick test – burki Aug 06 '19 at 07:53
1

@burki true, is it working as you said with the aggregationStrategy inside the pollEnrich() but I need the simple because I am calling an endpoint dynamically and I cannot do this in the pollEnrich (or at least I don't know how).

I was able to solve like this:

from("myTopic")
.pollEnrich()
.simple("ftp://myUrl&fileName=${in.headers.test}")
.aggregationStrategy((Exchange oldExchange, Exchange newExchange) -> {
    final String oldHeader = oldExchange.getIn().getHeader("test", String.class);
    newExchange.getIn().setHeader("test", oldHeader);
    return newExchange;
})

So instead of the .aggregate call, I am using .aggregationStrategy , what I understood is that the .aggregate call is for the standard aggregation (as @burki mentioned) if we want to aggregate multiple messages and the .aggregationStrategy call can be used to merge 2 messages (one of them is from an external service).

agata
  • 481
  • 2
  • 9
  • 29