Chinmay
Chinmay

Reputation: 364

Stream a file from using camel

I am new to Camel and was trying to work on streaming a log. I am trying to stream a log which keeps on changing. So , once my route starts it should keep on streaming the file and give every line to the processor. I tried the following: But then I suppose there might be a better of doing this and just send one line to the reader to process every time. Also, keep the route running to read every time log gets overwritten.

from("stream:file?fileName=/usr/dir/filename=logs.txt?options")
               .process(new Reader());

Also, How I would be able to read the line then in the processor. I searched for hours and I am not able to find a good example.

Upvotes: 1

Views: 4566

Answers (1)

Erik Karlstrand
Erik Karlstrand

Reputation: 1527

The documentation for the Camel Stream component suggests the following:

from("stream:file?fileName=/usr/dir/logs.txt&scanStream=true&scanStreamDelay=1000")
    .process(new Reader());

Also, you're using the fileName option twice, maybe a typo?

If you want to do some custom processing on each line you can create your own Processor:

public class MyProcessor implements Processor {
    @Override
    public void process(Exchange exchange) {
        String body = exchange.getIn().getBody(String.class);
        //Perform some logic and put it back in the exchange
        exchange.getIn().setBody(body);
    }
}

Then in your camel context:

Processor myProcessor = new MyProcessor();
...
from("file:///dir").process(myProcessor);

Upvotes: 2

Related Questions