1

I am trying to get the List of employee details From Database and Push it in to External System using REST API. I am able to create a route for the above flow .

Also, I want to know the count of created and failure records. So i created a counter by setting property with value 0 and then increment the property value . When i try to increment the property value it always the same as what i initialized.

 from("direct:test")
    .setBody(constant("select name as name, location as location,mobile as mobile from employee"))
    .to("jdbc:testdb")
    .process(exchange ->{
        // Custom Logic which will convert list of Employee  Details
    })
    .split(body())
    .setProperty("successrec", simple("0"))
    .setProperty("failurerec", simple("0"))
    .doTry()
    .setProperty("successrec", simple("${property.successrec++}"))
    .to("http://test/upload/employee")
    .doCatch((Exception.class)).process( exchange ->{   
        
        Integer failureRecords=exchange.getProperty("failurerec",Integer.class);
        exchange.setProperty("failurerec", failureRecords++);

     });

I tried even using processor to set and get the property value, But it didn't worked . Is there any way i can have a counter over success and failure records.

Karthik
  • 27
  • 6

2 Answers2

3

Properties are local to an exchange so you may need to use a Route Policy to inject a global counter.

Create a route policy:

class MyRoutePolicy extends RoutePolicySupport {
    private AtomicInteger counter = new AtomicInteger();

    @Override
    public void onExchangeBegin(Route route, Exchange exchange) {
        exchange.setProperty("counter", counter);
    }

    public int getCounter() {
        return counter.get();
    }
};

Associate the policy to the route:

MyRoutePolicy policy = new MyRoutePolicy();

from("direct:test")
    .routePolicy(policy)
    ...

This is one of the possible solutions but you may also using a global counter that depending on your needs, may be simpler.

Luca Burgazzoli
  • 1,216
  • 7
  • 9
  • Thanks for the info, Could not able to find anything related in the documentation . Could u please give me an example – Karthik Sep 10 '20 at 16:00
  • This could help https://github.com/apache/camel/blob/77f6b83f61d3251cb1475752e5c1b93dca8d6532/core/camel-core/src/test/java/org/apache/camel/impl/RoutePolicyTest.java – Luca Burgazzoli Sep 10 '20 at 20:24
  • Thanks, I'm new to Camel , I couldn't able to figure it out with the above approach. Also, I have another parameter part from counter for a global parameter. So I have used ThreadLocal, Which is working fine ... – Karthik Sep 19 '20 at 13:09
  • added an example – Luca Burgazzoli Sep 21 '20 at 06:36
  • The policy is invoked when an exchange is created so yes it is thread saafe – Luca Burgazzoli Sep 21 '20 at 10:46
  • I have tried the answer, unfortunately my counter is still not increment, can someone provide more information? – Haifeng Zhang Oct 29 '21 at 22:22
0

With this policy works:

public class MyRoutePolicy extends RoutePolicySupport {
    private AtomicInteger counter = new AtomicInteger();

    @Override
    public void onExchangeBegin(Route route, Exchange exchange) {
        exchange.setProperty("counter", counter.incrementAndGet());
    }

    public int getCounter() {
        return counter.get();
    }
};