Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Camel route that reads file?

I have camel route to read a file as below:

@Component
public class MessageRoute extends RouteBuilder {

    public static final String ROUTE_ID = "message.route";
    public static final String ROUTE_URI = "{{message.route.uri}}";

    @Override
    public void configure() throws Exception {

        from("file:://target/test.txt")
                .convertBodyTo(String.class)
                .process(exchange -> {
                    log.info("Body {}", exchange.getIn().getBody(String.class));
                });
    }
}

Now, the question is how to make a call to this route? My end goal is to call from producerTemplate and process the file content.

I couldn't find anything about this on Camel Docs

Also, I tried to use pollEnrich as mentioned in this answer, but while debugging, execution doesn't get there at all to aggregator.

I would be million dollars thankful for Any solution, suggestion or idea.

like image 231
Yogen Rai Avatar asked Nov 01 '25 04:11

Yogen Rai


1 Answers

I had to do something similar. This following works in camel 2.18+ -

rest("/load")
.get("/sampleFile")
.to("direct:readFromSampleFile")
    ;
from("direct:readFromSampleFile")
.pollEnrich("file://c:/folder?fileName=sample.txt&noop=true&idempotent=false") // idempotent to allow re-read, no-op to keep the file untouched
.convertBodyTo(String.class)
.log("Read ${body}")
.unmarshal().json(JsonLibrary.Jackson)
.setHeader("Content-Type").constant("application/json")
.log("Returned ${body}")
;
like image 76
Camel Rider Avatar answered Nov 02 '25 17:11

Camel Rider



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!