views:

40

answers:

2

Hello, In my project, I use some files in a directory. And for some reasons, I must load the files at the launch of the project, and if there is an action on these files, I must reload them (in a HashMap) For the moment, I load them just at the moment of the use, but I want to do this at the load of application, and let the JNotify working all the time.

I am working on a web project. Can I have some help please ? thnx

+1  A: 

Other than manually polling the file system yourself, JNotify (or a library like it) is probably your best option for now. There are changes planned for Java 7 that will address the need for this (see the WatchService section on the linked page).

Edit:

On closer reading of your question, I understand that you're not really looking for alternatives, but for help in using JNotify. See the code samples section. You need to add a watch to the directory or files you're watching, and add your own class as a Listener for change events. You'll need to implement JNotifyListener in your code and handle the events that you're interested in.

Bill the Lizard
I use the JNotifyListener, and everything is working. What I want to do, is that the listener works all the time, like as in a servlet that I launch in the start of the application.Do you see ??Thnx a lot
taichimaro
@taichimaro: You should be able to do that from a servlet by setting its `<load-on-startup>` parameter in your web.xml file. This will cause your servlet's init() method to be run when the server starts up, from which you can launch a daemon thread to handle notifications from JNotify.
Bill the Lizard
@Bill the Lizard : and how can I do for a daemon thread ??? It's really urgent
taichimaro
@taichimaro: Take a look at [TIJ Daemon threads](http://www.linuxtopia.org/online_books/programming_books/thinking_in_java/TIJ315_005.htm). Your thread class will need to implement `JNotifyListener` and handle the events that are generated when a file changes. Your servlet's init() method will need to instantiate and run your custom thread.
Bill the Lizard
@Bill the Lizard : and what should I do in the run method ????
taichimaro
A: 

I used a Servlet that I launch in the begining of the application. I create the JNotify in the init() method, and because it have Listeners, it will listen to the directories that I want to watch all the life of the Servlet. Thnx for you answers.

taichimaro