Home > Mobile >  Apache camel splitter not populating properties in the exchange
Apache camel splitter not populating properties in the exchange

Time:06-29

I have a route Producer -> Split -> Processor and in the processor, I am trying to find out if the current exchange being processed is the last one to get split.

According to the camel split docs, three properties are put into every camel exchange that has been split. CamelSplitIndex, CamelSplitSize and CamelSplitComplete.

When using camel 3.0.0-RC3, I am able to see these properties being populated and available in the processor class. But when using version 3.11.1 or 3.17.0, I do not see these values being populated in the exchange.

Am I missing something here or is this a camel bug.

Main Class:

SpringApplication.run(CamelTestMain.class, args);

Routes:

from("timer://simpleSplitTimer?period=1000000").process(new Processor() {
                            @Override
                            public void process(Exchange exchange) throws Exception {
                                Trade trade1 = new Trade(1L, 15.4, 200, "WF");
                                Trade trade2 = new Trade(2L, 75.1, 100, "GS");
                                Trade trade3 = new Trade(3L, 20.0, 15, "JP");
                                List<Trade> tradeList = new ArrayList<>();
                                tradeList.add(trade1);
                                tradeList.add(trade2);
                                tradeList.add(trade3);
                                exchange.getIn().setHeader("GUITrade", "Y");
                                exchange.getIn().setBody(tradeList);
                            }
                        })
                        .to("direct:toSplit");

                from("direct:toSplit").split(body()).to("direct:afterSplit1");
                from("direct:afterSplit1").process(postSplitProcessor);

Dependencies:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.11.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
            <version>3.0.0-RC3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jms</artifactId>
            <version>3.0.0-RC3</version>
        </dependency>
    </dependencies>

CodePudding user response:

Since you haven't posted the code snippet where you are accessing the Exchange properties, I would guess you are using Exchange#getProperties to access the different properties which has been optimized / refactored in version 3.9.0 of Apache Camel:

The properties on Exchange have been optimized to separate into two: internal state vs user properties. The method getProperties() now only returns user properties. To include internal properties as well, then use getAllProperties(). The other APIs such as getProperty(String) works the same way as before, being able to lookup a property regardless if its internal or custom.

You should be able to access the split related properties:

  • Either switching to use Exchange#getAllProperties:

      exchange.getAllProperties().get("CamelSplitIndex");
    
  • Or better use the safer Exchange#getProperty(ExchangePropertyKey):

      exchange.getProperty(ExchangePropertyKey.SPLIT_INDEX);
    
  • Related