0

Apache Camel has a very nice support for various protocols like FTP and "file" and I would like to use it just to just load a single file and maybe convert it a bit.

I understood how to do this using a RouteBuilder and CamelContext.start() like in the examples but I don't want a background daemon in an endless loop nor do I need complex routing.

How can I do something like:

String body = new DefaultCamelContext().justRunOnce(
   from("ftp://ftp.example.com/data/foo.txt")
     .validate(body().regex("My Data.*"))
     .getBody());

I did read some answers on how to stop a Route using "controlbus?action=stop" or "timer?repeat=1" or similar but those still start Camel as a new thread and then use some hack to stop it. I'm not looking for a complicated solution that somehow manages it but rather would like to know if Camel is capable to be used in a lean 10-liner, like in my example above.

lathspell
  • 3,040
  • 1
  • 30
  • 49
  • Possible duplicate of [How to get Camel FTP to fetch only once on demand](http://stackoverflow.com/questions/31819073/how-to-get-camel-ftp-to-fetch-only-once-on-demand) – René Scheibe Feb 06 '17 at 22:43
  • 1
    Surely, there are better approaches than using an enterprise integration patterns framework to download a single file and do some very simple processing on it. – Strelok Feb 07 '17 at 00:12
  • Just use the ConsumerTemplate to download the file once, and no need for any Routes – Claus Ibsen Feb 07 '17 at 11:14
  • Thanks @ClausIbsen, using camel.createConsumerTemplate().receiveBody() solves the "getBody()" problem from my question and I can use a singel endpoint. Too bad it does not accept a RouteDefinition as parameter. – lathspell Feb 07 '17 at 21:10
  • @Strelok: if I have FTP, file and SFTP, all with a bit of validating I already use EIP and several individual libraries. I just want integrating Camel components into an existing project and not yet have Camel as the main engine that dispatches everything. – lathspell Feb 07 '17 at 21:15

1 Answers1

1

You can use the pollEnricht feature, see http://camel.apache.org/content-enricher.html:

Example, you can also use a ftp endpoint instead of a file endpoint:

<route>
 <from uri="direct:start"/>
  <pollEnrich>
   <constant>file:inbox?fileName=data.txt</constant>
  </pollEnrich>
<to uri="direct:result"/>

soilworker
  • 1,317
  • 1
  • 15
  • 32