camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > File2
Date Sun, 29 Apr 2012 12:51:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/File2">File2</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >from(&quot;file:bar?doneFileName=done&quot;); <br>{code} <br></td></tr>
            <tr><td class="diff-changed-lines" >Will only consume files from the bar folder, if a file name done exists in the same directory as the target files. Camel will automatically delete the done file when it&#39;s done consuming the files. <span class="diff-added-words"style="background-color: #dfd;">From Camel *2.9.3* onwards Camel will not automatic delete the done file if {{noop=true}} is configured.</span> <br></td></tr>
            <tr><td class="diff-unchanged" > <br>However its more common to have one done file per target file. This means there is a 1:1 correlation. To do this you must use dynamic placeholders in the {{doneFileName}} option. Currently Camel supports the following two dynamic tokens: {{file:name}} and {{file:name.noext}} which must be enclosed in ${ }. The consumer only supports the static part of the done file name as either prefix or suffix (not both). <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="File2-FileComponent"></a>File Component </h2>

<p>The File component provides access to file systems, allowing files to be processed by any other Camel <a href="/confluence/display/CAMEL/Components" title="Components">Components</a> or messages from other components to be saved to disk.</p>

<h3><a name="File2-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
file:directoryName[?options]
</pre>
</div></div>

<p>or</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
file:<span class="code-comment">//directoryName[?options]</span>
</pre>
</div></div>

<p>Where <b>directoryName</b> represents the underlying file directory.</p>

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>Only directories</b><br />Camel supports only endpoints configured with a starting directory. So the <b>directoryName</b> must be a directory.<br/>
If you want to consume a single file only, you can use the <b>fileName</b> option, e.g. by setting <tt>fileName=thefilename</tt>.<br/>
Also, the starting directory must not contain dynamic expressions with ${ } placeholders. Again use the <tt>fileName</tt> option to specify the dynamic part of the filename.</td></tr></table></div>  

<div class='panelMacro'><table class='warningMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/forbidden.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>Avoid reading files currently being written by another application</b><br />Beware the JDK File IO API is a bit limited in detecting whether another application is currently writing/copying a file. And the implementation can be different depending on OS platform as well. This could lead to that Camel thinks the file is not locked by another process and start consuming it. Therefore you have to do you own investigation what suites your environment. To help with this Camel provides different <tt>readLock</tt> options and <tt>doneFileOption</tt> option that you can use. See also the section <em>Consuming files from folders where others drop files directly</em>.</td></tr></table></div>

<h3><a name="File2-URIOptions"></a>URI Options</h3>

<h4><a name="File2-Common"></a>Common</h4>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>autoCreate</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> Automatically create missing directories in the file's pathname. For the file consumer, that means creating the starting directory. For the file producer, it means the directory the files should be written to. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>bufferSize</tt> </td>
<td class='confluenceTd'> 128kb </td>
<td class='confluenceTd'> Write buffer sized in bytes. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>fileName</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Use <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a> such as <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a> to dynamically set the filename. For consumers, it's used as a filename filter. For producers, it's used to evaluate the filename to write. If an expression is set, it take precedence over the <tt>CamelFileName</tt> header. (<b>Note:</b> The header itself can also be an <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a>). The expression options support both <tt>String</tt> and <tt>Expression</tt> types. If the expression is a <tt>String</tt> type, it is <b>always</b> evaluated using the <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a>. If the expression is an <tt>Expression</tt> type, the specified <tt>Expression</tt> type is used - this allows you, for instance, to use <a href="/confluence/display/CAMEL/OGNL" title="OGNL">OGNL</a> expressions. For the consumer, you can use it to filter filenames, so you can for instance consume today's file using the <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a> syntax: <tt>mydata-${date:now:yyyyMMdd}.txt</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>flatten</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Flatten is used to flatten the file name path to strip any leading paths, so it's just the file name. This allows you to consume recursively into sub-directories, but when you eg write the files to another directory they will be written in a single directory. Setting this to <tt>true</tt> on the producer enforces that any file name recived in <tt>CamelFileName</tt> header will be stripped for any leading paths. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>charset</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.9.3:</b> this option is used to specify the encoding of the file, and camel will set the Exchange property with <tt>Exchange.CHARSET_NAME</tt> with the value of this option. You can use this on the consumer, to specify the encodings of the files, which allow Camel to know the charset it should load the file content in case the file content is being accessed. Likewise when writing a file, you can use this option to specify which charset to write the file as well. See further below for a examples and more important details. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>copyAndDeleteOnRenameFail</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>Camel 2.9</b>: whether to fallback and do a copy and delete file, in case the file could not be renamed directly. This option is not available for the <a href="/confluence/display/CAMEL/FTP2" title="FTP2">FTP</a> component. </td>
</tr>
</tbody></table>
</div>
</div>

<h4><a name="File2-Consumer"></a>Consumer</h4>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>initialDelay</tt> </td>
<td class='confluenceTd'> <tt>1000</tt> </td>
<td class='confluenceTd'> Milliseconds before polling the file/directory starts. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>delay</tt> </td>
<td class='confluenceTd'> <tt>500</tt> </td>
<td class='confluenceTd'> Milliseconds before the next poll of the file/directory. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>useFixedDelay</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Controls if fixed delay or fixed rate is used. See <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html" class="external-link" rel="nofollow">ScheduledExecutorService</a> in JDK for details. In <b>Camel 2.7.x</b> or older the default value is <tt>false</tt>. From <b>Camel 2.8</b> onwards the default value is <tt>true</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>runLoggingLevel</tt> </td>
<td class='confluenceTd'> <tt>TRACE</tt> </td>
<td class='confluenceTd'> <b>Camel 2.8:</b> The consumer logs a start/complete log line when it polls. This option allows you to configure the logging level for that. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>recursive</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> If a directory, will look for files in all the sub-directories as well. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>delete</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> If <tt>true</tt>, the file will be deleted <b>after</b> it is processed </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>noop</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> If <tt>true</tt>, the file is not moved or deleted in any way. This option is good for readonly data, or for <a href="/confluence/display/CAMEL/ETL" title="ETL">ETL</a> type requirements. If <tt>noop=true</tt>, Camel will set <tt>idempotent=true</tt> as well, to avoid consuming the same files over and over again. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>preMove</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a> (such as <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a>) used to dynamically set the filename when moving it <b>before</b> processing. For example to move in-progress files into the <tt>order</tt> directory set this value to <tt>order</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>move</tt> </td>
<td class='confluenceTd'> <tt>.camel</tt> </td>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a> (such as <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a>) used to dynamically set the filename when moving it <b>after</b> processing. To move files into a <tt>.done</tt> subdirectory just enter <tt>.done</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>moveFailed</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'>  <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a> (such as <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a>) used to dynamically set a different target directory when moving files after processing (configured via <tt>move</tt> defined above) failed. For example, to move files into a <tt>.error</tt> subdirectory use: <tt>.error</tt>. Note: When moving the files to the “fail” location Camel will <b>handle</b> the error and will not pick up the file again. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>include</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Is used to include files, if filename matches the regex pattern. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>exclude</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Is used to exclude files, if filename matches the regex pattern. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>antInclude</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> Ant style filter inclusion, for example <tt>antInclude=&#42;&#42;/&#42;.txt</tt>. Multiple inclusions may be specified in comma-delimited format. See <a href="#File2-FilteringusingANTpathmatcher">below</a> for more details about ant path filters. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>antExclude</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> Ant style filter exclusion. If both <tt>antInclude</tt> and <tt>antExclude</tt> are used, <tt>antExclude</tt> takes precedence over <tt>antInclude</tt>. Multiple exclusions may be specified in comma-delimited format. See <a href="#File2-FilteringusingANTpathmatcher">below</a> for more details about ant path filters. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>idempotent</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Option to use the <a href="/confluence/display/CAMEL/Idempotent+Consumer" title="Idempotent Consumer">Idempotent Consumer</a> EIP pattern to let Camel skip already processed files. Will by default use a memory based LRUCache that holds 1000 entries. If <tt>noop=true</tt> then idempotent will be enabled as well to avoid consuming the same files over and over again. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>idempotentRepository</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> A pluggable repository <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/spi/IdempotentRepository.html" class="external-link" rel="nofollow">org.apache.camel.spi.IdempotentRepository</a> which by default use <tt>MemoryMessageIdRepository</tt> if none is specified and <tt>idempotent</tt> is <tt>true</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>inProgressRepository</tt> </td>
<td class='confluenceTd'> <tt>memory</tt> </td>
<td class='confluenceTd'> A pluggable in-progress repository <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/spi/IdempotentRepository.html" class="external-link" rel="nofollow">org.apache.camel.spi.IdempotentRepository</a>. The in-progress repository is used to account the current in progress files being consumed. By default a memory based repository is used. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>filter</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Pluggable filter as a <tt>org.apache.camel.component.file.GenericFileFilter</tt> class. Will skip files if filter returns <tt>false</tt> in its <tt>accept()</tt> method. More details in section below. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>sorter</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Pluggable sorter as a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Comparator.html" class="external-link" rel="nofollow">java.util.Comparator&lt;org.apache.camel.component.file.GenericFile&gt;</a> class. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>sortBy</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Built-in sort using the <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a>. Supports nested sorts, so you can have a sort by file name and as a 2nd group sort by modified date. See sorting section below for details. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>readLock</tt> </td>
<td class='confluenceTd'> <tt>markerFile</tt> </td>
<td class='confluenceTd'> Used by consumer, to only poll the files if it has exclusive read-lock on the file (i.e. the file is not in-progress or being written). Camel will wait until the file lock is granted. <br/>
This option provides the build in strategies: 
<br class="atl-forced-newline" /> <tt>markerFile</tt> Camel creates a marker file and then holds a lock on it. This option is <b>not</b> available for the <a href="/confluence/display/CAMEL/FTP2" title="FTP2">FTP</a> component.
<br class="atl-forced-newline" /> <tt>changed</tt> is using file length/modification timestamp to detect whether the file is currently being copied or not. Will at least use 1 sec. to determine this, so this option cannot consume files as fast as the others, but can be more reliable as the JDK IO API cannot always determine whether a file is currently being used by another process. This option is <b>only</b> avail for the <a href="/confluence/display/CAMEL/FTP2" title="FTP2">FTP</a> component from <b>Camel 2.8</b> onwards. 
<br class="atl-forced-newline" /> <tt>fileLock</tt> is for using <tt>java.nio.channels.FileLock</tt>. This option is <b>not</b> avail for the <a href="/confluence/display/CAMEL/FTP2" title="FTP2">FTP</a> component.
<br class="atl-forced-newline" /> <tt>rename</tt> is for using a try to rename the file as a test if we can get exclusive read-lock.
<br class="atl-forced-newline" /> <tt>none</tt> is for no read locks at all.</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>readLockTimeout</tt> </td>
<td class='confluenceTd'><ul class="alternate" type="square">
	<li></li>
</ul>
</td>
<td class='confluenceTd'> Optional timeout in millis for the read-lock, if supported by the read-lock. If the read-lock could not be granted and the timeout triggered, then Camel will skip the file. At next poll Camel, will try the file again, and this time maybe the read-lock could be granted. Use a value of 0 or lower to indicate forever. In <b>Camel 2.0</b> the default value is 0. In <b>Camel 2.1</b> the default value is 10000. Currently <tt>fileLock</tt>, <tt>changed</tt> and <tt>rename</tt> support the timeout. For <a href="/confluence/display/CAMEL/FTP2" title="FTP2">FTP</a> the default <tt>readLockTimeout</tt> value is 20000. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>readLockCheckInterval</tt> </td>
<td class='confluenceTd'> <tt>1000</tt> </td>
<td class='confluenceTd'> <b>Camel 2.6:</b> Interval in millis for the read-lock, if supported by the read lock. This interval is used for sleeping between attempts to acquire the read lock. For example when using the <tt>changed</tt> read lock, you can set a higher interval period to cater for <em>slow writes</em>. The default of 1 sec. may be <em>too fast</em> if the producer is very slow writing the file. For <a href="/confluence/display/CAMEL/FTP2" title="FTP2">FTP</a> the default <tt>readLockCheckInterval</tt> is <tt>5000</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>directoryMustExist</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.5:</b> Similar to <tt>startingDirectoryMustExist</tt> but this applies during polling recursive sub directories. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>doneFileName</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.6:</b> If provided, Camel will only consume files if a <em>done</em> file exists. This option configures what file name to use. Either you can specify a fixed name. Or you can use dynamic placeholders. The <em>done</em> file is <b>always</b> expected in the same folder as the original file. See <em>using done file</em> and <em>writing done file</em> sections for examples. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>exclusiveReadLockStrategy</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> Pluggable read-lock as a <tt>org.apache.camel.component.file.GenericFileExclusiveReadLockStrategy</tt> implementation. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maxMessagesPerPoll</tt> </td>
<td class='confluenceTd'> <tt>0</tt> </td>
<td class='confluenceTd'> An integer to define a maximum messages to gather per poll. By default no maximum is set. Can be used to set a limit of e.g. 1000 to avoid when starting up the server that there are thousands of files. Set a value of 0 or negative to disabled it. See more details at <a href="/confluence/display/CAMEL/Batch+Consumer" title="Batch Consumer">Batch Consumer</a>. <b>Notice:</b> If this option is in use then the <a href="/confluence/display/CAMEL/File2" title="File2">File</a> and <a href="/confluence/display/CAMEL/FTP2" title="FTP2">FTP</a> components will limit <b>before</b> any sorting. For example if you have 100000 files and use <tt>maxMessagesPerPoll=500</tt>, then only the first 500 files will be picked up, and then sorted. You can use the <tt>eagerMaxMessagesPerPoll</tt> option and set this to <tt>false</tt> to allow to scan all files first and then sort afterwards. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>eagerMaxMessagesPerPoll</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>Camel 2.9.3:</b> Allows to control whether the limit from <tt>maxMessagesPerPoll</tt> is eager or not. If eager then the limit is during the scanning of files. Where as <tt>false</tt> would scan all files, and then perform sorting. Setting this option to <tt>false</tt> allows to sort all files first, and then limit the poll. Mind that this requires a higher memory usage as all file details are in memory to perform the sorting. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>minDepth</tt> </td>
<td class='confluenceTd'> 0 </td>
<td class='confluenceTd'> <b>Camel 2.8</b>: The minimum depth to start processing when recursively processing a directory. Using <tt>minDepth=1</tt> means the base directory. Using <tt>minDepth=2</tt> means the first sub directory. This option is supported by <a href="/confluence/display/CAMEL/FTP2" title="FTP2">FTP</a> consumer from <b>Camel 2.8.2, 2.9</b> onwards. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maxDepth</tt> </td>
<td class='confluenceTd'> <tt>Integer.MAX_VALUE</tt> </td>
<td class='confluenceTd'> <b>Camel 2.8:</b> The maximum depth to traverse when recursively processing a directory. This option is supported by <a href="/confluence/display/CAMEL/FTP2" title="FTP2">FTP</a> consumer from <b>Camel 2.8.2, 2.9</b> onwards. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>processStrategy</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> A pluggable <tt>org.apache.camel.component.file.GenericFileProcessStrategy</tt> allowing you to implement your own <tt>readLock</tt> option or similar. Can also be used when special conditions must be met before a file can be consumed, such as a special <em>ready</em> file exists. If this option is set then the <tt>readLock</tt> option does not apply. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>startingDirectoryMustExist</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.5:</b> Whether the starting directory must exist. Mind that the <tt>autoCreate</tt> option is default enabled, which means the starting directory is normally auto created if it doesn't exist. You can disable <tt>autoCreate</tt> and enable this to ensure the starting directory must exist. Will thrown an exception if the directory doesn't exist. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>pollStrategy</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.0:</b> A pluggable <tt>org.apache.camel.PollingConsumerPollStrategy</tt> allowing you to provide your custom implementation to control error handling usually occurred during the <tt>poll</tt> operation <b>before</b> an <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> have been created and being routed in Camel. In other words the error occurred while the polling was gathering information, for instance access to a file network failed so Camel cannot access it to scan for files. The default implementation will log the caused exception at <tt>WARN</tt> level and ignore it. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>sendEmptyMessageWhenIdle</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.9:</b> If the polling consumer did not poll any files, you can enable this option to send an empty message (no body) instead. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>consumer.bridgeErrorHandler</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.10:</b> Allows to bridge the consumer to the Camel routing <a href="/confluence/display/CAMEL/Error+Handler" title="Error Handler">Error Handler</a>, which mean any exceptions occurred while trying to pickup files, or the likes, will now be processed as a message and handled by the routing <a href="/confluence/display/CAMEL/Error+Handler" title="Error Handler">Error Handler</a>. By default the consumer will use the <tt>org.apache.camel.spi.ExceptionHandler</tt> to deal with exceptions, that by default will be logged at WARN/ERROR level and ignored. See further below on this page fore more details, at section <em>How to use the Camel error handler to deal with exceptions triggered outside the routing engine</em>. </td>
</tr>
</tbody></table>
</div>
</div>

<h4><a name="File2-Defaultbehaviorforfileconsumer"></a>Default behavior for file consumer</h4>
<ul>
	<li>By default the file is locked for the duration of the processing.</li>
	<li>After the route has completed, files are moved into the <tt>.camel</tt> subdirectory, so that they appear to be deleted.</li>
	<li>The File Consumer will always skip any file whose name starts with a dot, such as <tt>.</tt>, <tt>.camel</tt>, <tt>.m2</tt> or <tt>.groovy</tt>.</li>
	<li>Only files (not directories) are matched for valid filename, if options such as: <tt>include</tt> or <tt>exclude</tt> are used.</li>
</ul>


<h4><a name="File2-Producer"></a>Producer</h4>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>fileExist</tt> </td>
<td class='confluenceTd'> <tt>Override</tt> </td>
<td class='confluenceTd'> What to do if a file already exists with the same name. The following values can be specified: <b>Override</b>, <b>Append</b>, <b>Fail</b> and <b>Ignore</b>. <tt>Override</tt>, which is the default, replaces the existing file. <tt>Append</tt> adds content to the existing file. <tt>Fail</tt> throws a <tt>GenericFileOperationException</tt>, indicating that there is already an existing file. <tt>Ignore</tt> silently ignores the problem and <b>does not</b> override the existing file, but assumes everything is okay. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>tempPrefix</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> This option is used to write the file using a temporary name and then, after the write is complete, rename it to the real name. Can be used to identify files being written and also avoid consumers (not using exclusive read locks) reading in progress files. Is often used by <a href="/confluence/display/CAMEL/FTP2" title="FTP2">FTP</a> when uploading big files. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>tempFileName</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.1:</b> The <b>same</b> as <tt>tempPrefix</tt> option but offering a more fine grained control on the naming of the temporary filename as it uses the <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>keepLastModified</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.2:</b> Will keep the last modified timestamp from the source file (if any). Will use the <tt>Exchange.FILE_LAST_MODIFIED</tt> header to located the timestamp. This header can contain either a <tt>java.util.Date</tt> or <tt>long</tt> with the timestamp. If the timestamp exists and the option is enabled it will set this timestamp on the written file. <b>Note:</b> This option only applies to the <b>file</b> producer. You <em>cannot</em> use this option with any of the ftp producers. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>eagerDeleteTargetFile</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <b>Camel 2.3:</b> Whether or not to eagerly delete any existing target file. This option only applies when you use <tt>fileExists=Override</tt> and the <tt>tempFileName</tt> option as well. You can use this to disable (set it to false) deleting the target file before the temp file is written. For example you may write big files and want the target file to exists during the temp file is being written. This ensure the target file is only deleted until the very last moment, just before the temp file is being renamed to the target filename. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>doneFileName</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <b>Camel 2.6:</b> If provided, then Camel will write a 2nd <em>done</em> file when the original file has been written. The <em>done</em> file will be empty. This option configures what file name to use. Either you can specify a fixed name. Or you can use dynamic placeholders. The <em>done</em> file will <b>always</b> be written in the same folder as the original file. See <em>writing done file</em> section for examples. </td>
</tr>
</tbody></table>
</div>
</div>


<h4><a name="File2-Defaultbehaviorforfileproducer"></a>Default behavior for file producer</h4>
<ul>
	<li>By default it will override any existing file, if one exist with the same name.</li>
</ul>


<h3><a name="File2-MoveandDeleteoperations"></a>Move and Delete operations</h3>
<p>Any move or delete operations is executed after (post command) the routing has completed; so during processing of the <tt>Exchange</tt> the file is still located in the inbox folder. </p>

<p>Lets illustrate this with an example:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    from(<span class="code-quote">"file:<span class="code-comment">//inbox?move=.done"</span>).to(<span class="code-quote">"bean:handleOrder"</span>);</span>
</pre>
</div></div>
<p>When a file is dropped in the <tt>inbox</tt> folder, the file consumer notices this and creates a new <tt>FileExchange</tt> that is routed to the <tt>handleOrder</tt> bean. The bean then processes the <tt>File</tt> object. At this point in time the file is still located in the <tt>inbox</tt> folder. After the bean completes, and thus the route is completed, the file consumer will perform the move operation and move the file to the <tt>.done</tt> sub-folder.</p>

<p>The <b>move</b> and <b>preMove</b> options should be a directory name, which can be either relative or absolute. If relative, the directory is created as a sub-folder from within the folder where the file was consumed. </p>

<p>By default, Camel will move consumed files to the <tt>.camel</tt> sub-folder relative to the directory where the file was consumed.</p>

<p>If you want to delete the file after processing, the route should be:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    from(<span class="code-quote">"file:<span class="code-comment">//inobox?delete=<span class="code-keyword">true</span>"</span>).to(<span class="code-quote">"bean:handleOrder"</span>);</span>
</pre>
</div></div>

<p>We have introduced a <b>pre</b> move operation to move files <b>before</b> they are processed. This allows you to mark which files have been scanned as they are moved to this sub folder before being processed.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    from(<span class="code-quote">"file:<span class="code-comment">//inbox?preMove=inprogress"</span>).to(<span class="code-quote">"bean:handleOrder"</span>);</span>
</pre>
</div></div>

<p>You can combine the <b>pre</b> move and the regular move:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    from(<span class="code-quote">"file:<span class="code-comment">//inbox?preMove=inprogress&amp;move=.done"</span>).to(<span class="code-quote">"bean:handleOrder"</span>);</span>
</pre>
</div></div>
<p>So in this situation, the file is in the <tt>inprogress</tt> folder when being processed and after it's processed, it's moved to the <tt>.done</tt> folder.</p>

<h4><a name="File2-FinegrainedcontroloverMoveandPreMoveoption"></a>Fine grained control over Move and PreMove option</h4>
<p>The <b>move</b> and <b>preMove</b> option is <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a>-based, so we have the full power of the <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a> to do advanced configuration of the directory and name pattern.<br/>
Camel will, in fact, internally convert the directory name you enter into a <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a> expression. So when we enter <tt>move=.done</tt> Camel will convert this into: <tt>${<a href="file:parent" class="external-link" rel="nofollow">file:parent</a>}/.done/${<a href="file:onlyname" class="external-link" rel="nofollow">file:onlyname</a></tt>}. This is only done if Camel detects that you have not provided a ${ } in the option value yourself. So when you enter a ${ } Camel will <b>not</b> convert it and thus you have the full power.</p>

<p>So if we want to move the file into a backup folder with today's date as the pattern, we can do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
move=backup/${date:now:yyyyMMdd}/${file:name}
</pre>
</div></div> 

<h4><a name="File2-AboutmoveFailed"></a>About moveFailed</h4>
<p>The <tt>moveFailed</tt> option allows you to move files that <b>could not</b> be processed succesfully to another location such as a error folder of your choice. For example to move the files in an error folder with a timestamp you can use <tt>moveFailed=/error/${<a href="file:name.noext" class="external-link" rel="nofollow">file:name.noext</a>}-${date:now:yyyyMMddHHmmssSSS}.${<a href="file:ext" class="external-link" rel="nofollow">file:ext</a></tt>}.</p>

<p>See more examples at <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a></p>


<h3><a name="File2-MessageHeaders"></a>Message Headers</h3>

<p>The following headers are supported by this component:</p>

<h4><a name="File2-Fileproduceronly"></a>File producer only</h4>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Header </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelFileName</tt> </td>
<td class='confluenceTd'> Specifies the name of the file to write (relative to the endpoint directory). The name can be a <tt>String</tt>; a <tt>String</tt> with a <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a> or <a href="/confluence/display/CAMEL/Simple" title="Simple">Simple</a> expression; or an <a href="/confluence/display/CAMEL/Expression" title="Expression">Expression</a> object. If it's <tt>null</tt> then Camel will auto-generate a filename based on the message unique ID.  </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelFileNameProduced</tt> </td>
<td class='confluenceTd'> The actual absolute filepath (path + name) for the output file that was written. This header is set by Camel and its purpose is providing end-users with the name of the file that was written. </td>
</tr>
</tbody></table>
</div>
</div>

<h4><a name="File2-Fileconsumeronly"></a>File consumer only</h4>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Header </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelFileName</tt> </td>
<td class='confluenceTd'> Name of the consumed file as a relative file path with offset from the starting directory configured on the endpoint. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelFileNameOnly</tt> </td>
<td class='confluenceTd'> Only the file name (the name with no leading paths). </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelFileAbsolute</tt> </td>
<td class='confluenceTd'> A <tt>boolean</tt> option specifying whether the consumed file denotes an absolute path or not. Should normally be <tt>false</tt> for relative paths. Absolute paths should normally not be used but we added to the move option to allow moving files to absolute paths. But can be used elsewhere as well. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelFileAbsolutePath</tt> </td>
<td class='confluenceTd'> The absolute path to the file. For relative files this path holds the relative path instead. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelFilePath</tt> </td>
<td class='confluenceTd'> The file path. For relative files this is the starting directory + the relative filename. For absolute files this is the absolute path. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelFileRelativePath</tt> </td>
<td class='confluenceTd'> The relative path. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelFileParent</tt> </td>
<td class='confluenceTd'> The parent path. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelFileLength</tt> </td>
<td class='confluenceTd'> A <tt>long</tt> value containing the file size. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelFileLastModified</tt> </td>
<td class='confluenceTd'> A <tt>Date</tt> value containing the last modified timestamp of the file. </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="File2-BatchConsumer"></a>Batch Consumer</h3>
<p>This component implements the <a href="/confluence/display/CAMEL/Batch+Consumer" title="Batch Consumer">Batch Consumer</a>. </p>

<h4><a name="File2-ExchangeProperties%2Cfileconsumeronly"></a>Exchange Properties, file consumer only</h4>
<p>As the file consumer is <tt>BatchConsumer</tt> it supports batching the files it polls. By batching it means that Camel will add some properties to the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> so you know the number of files polled the current index in that order.</p>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Property </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelBatchSize</tt> </td>
<td class='confluenceTd'> The total number of files that was polled in this batch. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelBatchIndex</tt> </td>
<td class='confluenceTd'> The current index of the batch. Starts from 0. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>CamelBatchComplete</tt> </td>
<td class='confluenceTd'> A <tt>boolean</tt> value indicating the last <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> in the batch. Is only <tt>true</tt> for the last entry. </td>
</tr>
</tbody></table>
</div>
</div>

<p>This allows you for instance to know how many files exists in this batch and for instance let the <a href="/confluence/display/CAMEL/Aggregator2" title="Aggregator2">Aggregator2</a> aggregate this number of files.</p>

<h3><a name="File2-Usingcharset"></a>Using charset</h3>
<p><b>Available as of Camel 2.9.3</b> <br/>
The charset option allows to configure an encoding of the files on both the consumer and producer endpoints. For example if you read utf-8 files, and want to convert the files to iso-8859-1, you can do:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:inbox?charset=utf-8"</span>)
  .to(<span class="code-quote">"file:outbox?charset=iso-8859-1"</span>)
</pre>
</div></div>

<p>You can also use the <tt>convertBodyTo</tt> in the route. In the example below we have still input files in utf-8 format, but we want to convert the file content to a byte array in iso-8859-1 format. And then let a bean process the data. Before writing the content to the outbox folder using the current charset.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:inbox?charset=utf-8"</span>)
  .convertBodyTo(<span class="code-object">byte</span>[].class, <span class="code-quote">"iso-8859-1"</span>)
  .to(<span class="code-quote">"bean:myBean"</span>)
  .to(<span class="code-quote">"file:outbox"</span>);
</pre>
</div></div>

<p>If you omit the charset on the consumer endpoint, then Camel does not know the charset of the file, and would by default use "UTF-8". However you can configure a JVM system property to override and use a different default encoding with the key <tt>org.apache.camel.default.charset</tt>.</p>

<p>In the example below this could be a problem if the files is not in UTF-8 encoding, which would be the default encoding for read the files.<br/>
In this example when writing the files, the content has already been converted to a byte array, and thus would write the content directly as is (without any further encodings).</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:inbox"</span>)
  .convertBodyTo(<span class="code-object">byte</span>[].class, <span class="code-quote">"iso-8859-1"</span>)
  .to(<span class="code-quote">"bean:myBean"</span>)
  .to(<span class="code-quote">"file:outbox"</span>);
</pre>
</div></div>

<p>You can also override and control the encoding dynamic when writing files, by setting a property on the exchange with the key <tt>Exchange.CHARSET_NAME</tt>. For example in the route below we set the property with a value from a message header.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:inbox"</span>)
  .convertBodyTo(<span class="code-object">byte</span>[].class, <span class="code-quote">"iso-8859-1"</span>)
  .to(<span class="code-quote">"bean:myBean"</span>)
  .setProperty(Exchange.CHARSET_NAME, header(<span class="code-quote">"someCharsetHeader"</span>))
  .to(<span class="code-quote">"file:outbox"</span>);
</pre>
</div></div>

<p>We suggest to keep things simpler, so if you pickup files with the same encoding, and want to write the files in a specific encoding, then favor to use the <tt>charset</tt> option on the endpoints.</p>

<p>Notice that if you have explicit configured a <tt>charset</tt> option on the endpoint, then that configuration is used, regardless of the <tt>Exchange.CHARSET_NAME</tt> property.</p>

<p>If you have some issues then you can enable DEBUG logging on <tt>org.apache.camel.component.file</tt>, and Camel logs when it reads/write a file using a specific charset.<br/>
For example the route below will log the following:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:inbox?charset=utf-8"</span>)
  .to(<span class="code-quote">"file:outbox?charset=iso-8859-1"</span>)
</pre>
</div></div>
<p>And the logs:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
DEBUG GenericFileConverter           - Read file /Users/davsclaus/workspace/camel/camel-core/target/charset/input/input.txt with charset utf-8
DEBUG FileOperations                 - Using Reader to write file: target/charset/output.txt with charset: iso-8859-1
</pre>
</div></div>

<h3><a name="File2-Commongotchaswithfolderandfilenames"></a>Common gotchas with folder and filenames</h3>

<p>When Camel is producing files (writing files) there are a few gotchas affecting how to set a filename of your choice. By default, Camel will use the message ID as the filename, and since the message ID is normally a unique generated ID, you will end up with filenames such as: <tt>ID-MACHINENAME-2443-1211718892437-1-0</tt>. If such a filename is not desired, then you must provide a filename in the <tt>CamelFileName</tt> message header. The constant, <tt>Exchange.FILE_NAME</tt>, can also be used. </p>

<p>The sample code below produces files using the message ID as the filename:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:report"</span>).to(<span class="code-quote">"file:target/reports"</span>);
</pre>
</div></div>
<p>To use <tt>report.txt</tt> as the filename you have to do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:report"</span>).setHeader(Exchange.FILE_NAME, constant(<span class="code-quote">"report.txt"</span>)).to( <span class="code-quote">"file:target/reports"</span>);
</pre>
</div></div>
<p>... the same as above, but with <tt>CamelFileName</tt>:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:report"</span>).setHeader(<span class="code-quote">"CamelFileName"</span>, constant(<span class="code-quote">"report.txt"</span>)).to( <span class="code-quote">"file:target/reports"</span>);
</pre>
</div></div>
<p>And a syntax where we set the filename on the endpoint with the <b>fileName</b> URI option.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:report"</span>).to(<span class="code-quote">"file:target/reports/?fileName=report.txt"</span>);
</pre>
</div></div>

<h3><a name="File2-FilenameExpression"></a>Filename Expression</h3>
<p>Filename can be set either using the <b>expression</b> option or as a string-based <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a> expression in the <tt>CamelFileName</tt> header. See the <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a> for syntax and samples.</p>

<h3><a name="File2-Consumingfilesfromfolderswhereothersdropfilesdirectly"></a>Consuming files from folders where others drop files directly</h3>
<p>Beware if you consume files from a folder where other applications write files directly. Take a look at the different <tt>readLock</tt> options to see what suits your use cases. The best approach is however to write to another folder and after the write move the file in the drop folder. However if you write files directly to the drop folder then the option <tt>changed</tt> could better detect whether a file is currently being written/copied as it uses a file changed algorithm to see whether the file size / modification changes over a period of time. The other read lock options rely on Java File API that sadly is not always very good at detecting this. You may also want to look at the <tt>doneFileName</tt> option, which uses a marker file (done) to signal when a file is done and ready to be consumed.</p>


<h3><a name="File2-Usingdonefiles"></a>Using done files</h3>
<p><b>Available as of Camel 2.6</b></p>

<p>See also section <em>writing done files</em> below.</p>

<p>If you want only to consume files when a done file exists, then you can use the <tt>doneFileName</tt> option on the endpoint. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:bar?doneFileName=done"</span>);
</pre>
</div></div>
<p>Will only consume files from the bar folder, if a file name done exists in the same directory as the target files. Camel will automatically delete the done file when it's done consuming the files. From Camel <b>2.9.3</b> onwards Camel will not automatic delete the done file if <tt>noop=true</tt> is configured.</p>

<p>However its more common to have one done file per target file. This means there is a 1:1 correlation. To do this you must use dynamic placeholders in the <tt>doneFileName</tt> option. Currently Camel supports the following two dynamic tokens: <tt><a href="file:name" class="external-link" rel="nofollow">file:name</a></tt> and <tt><a href="file:name.noext" class="external-link" rel="nofollow">file:name.noext</a></tt> which must be enclosed in ${ }. The consumer only supports the static part of the done file name as either prefix or suffix (not both).</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:bar?doneFileName=${file:name}.done"</span>);
</pre>
</div></div>

<p>In this example only files will be polled if there exists a done file with the name <em>file name</em>.done. For example</p>
<ul class="alternate" type="square">
	<li><tt>hello.txt</tt> - is the file to be consumed</li>
	<li><tt>hello.txt.done</tt> - is the associated done file</li>
</ul>


<p>You can also use a prefix for the done file, such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:bar?doneFileName=ready-${file:name}"</span>);
</pre>
</div></div>
<ul class="alternate" type="square">
	<li><tt>hello.txt</tt> - is the file to be consumed</li>
	<li><tt>ready-hello.txt</tt> - is the associated done file</li>
</ul>




<h3><a name="File2-Writingdonefiles"></a>Writing done files</h3>
<p><b>Available as of Camel 2.6</b></p>

<p>After you have written af file you may want to write an additional <em>done</em> file as a kinda of marker, to indicate to others that the file is finished and has been written. To do that you can use the <tt>doneFileName</tt> option on the file producer endpoint. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
.to(<span class="code-quote">"file:bar?doneFileName=done"</span>);
</pre>
</div></div>
<p>Will simply create a file named <tt>done</tt> in the same directory as the target file.</p>

<p>However its more common to have one done file per target file. This means there is a 1:1 correlation. To do this you must use dynamic placeholders in the <tt>doneFileName</tt> option. Currently Camel supports the following two dynamic tokens: <tt><a href="file:name" class="external-link" rel="nofollow">file:name</a></tt> and <tt><a href="file:name.noext" class="external-link" rel="nofollow">file:name.noext</a></tt> which must be enclosed in ${ }. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
.to(<span class="code-quote">"file:bar?doneFileName=done-${file:name}"</span>);
</pre>
</div></div>
<p>Will for example create a file named <tt>done-foo.txt</tt> if the target file was <tt>foo.txt</tt> in the same directory as the target file.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
.to(<span class="code-quote">"file:bar?doneFileName=${file:name}.done"</span>);
</pre>
</div></div>
<p>Will for example create a file named <tt>foo.txt.done</tt> if the target file was <tt>foo.txt</tt> in the same directory as the target file.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
.to(<span class="code-quote">"file:bar?doneFileName=${file:name.noext}.done"</span>);
</pre>
</div></div>
<p>Will for example create a file named <tt>foo.done</tt> if the target file was <tt>foo.txt</tt> in the same directory as the target file.</p>

<h3><a name="File2-Samples"></a>Samples</h3>

<h4><a name="File2-Readfromadirectoryandwritetoanotherdirectory"></a>Read from a directory and write to another directory</h4>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:<span class="code-comment">//inputdir/?delete=<span class="code-keyword">true</span>"</span>).to(<span class="code-quote">"file://outputdir"</span>)</span>
</pre>
</div></div>

<p>Listen on a directory and create a message for each file dropped there. Copy the contents to the <tt>outputdir</tt> and delete the file in the <tt>inputdir</tt>.</p>

<h4><a name="File2-Readingrecursivelyfromadirectoryandwritingtoanother"></a>Reading recursively from a directory and writing to another</h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:<span class="code-comment">//inputdir/?recursive=<span class="code-keyword">true</span>&amp;delete=<span class="code-keyword">true</span>"</span>).to(<span class="code-quote">"file://outputdir"</span>)</span>
</pre>
</div></div>

<p>Listen on a directory and create a message for each file dropped there. Copy the contents to the <tt>outputdir</tt> and delete the file in the <tt>inputdir</tt>. Will scan recursively into sub-directories. Will lay out the files in the same directory structure in the <tt>outputdir</tt> as the <tt>inputdir</tt>, including any sub-directories.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
inputdir/foo.txt
inputdir/sub/bar.txt
</pre>
</div></div>
<p>Will result in the following output layout:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
outputdir/foo.txt
outputdir/sub/bar.txt
</pre>
</div></div>

<h5><a name="File2-Usingflatten"></a>Using flatten</h5>
<p>If you want to store the files in the outputdir directory in the same directory, disregarding the source directory layout (e.g. to flatten out the path), you just add the <tt>flatten=true</tt> option on the file producer side:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:<span class="code-comment">//inputdir/?recursive=<span class="code-keyword">true</span>&amp;delete=<span class="code-keyword">true</span>"</span>).to(<span class="code-quote">"file://outputdir?flatten=<span class="code-keyword">true</span>"</span>)</span>
</pre>
</div></div>
<p>Will result in the following output layout:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
outputdir/foo.txt
outputdir/bar.txt
</pre>
</div></div>

<h4><a name="File2-Readingfromadirectoryandthedefaultmoveoperation"></a>Reading from a directory and the default move operation</h4>
<p>Camel will by default move any processed file into a <tt>.camel</tt> subdirectory in the directory the file was consumed from. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:<span class="code-comment">//inputdir/?recursive=<span class="code-keyword">true</span>&amp;delete=<span class="code-keyword">true</span>"</span>).to(<span class="code-quote">"file://outputdir"</span>)</span>
</pre>
</div></div>
<p>Affects the layout as follows:<br/>
<b>before</b></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
inputdir/foo.txt
inputdir/sub/bar.txt
</pre>
</div></div>

<p><b>after</b></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
inputdir/.camel/foo.txt
inputdir/sub/.camel/bar.txt
outputdir/foo.txt
outputdir/sub/bar.txt
</pre>
</div></div>

<h4><a name="File2-Readfromadirectoryandprocessthemessageinjava"></a>Read from a directory and process the message in java </h4>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:<span class="code-comment">//inputdir/"</span>).process(<span class="code-keyword">new</span> Processor() {
</span>  <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
    <span class="code-object">Object</span> body = exchange.getIn().getBody();
    <span class="code-comment">// <span class="code-keyword">do</span> some business logic with the input body
</span>  }
});
</pre>
</div></div>

<p>The body will be a <tt>File</tt> object that points to the file that was just dropped into the <tt>inputdir</tt> directory.</p>

<h4><a name="File2-Writingtofiles"></a>Writing to files</h4>
<p>Camel is of course also able to write files, i.e. produce files. In the sample below we receive some reports on the SEDA queue that we process before they are written to a directory.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> void testToFile() <span class="code-keyword">throws</span> Exception {
    MockEndpoint mock = getMockEndpoint(<span class="code-quote">"mock:result"</span>);
    mock.expectedMessageCount(1);
    mock.expectedFileExists(<span class="code-quote">"target/test-reports/report.txt"</span>);

    template.sendBody(<span class="code-quote">"direct:reports"</span>, <span class="code-quote">"This is a great report"</span>);

    assertMockEndpointsSatisfied();
}

<span class="code-keyword">protected</span> JndiRegistry createRegistry() <span class="code-keyword">throws</span> Exception {
    <span class="code-comment">// bind our processor in the registry with the given id
</span>    JndiRegistry reg = <span class="code-keyword">super</span>.createRegistry();
    reg.bind(<span class="code-quote">"processReport"</span>, <span class="code-keyword">new</span> ProcessReport());
    <span class="code-keyword">return</span> reg;
}

<span class="code-keyword">protected</span> RouteBuilder createRouteBuilder() <span class="code-keyword">throws</span> Exception {
    <span class="code-keyword">return</span> <span class="code-keyword">new</span> RouteBuilder() {
        <span class="code-keyword">public</span> void configure() <span class="code-keyword">throws</span> Exception {
            <span class="code-comment">// the reports from the seda queue is processed by our processor
</span>            <span class="code-comment">// before they are written to files in the target/reports directory
</span>            from(<span class="code-quote">"direct:reports"</span>).processRef(<span class="code-quote">"processReport"</span>).to(<span class="code-quote">"file:<span class="code-comment">//target/test-reports"</span>, <span class="code-quote">"mock:result"</span>);
</span>        }
    };
}

<span class="code-keyword">private</span> <span class="code-keyword">static</span> class ProcessReport <span class="code-keyword">implements</span> Processor {

    <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
        <span class="code-object">String</span> body = exchange.getIn().getBody(<span class="code-object">String</span>.class);
        <span class="code-comment">// <span class="code-keyword">do</span> some business logic here
</span>
        <span class="code-comment">// set the output to the file
</span>        exchange.getOut().setBody(body);

        <span class="code-comment">// set the output filename using java code logic, notice that <span class="code-keyword">this</span> is done by setting
</span>        <span class="code-comment">// a special header property of the out exchange
</span>        exchange.getOut().setHeader(Exchange.FILE_NAME, <span class="code-quote">"report.txt"</span>);
    }

}
</pre>
</div></div>

<h4><a name="File2-Writetosubdirectoryusing%7B%7BExchange.FILENAME%7D%7D"></a>Write to subdirectory using <tt>Exchange.FILE_NAME</tt></h4>
<p>Using a single route, it is possible to write a file to any number of subdirectories. If you have a route setup as such:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
  &lt;route&gt;
    &lt;from uri=<span class="code-quote">"bean:myBean"</span>/&gt;
    &lt;to uri=<span class="code-quote">"file:/rootDirectory"</span>/&gt;
  &lt;/route&gt; 
</pre>
</div></div>

<p>You can have <tt>myBean</tt> set the header <tt>Exchange.FILE_NAME</tt> to values such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Exchange.FILE_NAME = hello.txt =&gt; /rootDirectory/hello.txt
Exchange.FILE_NAME = foo/bye.txt =&gt; /rootDirectory/foo/bye.txt 
</pre>
</div></div>

<p>This allows you to have a single route to write files to multiple destinations.</p>

<h4><a name="File2-Usingexpressionforfilenames"></a>Using expression for filenames</h4>

<p>In this sample we want to move consumed files to a backup folder using today's date as a sub-folder name:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:<span class="code-comment">//inbox?move=backup/${date:now:yyyyMMdd}/${file:name}"</span>).to(<span class="code-quote">"..."</span>);</span>
</pre>
</div></div>

<p>See <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a> for more samples.</p>

<h3><a name="File2-Avoidingreadingthesamefilemorethanonce%28idempotentconsumer%29"></a>Avoiding reading the same file more than once (idempotent consumer)</h3>
<p>Camel supports <a href="/confluence/display/CAMEL/Idempotent+Consumer" title="Idempotent Consumer">Idempotent Consumer</a> directly within the component so it will skip already processed files. This feature can be enabled by setting the <tt>idempotent=true</tt> option.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"file:<span class="code-comment">//inbox?idempotent=<span class="code-keyword">true</span>"</span>).to(<span class="code-quote">"..."</span>);</span>
</pre>
</div></div>

<p>By default Camel uses a in memory based store for keeping track of consumed files, it uses a least recently used cache holding up to 1000 entries. You can plugin your own implementation of this store by using the <tt>idempotentRepository</tt> option using the <tt>&#35;</tt> sign in the value to indicate it's a referring to a bean in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a> with the specified <tt>id</tt>.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
   <span class="code-tag"><span class="code-comment">&lt;!-- define our store as a plain spring bean --&gt;</span></span>
   <span class="code-tag">&lt;bean id=<span class="code-quote">"myStore"</span> class=<span class="code-quote">"com.mycompany.MyIdempotentStore"</span>/&gt;</span>

  <span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"file://inbox?idempotent=true&amp;amp;idempotentRepository=#myStore"</span>/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"bean:processInbox"</span>/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<p>Camel will log at <tt>DEBUG</tt> level if it skips a file because it has been consumed before:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
DEBUG FileConsumer is idempotent and the file has been consumed before. Will skip <span class="code-keyword">this</span> file: target\idempotent\report.txt
</pre>
</div></div>


<h4><a name="File2-Usingafilebasedidempotentrepository"></a>Using a file based idempotent repository</h4>
<p>In this section we will use the file based idempotent repository <tt>org.apache.camel.processor.idempotent.FileIdempotentRepository</tt> instead of the in-memory based that is used as default. <br/>
This repository uses a 1st level cache to avoid reading the file repository. It will only use the file repository to store the content of the 1st level cache. Thereby the repository can survive server restarts. It will load the content of the file into the 1st level cache upon startup. The file structure is very simple as it stores the key in separate lines in the file. By default, the file store has a size limit of 1mb. When the file grows larger Camel will truncate the file store, rebuilding the content by flushing the 1st level cache into a fresh empty file.</p>

<p>We configure our repository using Spring XML creating our file idempotent repository and define our file consumer to use our repository with the <tt>idempotentRepository</tt> using <tt>&#35;</tt> sign to indicate <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a> lookup:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag"><span class="code-comment">&lt;!-- this is our file based idempotent store configured to use the .filestore.dat as file --&gt;</span></span>
<span class="code-tag">&lt;bean id=<span class="code-quote">"fileStore"</span> class=<span class="code-quote">"org.apache.camel.processor.idempotent.FileIdempotentRepository"</span>&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- the filename for the store --&gt;</span></span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"fileStore"</span> value=<span class="code-quote">"target/fileidempotent/.filestore.dat"</span>/&gt;</span>
    &lt;!-- the max filesize in bytes for the file. Camel will trunk and flush the cache
         if the file gets bigger --&gt;
    <span class="code-tag">&lt;property name=<span class="code-quote">"maxFileStoreSize"</span> value=<span class="code-quote">"512000"</span>/&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- the number of elements in our store --&gt;</span></span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"cacheSize"</span> value=<span class="code-quote">"250"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"file://target/fileidempotent/?idempotent=true&amp;amp;idempotentRepository=#fileStore&amp;amp;move=done/${file:name}"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>


<h4><a name="File2-UsingaJPAbasedidempotentrepository"></a>Using a JPA based idempotent repository</h4>
<p>In this section we will use the JPA based idempotent repository instead of the in-memory based that is used as default. </p>

<p>First we need a persistence-unit in <tt>META-INF/persistence.xml</tt> where we need to use the class <tt>org.apache.camel.processor.idempotent.jpa.MessageProcessed</tt> as model.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;persistence-unit name=<span class="code-quote">"idempotentDb"</span> transaction-type=<span class="code-quote">"RESOURCE_LOCAL"</span>&gt;</span>
  <span class="code-tag">&lt;class&gt;</span>org.apache.camel.processor.idempotent.jpa.MessageProcessed<span class="code-tag">&lt;/class&gt;</span>

  <span class="code-tag">&lt;properties&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"openjpa.ConnectionURL"</span> value=<span class="code-quote">"jdbc:derby:target/idempotentTest;create=true"</span>/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"openjpa.ConnectionDriverName"</span> value=<span class="code-quote">"org.apache.derby.jdbc.EmbeddedDriver"</span>/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"openjpa.jdbc.SynchronizeMappings"</span> value=<span class="code-quote">"buildSchema"</span>/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"openjpa.Log"</span> value=<span class="code-quote">"DefaultLevel=WARN, Tool=INFO"</span>/&gt;</span>
  <span class="code-tag">&lt;/properties&gt;</span>
<span class="code-tag">&lt;/persistence-unit&gt;</span>
</pre>
</div></div>

<p>Then we need to setup a Spring <tt>jpaTemplate</tt> in the spring XML file:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag"><span class="code-comment">&lt;!-- this is standard spring JPA configuration --&gt;</span></span>
<span class="code-tag">&lt;bean id=<span class="code-quote">"jpaTemplate"</span> class=<span class="code-quote">"org.springframework.orm.jpa.JpaTemplate"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"entityManagerFactory"</span> ref=<span class="code-quote">"entityManagerFactory"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"entityManagerFactory"</span> class=<span class="code-quote">"org.springframework.orm.jpa.LocalEntityManagerFactoryBean"</span>&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- we use idempotentDB as the persitence unit name defined in the persistence.xml file --&gt;</span></span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"persistenceUnitName"</span> value=<span class="code-quote">"idempotentDb"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>And finally we can create our JPA idempotent repository in the spring XML file as well:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag"><span class="code-comment">&lt;!-- we define our jpa based idempotent repository we want to use in the file consumer --&gt;</span></span>
<span class="code-tag">&lt;bean id=<span class="code-quote">"jpaStore"</span> class=<span class="code-quote">"org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository"</span>&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- Here we refer to the spring jpaTemplate --&gt;</span></span>
    <span class="code-tag">&lt;constructor-arg index=<span class="code-quote">"0"</span> ref=<span class="code-quote">"jpaTemplate"</span>/&gt;</span>
    &lt;!-- This 2nd parameter is the name  (= a cateogry name).
         You can have different repositories with different names --&gt;
    <span class="code-tag">&lt;constructor-arg index=<span class="code-quote">"1"</span> value=<span class="code-quote">"FileConsumer"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>And yes then we just need to refer to the <b>jpaStore</b> bean in the file consumer endpoint using the <tt>idempotentRepository</tt> using the <tt>&#35;</tt> syntax option:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
  <span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"file://inbox?idempotent=true&amp;amp;idempotentRepository=#jpaStore"</span>/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"bean:processInbox"</span>/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<h3><a name="File2-Filterusingorg.apache.camel.component.file.GenericFileFilter"></a>Filter using org.apache.camel.component.file.GenericFileFilter</h3>
<p>Camel supports pluggable filtering strategies. You can then configure the endpoint with such a filter to skip certain files being processed.</p>

<p>In the sample we have built our own filter that skips files starting with <tt>skip</tt> in the filename:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class MyFileFilter&lt;T&gt; <span class="code-keyword">implements</span> GenericFileFilter&lt;T&gt; {
    <span class="code-keyword">public</span> <span class="code-object">boolean</span> accept(GenericFile&lt;T&gt; file) {
        <span class="code-comment">// we want all directories
</span>        <span class="code-keyword">if</span> (file.isDirectory()) {
            <span class="code-keyword">return</span> <span class="code-keyword">true</span>;
        }
        <span class="code-comment">// we dont accept any files starting with skip in the name
</span>        <span class="code-keyword">return</span> !file.getFileName().startsWith(<span class="code-quote">"skip"</span>);
    }
}
</pre>
</div></div>

<p>And then we can configure our route using the <b>filter</b> attribute to reference our filter (using <tt>&#35;</tt> notation) that we have defined in the spring XML file:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
   <span class="code-tag"><span class="code-comment">&lt;!-- define our sorter as a plain spring bean --&gt;</span></span>
   <span class="code-tag">&lt;bean id=<span class="code-quote">"myFilter"</span> class=<span class="code-quote">"com.mycompany.MyFileSorter"</span>/&gt;</span>

  <span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"file://inbox?filter=#myFilter"</span>/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"bean:processInbox"</span>/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<h4><a name="File2-FilteringusingANTpathmatcher"></a>Filtering using ANT path matcher</h4>
<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>New options from Camel 2.10 onwards</b><br />There are now <tt>antInclude</tt> and <tt>antExclude</tt> options to make it easy to specify ANT style include/exclude without having to define the filter. See the URI options above for more information.</td></tr></table></div>

<p>The ANT path matcher is shipped out-of-the-box in the <b>camel-spring</b> jar. So you need to depend on <b>camel-spring</b> if you are using Maven.<br/>
The reasons is that we leverage Spring's <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/util/AntPathMatcher.html" class="external-link" rel="nofollow">AntPathMatcher</a> to do the actual matching.</p>

<p>The file paths is matched with the following rules:</p>
<ul class="alternate" type="square">
	<li><tt>?</tt> matches one character</li>
	<li><tt>*</tt> matches zero or more characters</li>
	<li><tt>**</tt> matches zero or more directories in a path</li>
</ul>


<p>The sample below demonstrates how to use it:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
    <span class="code-tag">&lt;template id=<span class="code-quote">"camelTemplate"</span>/&gt;</span>

    <span class="code-tag"><span class="code-comment">&lt;!-- use myFilter as filter to allow setting ANT paths for which files to scan for --&gt;</span></span>
    <span class="code-tag">&lt;endpoint id=<span class="code-quote">"myFileEndpoint"</span> uri=<span class="code-quote">"file://target/antpathmatcher?recursive=true&amp;amp;filter=#myAntFilter"</span>/&gt;</span>

    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from ref=<span class="code-quote">"myFileEndpoint"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag"><span class="code-comment">&lt;!-- we use the antpath file filter to use ant paths for includes and exlucde --&gt;</span></span>
<span class="code-tag">&lt;bean id=<span class="code-quote">"myAntFilter"</span> class=<span class="code-quote">"org.apache.camel.component.file.AntPathMatcherGenericFileFilter"</span>&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- include and file in the subfolder that has day in the name --&gt;</span></span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"includes"</span> value=<span class="code-quote">"**/subfolder/**/*day*"</span>/&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- exclude all files with bad in name or .xml files. Use comma to seperate multiple excludes --&gt;</span></span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"excludes"</span> value=<span class="code-quote">"**/*bad*,**/*.xml"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<h3><a name="File2-SortingusingComparator"></a>Sorting using Comparator</h3>
<p>Camel supports pluggable sorting strategies. This strategy it to use the build in <tt>java.util.Comparator</tt> in Java. You can then configure the endpoint with such a comparator and have Camel sort the files before being processed.</p>

<p>In the sample we have built our own comparator that just sorts by file name:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class MyFileSorter&lt;T&gt; <span class="code-keyword">implements</span> Comparator&lt;GenericFile&lt;T&gt;&gt; {
    <span class="code-keyword">public</span> <span class="code-object">int</span> compare(GenericFile&lt;T&gt; o1, GenericFile&lt;T&gt; o2) {
        <span class="code-keyword">return</span> o1.getFileName().compareToIgnoreCase(o2.getFileName());
    }
}
</pre>
</div></div>

<p>And then we can configure our route using the <b>sorter</b> option to reference to our sorter (<tt>mySorter</tt>) we have defined in the spring XML file:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
   <span class="code-tag"><span class="code-comment">&lt;!-- define our sorter as a plain spring bean --&gt;</span></span>
   <span class="code-tag">&lt;bean id=<span class="code-quote">"mySorter"</span> class=<span class="code-quote">"com.mycompany.MyFileSorter"</span>/&gt;</span>

  <span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"file://inbox?sorter=#mySorter"</span>/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"bean:processInbox"</span>/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>URI options can reference beans using the # syntax</b><br />In the Spring DSL route about notice that we can refer to beans in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a> by prefixing the id with <tt>&#35;</tt>. So writing <tt>sorter=#mySorter</tt>, will instruct Camel to go look in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a> for a bean with the ID, <tt>mySorter</tt>.</td></tr></table></div>

<h3><a name="File2-SortingusingsortBy"></a>Sorting using sortBy</h3>
<p>Camel supports pluggable sorting strategies. This strategy it to use the <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a> to configure the sorting. The <tt>sortBy</tt> option is configured as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
sortBy=group 1;group 2;group 3;...
</pre>
</div></div>
<p>Where each group is separated with semi colon. In the simple situations you just use one group, so a simple example could be:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
sortBy=file:name
</pre>
</div></div>
<p>This will sort by file name, you can reverse the order by prefixing <tt>reverse:</tt> to the group, so the sorting is now Z..A:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
sortBy=reverse:file:name
</pre>
</div></div>
<p>As we have the full power of <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a> we can use some of the other parameters, so if we want to sort by file size we do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
sortBy=file:length
</pre>
</div></div>

<p>You can configure to ignore the case, using <tt>ignoreCase:</tt> for string comparison, so if you want to use file name sorting but to ignore the case then we do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
sortBy=ignoreCase:file:name
</pre>
</div></div>
<p>You can combine ignore case and reverse, however reverse must be specified first:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
sortBy=reverse:ignoreCase:file:name
</pre>
</div></div>

<p>In the sample below we want to sort by last modified file, so we do:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
sortBy=file:modifed
</pre>
</div></div>
<p>And then we want to group by name as a 2nd option so files with same modifcation is sorted by name:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
sortBy=file:modifed;file:name
</pre>
</div></div>
<p>Now there is an issue here, can you spot it? Well the modified timestamp of the file is too fine as it will be in milliseconds, but what if we want to sort by date only and then subgroup by name?<br/>
Well as we have the true power of <a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a> we can use the its date command that supports patterns. So this can be solved as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
sortBy=date:file:yyyyMMdd;file:name
</pre>
</div></div>
<p>Yeah, that is pretty powerful, oh by the way you can also use reverse per group, so we could reverse the file names:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
sortBy=date:file:yyyyMMdd;reverse:file:name
</pre>
</div></div>

<h3><a name="File2-UsingGenericFileProcessStrategy"></a>Using GenericFileProcessStrategy</h3>
<p>The option <tt>processStrategy</tt> can be used to use a custom <tt>GenericFileProcessStrategy</tt> that allows you to implement your own <em>begin</em>, <em>commit</em> and <em>rollback</em> logic.<br/>
For instance lets assume a system writes a file in a folder you should consume. But you should not start consuming the file before another <em>ready</em> file has been written as well.</p>

<p>So by implementing our own <tt>GenericFileProcessStrategy</tt> we can implement this as:</p>
<ul class="alternate" type="square">
	<li>In the <tt>begin()</tt> method we can test whether the special <em>ready</em> file exists. The begin method returns a <tt>boolean</tt> to indicate if we can consume the file or not.</li>
	<li>in the <tt>commit()</tt> method we can move the actual file and also delete the <em>ready</em> file.</li>
</ul>


<h3><a name="File2-Usingfilter"></a>Using filter</h3>
<p>The <tt>filter</tt> option allows you to implement a custom filter in Java code by implementing the <tt>org.apache.camel.component.file.GenericFileFilter</tt> interface. This interface has an <tt>accept</tt> method that returns a boolean. Return <tt>true</tt> to include the file, and <tt>false</tt> to skip the file. From Camel 2.10 onwards, there is a <tt>isDirectory</tt> method on <tt>GenericFile</tt> whether the file is a directory. This allows you to filter unwanted directories, to avoid traversing down unwanted directories.</p>

<p>For example to skip any directories which starts with <tt>"skip"</tt> in the name, can be implemented as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-keyword">public</span> class MyDirectoryFilter&lt;T&gt; <span class="code-keyword">implements</span> GenericFileFilter&lt;T&gt; {

    <span class="code-keyword">public</span> <span class="code-object">boolean</span> accept(GenericFile&lt;T&gt; file) {
        <span class="code-comment">// remember the name due unit testing (should not be needed in regular use-cases)
</span>        names.add(file.getFileName());
        
        <span class="code-comment">// we dont accept any files within directory starting with skip in the name
</span>        <span class="code-keyword">if</span> (file.isDirectory() &amp;&amp; file.getFileName().startsWith(<span class="code-quote">"skip"</span>)) {
            <span class="code-keyword">return</span> <span class="code-keyword">false</span>;
        }

        <span class="code-keyword">return</span> <span class="code-keyword">true</span>;
    }

}
</pre>
</div></div>

<h3><a name="File2-HowtousetheCamelerrorhandlertodealwithexceptionstriggeredoutsidetheroutingengine"></a>How to use the Camel error handler to deal with exceptions triggered outside the routing engine</h3>
<p>The file and ftp consumers, will by default try to pickup files. Only if that is successful then a Camel <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> can be created and passed in the Camel routing engine.<br/>
When the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> is processed by the routing engine, then the Camel <a href="/confluence/display/CAMEL/Error+handling+in+Camel" title="Error handling in Camel">Error Handling</a> takes over (eg the onException / errorHandler in the routes).<br/>
However outside the scope of the routing engine, any exceptions handling is component specific. Camel offers a <tt>org.apache.camel.spi.ExceptionHandler</tt> that allows components<br/>
to use that as a pluggable hook for end users to use their own implementation. Camel offers a default <tt>LoggingExceptionHandler</tt> that will log the exception at ERROR/WARN level.<br/>
For the file and ftp components this would be the case. However if you want to bridge the <tt>ExceptionHandler</tt> so it uses the Camel <a href="/confluence/display/CAMEL/Error+handling+in+Camel" title="Error handling in Camel">Error Handling</a>, then<br/>
you need to implement a custom <tt>ExceptionHandler</tt> that will handle the exception by creating a Camel <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> and send it to the routing engine; then the error handling of the routing engine can get triggered.</p>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>Easier with Camel 2.10</b><br />The new option consumer.bridgeErrorHandler can be set to true, to make this even easier. See further below</td></tr></table></div>

<p>Here is such an example based upon an unit test.</p>

<p>First we have a custom <tt>ExceptionHandler</tt> where you can see we deal with the exception by sending it to a Camel <a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a> named "direct:file-error":</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>MyExceptionHandler</b></div><div class="codeContent panelContent">
<pre class="code-java">/**
 * Custom {@link ExceptionHandler} to be used on the file consumer, to send
 * exceptions to a Camel route, to let Camel deal with the error.
 */
<span class="code-keyword">private</span> <span class="code-keyword">static</span> class MyExceptionHandler <span class="code-keyword">implements</span> ExceptionHandler {

    <span class="code-keyword">private</span> ProducerTemplate template;

    /**
     * We use a producer template to send a message to the Camel route
     */
    <span class="code-keyword">public</span> void setTemplate(ProducerTemplate template) {
        <span class="code-keyword">this</span>.template = template;
    }

    @Override
    <span class="code-keyword">public</span> void handleException(Throwable exception) {
        handleException(exception.getMessage(), exception);
    }

    @Override
    <span class="code-keyword">public</span> void handleException(<span class="code-object">String</span> message, Throwable exception) {
        handleException(exception.getMessage(), <span class="code-keyword">null</span>, exception);
    }

    @Override
    <span class="code-keyword">public</span> void handleException(<span class="code-keyword">final</span> <span class="code-object">String</span> message, <span class="code-keyword">final</span> Exchange originalExchange, <span class="code-keyword">final</span> Throwable exception) {
        <span class="code-comment">// send the message to the special direct:file-error endpoint, which will trigger exception handling
</span>        <span class="code-comment">//
</span>        template.send(<span class="code-quote">"direct:file-error"</span>, <span class="code-keyword">new</span> Processor() {
            @Override
            <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
                <span class="code-comment">// set an exception on the message from the start so the error handling is triggered
</span>                exchange.setException(exception);
                exchange.getIn().setBody(message);
            }
        });
    }
}
</pre>
</div></div>

<p>Then we have a Camel route that uses the Camel routing error handler, which is the <tt>onException</tt> where we handle any IOException being thrown.<br/>
We then send the message to the same "direct:file-error" endpoint, where we handle it by transforming it to a message, and then being sent to a <a href="/confluence/display/CAMEL/Mock" title="Mock">Mock</a> endpoint.<br/>
This is just for testing purpose. You can handle the exception in any custom way you want, such as using a <a href="/confluence/display/CAMEL/Bean" title="Bean">Bean</a> or sending an email etc.</p>

<p>Notice how we configure our custom <tt>MyExceptionHandler</tt> by using the <tt>consumer.exceptionHandler</tt> option to refer to <tt>#myExceptionHandler</tt> which is a id of the bean registered in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>. If using Spring XML or OSGi Blueprint, then that would be a &lt;bean id="myExceptionHandler" class="com.foo.MyExceptionHandler"/&gt;:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Camel route with routing engine error handling</b></div><div class="codeContent panelContent">
<pre class="code-java">@Override
<span class="code-keyword">protected</span> RouteBuilder createRouteBuilder() <span class="code-keyword">throws</span> Exception {
    <span class="code-keyword">return</span> <span class="code-keyword">new</span> RouteBuilder() {
        @Override
        <span class="code-keyword">public</span> void configure() <span class="code-keyword">throws</span> Exception {
            <span class="code-comment">// to handle any IOException being thrown
</span>            onException(IOException.class)
                .handled(<span class="code-keyword">true</span>)
                .log(<span class="code-quote">"IOException occurred due: ${exception.message}"</span>)
                <span class="code-comment">// as we handle the exception we can send it to direct:file-error,
</span>                <span class="code-comment">// where we could send out alerts or whatever we want
</span>                .to(<span class="code-quote">"direct:file-error"</span>);

            <span class="code-comment">// special route that handles file errors
</span>            from(<span class="code-quote">"direct:file-error"</span>)
                .log(<span class="code-quote">"File error route triggered to deal with exception ${exception?.class}"</span>)
                <span class="code-comment">// as <span class="code-keyword">this</span> is based on unit test just transform a message and send it to a mock
</span>                .transform().simple(<span class="code-quote">"Error ${exception.message}"</span>)
                .to(<span class="code-quote">"mock:error"</span>);

            <span class="code-comment">// <span class="code-keyword">this</span> is the file route that pickup files, notice how we use our custom exception handler on the consumer
</span>            <span class="code-comment">// the exclusiveReadLockStrategy is only configured because <span class="code-keyword">this</span> is from an unit test, so we use that to simulate exceptions
</span>            from(<span class="code-quote">"file:target/nospace?exclusiveReadLockStrategy=#myReadLockStrategy&amp;consumer.exceptionHandler=#myExceptionHandler"</span>)
                .convertBodyTo(<span class="code-object">String</span>.class)
                .to(<span class="code-quote">"mock:result"</span>);
        }
    };
}
</pre>
</div></div>

<p>The source code for this example can be seen <a href="https://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCustomExceptionHandlerTest.java" class="external-link" rel="nofollow">here</a></p>

<h4><a name="File2-Usingconsumer.bridgeErrorHandler"></a>Using consumer.bridgeErrorHandler</h4>
<p><b>Available as of Camel 2.10</b></p>

<p>If you want to use the Camel <a href="/confluence/display/CAMEL/Error+Handler" title="Error Handler">Error Handler</a> to deal with any exception occurring in the file consumer, then you can enable the <tt>consumer.bridgeErrorHandler</tt> option as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Using consumer.bridgeErrorHandler</b></div><div class="codeContent panelContent">
<pre class="code-java">@Override
<span class="code-keyword">protected</span> RouteBuilder createRouteBuilder() <span class="code-keyword">throws</span> Exception {
    <span class="code-keyword">return</span> <span class="code-keyword">new</span> RouteBuilder() {
        @Override
        <span class="code-keyword">public</span> void configure() <span class="code-keyword">throws</span> Exception {
            <span class="code-comment">// to handle any IOException being thrown
</span>            onException(IOException.class)
                .handled(<span class="code-keyword">true</span>)
                .log(<span class="code-quote">"IOException occurred due: ${exception.message}"</span>)
                .transform().simple(<span class="code-quote">"Error ${exception.message}"</span>)
                .to(<span class="code-quote">"mock:error"</span>);

            <span class="code-comment">// <span class="code-keyword">this</span> is the file route that pickup files, notice how we bridge the consumer to use the Camel routing error handler
</span>            <span class="code-comment">// the exclusiveReadLockStrategy is only configured because <span class="code-keyword">this</span> is from an unit test, so we use that to simulate exceptions
</span>            from(<span class="code-quote">"file:target/nospace?exclusiveReadLockStrategy=#myReadLockStrategy&amp;consumer.bridgeErrorHandler=<span class="code-keyword">true</span>"</span>)
                .convertBodyTo(<span class="code-object">String</span>.class)
                .to(<span class="code-quote">"mock:result"</span>);
        }
    };
}
</pre>
</div></div>

<p>So all you have to do is to enable this option, and the error handler in the route will take it from there.</p>

<h3><a name="File2-Debuglogging"></a>Debug logging</h3>
<p>This component has log level <b>TRACE</b> that can be helpful if you have problems.</p>

<h3><a name="File2-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting Started</a></li>
</ul>

<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/File+Language" title="File Language">File Language</a></li>
	<li><a href="/confluence/display/CAMEL/FTP2" title="FTP2">FTP</a></li>
	<li><a href="/confluence/display/CAMEL/Polling+Consumer" title="Polling Consumer">Polling Consumer</a></li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action" class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/File2">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=109456&revisedVersion=109&originalVersion=108">View Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/File2?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message