user2519825
user2519825

Reputation: 101

Camel AggregatonStrategy wrong result

I am using CAMEL to aggregate responses from two sources, one being xml and other json. Initially I have stubbed those responses and I am getting them from the file. The goal is to aggregate the responses from both sources.

My aggregator route looks like this

    from("direct:fetchProfile")
    .multicast(new ProfileAggregationStrategy()).stopOnException()
    .enrich("direct:xmlProfile")
    .enrich("direct:jsonProfile")
    .end();

My "direct:xmlProfile" route looks like -

    from("direct:xmlProfile")
    .process(new Processor(){

    @Override
        public void process(Exchange exchange) throws Exception {
            String filename = "target/classes/xml/customerDetails.xml";
             InputStream fileStream = new FileInputStream(filename);
            exchange.getIn().setBody(fileStream);
         }
     })
    .split(body().tokenizeXML("attributes", null))
    .unmarshal(jaxbDataFormat)
    .process(new Processor(){
        @Override
        public void process(Exchange exchange) throws Exception {
            Profile legacyProfile = exchange.getIn().getBody(Profile.class);
            // some more processing here
            exchange.getIn().setBody(legacyProfile);
        }

    });

In the above route I am reading the xml from a file and then using jaxb converter to map elements of interest into the class denoted by 'Profile'. After calling this route, CAMEL calls ProfileAggregationStrategy. The code for this is -

    public class ProfileAggregationStrategy implements AggregationStrategy{
       @Override
       public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
             // this is the problematic line
             Profile newProfile = newExchange.getIn().getBody(Profile.class);
             if (oldExchange == null){
                 return newExchange;
             } else {
                 Profile oldProfile = oldExchange.getIn().getBody(Profile.class);
                 // code to copy merge oldProfile with newProfile
                 return oldExchange;
             }

        }
    }

The issue is with the line marked as 'problematic line'. Even though in the final phase of the route 'direct:xmlProfile' I have explicitly put an object into the Body of the exchange, the newExchange in ProfileAggregationStrategy still shows that Body is of type iostream.

Upvotes: 0

Views: 120

Answers (1)

Claus Ibsen
Claus Ibsen

Reputation: 55540

Read the documentation about the splitter eip.

As the output of the splitter is the input, unless you specify an aggregation strategy to the splitter where you can decide what the output should be. For example if you want to use the last splitted element, then there is a UseLastestAggregrationStrategy you can use.

Upvotes: 1

Related Questions