Search code examples
javaconfigurationroutesapache-camelfile-uri

Camel File processing


I'm using Camel (2.11.0) to try and achieve the following functionality:

  • If a file exists at a certain location, copy it to another location and then begin processing it
  • If no such file exists, then I don't want the file consumer/poller to block; I just want processing to continue to a direct:cleanup route

I only want the file to be polled once!

Here's what I have so far (using Spring XML):

<camelContext id="my-camel-context" xmlns="http://camel.apache.org/schema/spring">
    <route id="my-route
        <from uri="file:///home/myUser/myApp/fizz?include=buzz_.*txt"/>

        <choice>
            <when>
                <!-- If the body is empty/NULL, then there was no file. Send to cleanup route. -->
                <simple>${body} == null</simple>
                <to uri="direct:cleanup" />
            </when>

            <otherwise>
                <!-- Otherwise we have a file. Copy it to the parent directory, and then continue processing. -->
                <to uri="file:///home/myUser/myApp" />
            </otherwise>
        </choice>

        <!-- We should only get here if a file existed and we've already copied it to the parent directory. -->
        <to uri="bean:shouldOnlyGetHereIfFileExists?method=doSomething" />
    </route>

    <!--
        Other routes defined down here, including one with a "direct:cleanup" endpoint.
    -->
</camelContext>

With the above configuration, if there is no file at /home/myUser/myApp/fizz, then Camel just waits/blocks until there is one. Instead, I want it to just give up and move on to direct:cleanup.

And if there is a file, I see it getting processed inside the shouldOnlyGetHereIfFileExists bean, but I do not see it getting copied to /home/myUser/myApp; so it's almost as if the <otherwise> element is being skipped/ignored altogether!

Any ideas? Thanks in advance!


Solution

  • Try this setting, and tune your polling interval to suit:

    From Camel File Component docs:

    sendEmptyMessageWhenIdle

    default =false

    Camel 2.9: If the polling consumer did not poll any files, you can enable this option to send an empty message (no body) instead.

    Regarding writing the file, add a log statement inside the <otherwise> to ensure it's being executed. If so, check file / folder permissions, etc.

    Good luck.