Camel - content enricher: enrich() vs pollEnrich()

2019-02-22 01:50发布

问题:

1ST QUESTION:

I'm not able to really understand the difference between enrich() and pollEnrich(). Maybe the terms Camel uses are not so great.

I read here: http://camel.apache.org/content-enricher.html

Content enrichment using the enrich DSL element

Camel comes with two flavors of content enricher in the DSL

  • enrich
  • pollEnrich

enrich uses a Producer to obtain the additional data. It is usually used for Request Reply messaging, for instance to invoke an external web service. pollEnrich on the other hand uses a Polling Consumer to obtain the additional data. It is usually used for Event Message messaging, for instance to read a file or download a FTP file.

I don't understand what the difference is. They both seem to get the additional data (Web service response, FTP file) by consuming it. So why do they say getting the Web service response is done by a "producer"?

2ND QUESTION:

In the "Camel in action" book p. 72 they say:

Enrich and pollEnrich can’t access information in the current exchange

Neither enrich nor pollEnrich can leverage any information from the current exchange. This means, for example, that you can’t store a filename header on the exchange for pollEnrich to use to select a particular file. This may change in the future if the Camel team can find a solution.

However they give a code example similar to the following, for implementing an aggregation strategy:

public class ExampleAggregationStrategy implements AggregationStrategy {

    public Exchange aggregate(Exchange original, Exchange resource) {
        Object originalBody = original.getIn().getBody();
        Object resourceResponse = resource.getIn().getBody();
        Object mergeResult = ... // combine original body and resource response
        if (original.getPattern().isOutCapable()) {
            original.getOut().setBody(mergeResult);
        } else {
            original.getIn().setBody(mergeResult);
        }
        return original;
    }

}

In this example I see that they have access to the Exchange original, is it not the "current exchange"? If not, then what exchange does the "original exchange" represent? And what do they mean by the "current exchange"?

回答1:

Here is the difference:

  • enrich assumes you want to use an incoming Exchange as a parameter to an another service request. for example, your incoming Exchange could be a userID, but you really need the entire User object, so you could enrich it by passing the userID to a REST service that returns the User object which becomes the Exchange, etc.

  • pollEnrich assumes the incoming Exchange is a simple trigger that tell a PollingConsumer to look for data and create an Exchange (ignoring the contents of the incoming Exchange). For example, you could have a timer or other business process event that requires picking up a file for processing, etc. that said, the incoming Exchange data is NOT used to dynamically configure the PollingConsumer...only the URI is used for this.

That said, as of Camel 2.12, there is an option to specify an aggregation strategy to combine the incoming/returned Exchanges for both enrich and pollEnrich

See this unit test for an example of pollEnrich with an aggregation strategy