ttt
ttt

Reputation: 4004

Camel check file's last modified date frequently using scheduling service

I want to use camel in my project to check a file's last modified date every xx minutes using camel's scheduling/timer service.

I read the document for file component it seems there is a polling function, however there is also a timer component for camel.

Anyone has some code example if i want to do with the requirement?

Upvotes: 2

Views: 2492

Answers (1)

Namphibian
Namphibian

Reputation: 12221

I would use the file consumer end point.

Something like this:

file:c:/foldername?delay=5000

This will scan the folder every 5 seconds for files and for each file send a message on the route.

You would probably need to store the previous times somewhere such as a text file or database and then compare the modified variable passed in the message to the modified one stored in the database or file.

A rough example of this would look like follows:

 <route id="CheckFileRoute">
    <from uri="file:d:/RMSInbox?delay=5000&readLock=changed/>
    <log message="${ file:modified }/>
    <bean ref="CompareDates"/>
  </route>

The file consumer will place a lot of information regarding the file in the header such as modified date. Go read this link for more details on the variables in the header http://camel.apache.org/file2.html

The compare dates bean would be java class that acts like a processor which would have a structure like this:

public class CompareDates {

@Handler
public void CheckDates
(
        @Body Object msgbody
        , @Headers Map hdr

) 
{

           Date newDate =  (Date)hdr.get("CamelFileLastModified");
           Date oldDate = readfromfileorDatabase
           if(newDate>oldDate)
           {
              //the date has changed look busy
           }
}

Hope this gets you going.

Upvotes: 4

Related Questions