dark ninja
dark ninja

Reputation: 15

How to access the header in the subsequent splits in Apache Camel

I need to stream a file and process the lines one by line. During this process, I need to keep track of the successful processed lines and failed process lines and publish those metrics for each split.

Camel split will create new exchange on each split so the headers will be lost and also the aggregation will not work here. Since the results will be accessed at the end of the split.

How to resolve this problem using camel?

Sample route:

    onException(Exception.class)
        .handled(true)
        .process(new FailureProcessor()) // increment failureCount
        .to("{{publishMetrics}}");

    from("{{file}}")
        .setHeader("successCount", constant(0))
        .setHeader("failureCount", constant(0))
        .split(body().tokenize("\n"))
        .process(new MyProcessor()) // Process the lines
        .process(new SuccessProcessor()) // Increment the successCount header
        .to("{{publishMetrics}}") // Publish the metrics(successCount & failureCount)
        .end()
        .end(); // split ends

Upvotes: 0

Views: 51

Answers (1)

Paul M
Paul M

Reputation: 357

You can use a custom aggregation strategy to combine the results of your split into a single summary message which you could then publish.

See https://camel.apache.org/components/4.4.x/eips/split-eip.html#_aggregating

Upvotes: 0

Related Questions