This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
David Wei
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2937
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
karl koch
Ranch Hand
Posts: 388
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 388
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic