0

So, I am trying to learn Apache Camel, and the Hello World example they provide in the Camel in action book is the following:

public class FileCopierWithCamel {
    public static void main(String args[]) throws Exception {
    CamelContext context = new DefaultCamelContext();
    context.addRoutes(new RouteBuilder() {
        public void configure() {
            from("file:data/inbox?noop=true").to("file:data/outbox");
        }
    });
    context.start();

    Thread.sleep(10000);
    context.stop();
}}

This example tries to copy all files from a folder to another another one.

They say that it has less lines and praise how better it is when compared to a long Java version, which actually works better than the solution they propose using Camel.

I say this because this proposed solution suffers from a huge problem: it has a wait. Say that I want to copy 1000 files - or even better - that I do not exactly know the number of files I have in the folder (which is what happens in most cases). Using this solution I actually have to guess how long the operation is going to take. Not only is this a bad start already, it also makes no sense. I am a computer engineer, not a seer.

How can one modify this example as to only stop when it copies all the files?

Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
  • 1
    The context.stop(); is part of the example. You do not need to stop it right after starting it. – Thomas Apr 04 '14 at 12:39
  • 1
    If you don't need a daemon process and if you only want to do a one time job, just use plain old Java. E.g. see http://stackoverflow.com/questions/1146153/copying-files-from-one-directory-to-another-in-java. However, if you need a daemon process and if you need the `noop` functionality, then Camel is pretty slick. – Peter Keller Apr 04 '14 at 13:02
  • So the solution is: use plain old Java. I see where this is going. Thanks ! kudos++ for you sir! – Flame_Phoenix Apr 04 '14 at 13:08
  • See this FAQ and the links it refer to: http://camel.apache.org/running-camel-standalone.html especially that about the cookbook and keep it running. – Claus Ibsen Apr 04 '14 at 20:01
  • A bit late to the party, but think you are missing the main idea behind Apache Camel. It is an integration framework with emphasis on integration. It should not stop when all the files or one file is copied. It should continue unless something happens to make it stop. That is the whole idea of integration. If you just want a one-off thing then you can just write plain java. For a more in depth File polling adapter camel can help. – Souciance Eqdam Rashti Oct 25 '15 at 23:30
  • You know what they say "Better late than never" :D With this in mind I respectfully disagree with your opinion. Camel may be an integration framework, but one that it is also based on rules. Anyway, thanks for commenting ! – Flame_Phoenix Oct 26 '15 at 09:54

1 Answers1

2

There is an alternate way to run simple camel code, one that does not require sleep().

You can simply re-write your example like this:

public class FileCopierWithCamel2 {
    public static void main(String[] args) throws Exception {
        Main camelMain = new Main();
        camelMain.addRouteBuilder(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("file:data/inbox?noop=true").to("file:data/outbox");
            }
        });
        camelMain.run();
    }
}

Hope this helps ;)

vikingsteve
  • 38,481
  • 23
  • 112
  • 156
  • When will the program stop then? Will it stop automatically when all files are copied or will it just keep on running monitoring the folder? – Flame_Phoenix Apr 04 '14 at 13:07
  • It will just keep on monitoring the folder for eternity :) If you want to stop it you need to do so manually. – vikingsteve Apr 04 '14 at 13:38