1

I'm using the camel file component to poll files from a directory. Before I can handle a file some conditions have to be satisfied, if not camel should skip the file without deleting/moving it and go to the next one.

To do this I use this:

public InputStream myMethod(@Body InputStream is, @Headers .....) {

if( !checkPrerequisites )
    throw new MyRuntimeException("conditions not satisfied yet");

So I'm wondering if there is another way to archive the desired behaviour.

sinclair
  • 2,812
  • 4
  • 24
  • 53
  • Are the conditions about the file i.e. filename, filesize etc, or conditions about the data inside the file? – Souciance Eqdam Rashti Jun 17 '16 at 08:40
  • The condition is that another file exists in another directory, if it is not there yet I want to skip the current file and try again later. – sinclair Jun 17 '16 at 09:03
  • Would it be easier if you first look for the file in the second directory, and only when that arrives you pick up file in the current directory? I.e. the route for the current directory is not started until the first file arrives. – Souciance Eqdam Rashti Jun 17 '16 at 09:25
  • Well, then I would have the same problem just vice versa. – sinclair Jun 17 '16 at 09:32
  • Well, in this case, route1 is polling the second directory for a file and only when that file arrives, does it start route2 which picks up the file in the current directory. Alternatively, you don't need two routes. In route1, once the first file arrives, you can use enrich to grab the second file. – Souciance Eqdam Rashti Jun 17 '16 at 09:37
  • What would route1 do if the file is not present? It would lead to the same situation addressed in my question. – sinclair Jun 17 '16 at 09:47
  • Well, route1 wouldn't do anything unless the file is present. and only then pick up the file in the current directory. Basically, it removes the need to skip the current file if the first conditional file is not present. – Souciance Eqdam Rashti Jun 17 '16 at 09:51
  • That's the point, the route is not supposed to do nothing, it should try the next file. – sinclair Jun 17 '16 at 09:57
  • Well, the solution works for multiple files as well. You just then need to add a file filter like Andy mentions below. I would use one route that has a file filter and once there is a match use poll enrich to fetch the corresponding file. – Souciance Eqdam Rashti Jun 17 '16 at 10:36

1 Answers1

4

You could implement a GenericFileFilter. Create the filter, like so:

public class AnotherFileExistsFilter<T> implements GenericFileFilter<T> {

    @Override
    public boolean accept(GenericFile<T> firstFile) {
        return Files.exists(Paths.get("/some/other/folder/" + firstFile.getFileName()));
    }
}

Add it to your endpoint using filter=#anotherFileExistsBeanName.

If you want to keep checking the file, set idempotent=false, and I recommend setting a delay (delay=xxx in ms) in order to not poll the folder continuously.

More details are on the Apache Camel File2 page.

AndyN
  • 2,075
  • 16
  • 25
  • Will this continue with the next file or will it idle until the first file could be processed? – sinclair Jun 17 '16 at 10:12
  • No, it won't idle until the first file can be processed. Camel will process all files in a directory before retrying any file. – AndyN Jun 17 '16 at 10:23
  • I'm getting this error: `No type converter available to convert from type: java.lang.String to the required type: org.apache.camel.component.file.GenericFileFilter` – sinclair Jun 17 '16 at 11:39
  • Sounds like you're not referencing your bean correctly: http://stackoverflow.com/questions/23015179/how-to-set-up-route-in-java-that-uses-bean-references. You need to make sure you've registered your GenericFileFilter bean, and you're using that bean name in the endpoint. – AndyN Jun 17 '16 at 12:06
  • Thanks so far, I've created a new question for this problem http://stackoverflow.com/questions/37882752/camel-file-component-filter-with-cdi – sinclair Jun 17 '16 at 13:24