Yogen Rai
Yogen Rai

Reputation: 3033

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.

Upvotes: 4

Views: 9874

Answers (3)

Camel Rider
Camel Rider

Reputation: 41

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}")
;

Upvotes: 4

Yogen Rai
Yogen Rai

Reputation: 3033

I was actually trying to call this route from another route or cascade it within a route. I found this working:

public static final String FILE_ROUTE_ID = "file.route";
public static final String FILE_ROUTE_URI = "{{file.route.uri}}";

@Override
public void configure() throws Exception {

    from(FILE_ROUTE_URI)
            .routeId(FILE_ROUTE_ID)
            .log(LoggingLevel.INFO, "Header {}", String.valueOf(simple("${header.purpose}")))
            .from("file:apache-camel-spring-boot?fileName=printing.key&noop=true")
            .convertBodyTo(String.class)
            .process(exchange -> {
                log.info("Processing file . . .");
                KeyBody keyBody = new KeyBody(exchange.getIn().getBody(String.class));
                exchange.getIn().setBody(keyBody);
            });
}

Thank you all for looking into this!! Cheers!

Upvotes: 1

Roman Vottner
Roman Vottner

Reputation: 12829

What exectly do you want to test? The file component is tested by Camel already. For testing the processors and beans involved in your route you basically don't need the file component hence replacing the from part with i.e. a direct:start via advicing your route is probably the recommended way.

If you insist on testing the file component, which works on files or directories, you should write test files into a test directory within your test and then start your route and see if the files are consumed and processed correctly. JUnit provides a TemporaryFolder that might help you on creating and cleaning up test directories. You can see my answer to a similar question to see how this can be done with Camel.

Upvotes: 0

Related Questions