• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic