aspose file tools*
The moose likes Java in General and the fly likes Fire up a read thread when the new file added to the directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Fire up a read thread when the new file added to the directory" Watch "Fire up a read thread when the new file added to the directory" New topic
Author

Fire up a read thread when the new file added to the directory

David Wei
Greenhorn

Joined: Jan 04, 2002
Posts: 17
Hello,
I am writing a thread to copy the files from a specific directory to the server. Since the files are sent to this directory by another group and I want to do the coping whenever the files are in the directory.
Could you tell me how I can monitor the files in the directory and fire up the copy thread when the files are there?
Thanks in advance!
David
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Who is creating the files in the folder in the first place? If it is a server-controlled code (some servlet), then it can issue a notify() after the files are created. The copying thread would be in a wait state, but when notified, it would copy the files to whatever their final destination is.
Alternatively, you may want to do some other means of notification, such as JMS.
If your server is not in control of when the files are created, then your option is to run a low priority background thread that will check for the existance of the new files and copy them to the destination.
Eugene.
Trelan Hylton
Greenhorn

Joined: Apr 15, 2003
Posts: 27
I love this thread! I am doing something similar so i need to know what is JMS that was suggested to do the checking for the files. another program writes the files to the directory and makes a note of this to a textfile. the java program then reads this textfile to do its processes to the file recently added to the directory. i was thinking about using a timer to check the textfile from time to time but a listener for the generated file would be much more accurate. so again what is JMS?


JH
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
hi
- check out this JMS link.
it is used for async communication through messages. the part of your code creating the file would notify the copy thread trough a JMS message.
- there is another way to achieve this: your copy thread could just remember the time it run the last time and then use the lastModifed timestampt fom the files. this way you would need to scan the full directory(structure) every time which might result in bad performance if there are lots of files/directories.
k
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
hi again
i just checked the lastModifed() on a win machine. if you create a file, the parent directorys lastModifed() is updated too. other than that it seems to work.
k
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If you're on Windows and have some good C++ skills in your group ... a C program can subscribe to messages from Windows about changes to directories. That's how Windows Explorer and even the file/open dialog can instantly show new files. The C program would send an event back to Java - perhaps via JNI? Donno if Unix has a similar API.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
 
subject: Fire up a read thread when the new file added to the directory