Ah, whoops, my fault.  I was looking at the File documentation and not at
File2.

I think I have a working solution, but I'm wondering if I'm going about this
in the best way.

I set up my route with the filter:

        <bean id="bothFilesReceivedFilter"
class="com.mycompany.BothFilesReceivedFilter" />

        <camelContext xmlns="http://camel.apache.org/schema/spring";>
                <route id="report-file-consolidation">
                        <from
uri="file:///data/input?delete=true&amp;include=.*.txt&amp;maxMessagesPerPoll=10&amp;filter=#bothFilesReceivedFilter"
/>
                        <split streaming="true">
                                <tokenize includeTokens="true" token="\n" 
group="5000" />
                                <convertBodyTo type="String" charset="UTF-8" />
                                <setHeader headerName="CamelFileName">
                                        <constant>output-file.txt</constant>
                                </setHeader>
                <to uri="log:lineparser?showBody=true&amp;showHeaders=true"
/>
                                <to uri="file:///data/output?fileExist=Append" 
/>
                        </split>
                </route>
        </camelContext>

And I set my filter up like this:

public class BothFilesReceivedFilter<T> implements GenericFileFilter<T> {
        Logger log = Logger.getLogger();
        
        HashSet<String> namesReceived = new HashSet<String>();
        
        public boolean accept(GenericFile<T> file) {
                log.debug("Received file named: " + file.getFileName());
                
                namesReceived.add(file.getFileName());
                
                if (file.getFileName().equals("foo.txt")){
                        return namesReceived.contains("bar.txt");
                }
                else if (file.getFileName().equals("bar.txt")){
                        return namesReceived.contains("foo.txt");
                }
                else {
                        return false;
                }
        }
}

And this seems to work.  Is this sort of what you guys meant?  The only
thing I don't really like about this solution is that the file filtering
still seem to be getting evaluated separately from each other.  So with the
above solution, after both files have been received, if we receive another
input file with the same name, it gets processed immediately even when there
isn't the companion file in the input directory as well, because the HashSet
still shows that both files have been received.  But if I clear out the Set
once both files have been received, I think it will ignore the second file.

Since I was using maxMessagesPerPoll, I thought this would work as a batch
consumer, but I don't see the CamelBatchSize header populated when I print
the headers.  Is there any way to make the filtering inspect all the files
in the batch at once?

Am I off track here?

Thanks again.



--
View this message in context: 
http://camel.465427.n5.nabble.com/File-Exists-tp5746608p5746675.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to