Camel Spring DSL Call a method on bean once all all files in a directory have been processed

2.6k views Asked by At

I am trying to achieve the following in Camel Spring DSL (Version 2.12.1).

  • Read all files from a directory
  • Process each file with an indexer bean
  • Call a finalizeRepository method on the indexer bean once all files have been processed

(Based on the answer supplied by Claus I have updated the code sample below with a working solution.)

<route>
    <from uri="file:./dataToIndex" />
    <bean ref="indexer" method="addDocument(${body}, ${headers.CamelFileNameOnly})" />
    <when>
        <simple>${headers.CamelBatchComplete} == true</simple>
    <bean ref="indexer" method="finalizeRepository" />
    </when>
</route>

If the header CamelBatchComplete holds the value true the method finalizeRepository on the indexer bean will be called. The header is set to true once all documents in the dataToIndex directory have been processed.

The only thing I am not quite certain about is the definition of batch. Is a batch determined based on what is found during a single polling event (e.g. if 1k files are found in a directory then this constitutes the batch), or a pattern of polling event (i.e. a series of successfull polls with items followed by a poll without any items is considered a batch).

1

There are 1 answers

0
Claus Ibsen On BEST ANSWER

The file component adds a header when its processing the last file in the batch. You can use that to know when its the last and call the bean.

This is done by other components which supports "batch" as you can find details here