Matthias Hryniszak
Matthias Hryniszak

Reputation: 3162

How to periodically process some input using Apache Camel

I wonder if there is a way to make Camel do what I need which is as follows:

"Periodically read data from some source (let's say a file), do some processing on it and write it to some other place"

I figured out how to do all that minus the "periodically" part. I know how to trigger a route using Quartz or Timer. But when I use those the part is already taken so I can't change the body anymore.

Any advice?

Upvotes: 2

Views: 2086

Answers (2)

Claus Ibsen
Claus Ibsen

Reputation: 55555

You can use scheduled route policy as well http://camel.apache.org/scheduledroutepolicy.html

Upvotes: 5

Ben ODay
Ben ODay

Reputation: 21015

you can kick it off with timer/quartz and then use either a content enricher or a polling consumer

//using timer/pollEnrich to populate the body with the polling results
from("timer://foo?period=5000")
    .pollEnrich("file:inbox")
    .to("file:outbout");

//using time/polling consumer bean for more flexibility and multiple polling
from("timer://foo?period=5000")
    .bean(myPollingConsumerBean, "doIt");

public static class MyPollingConsumerBean {
...
    public void doIt() {
      while (true) {
        String msg = consumer.receiveBody("file:inbox", 3000, String.class);
        if (msg == null) {
            break;
        }
        producer.sendBody("file:outbox", msg);
      }
    }
}

Upvotes: 4

Related Questions