File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to automate a process when a directory's contents have changed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to automate a process when a directory Watch "How to automate a process when a directory New topic
Author

How to automate a process when a directory's contents have changed

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1054
I want to build an html page dynamically with links to images from the files that are in a directory.
The web master can add new image files whenever he wants.
For performance reasons, I would like to build an HTML page once a day or other interval based on the files in the directory at that time.
Is there a way to automate a servlet (or other process) to execute code at particular times? For example, I would like a "process" (not sure what this would be) to check everyday, and if new files, then re-build the HTML.
I know how to do everything *but* executing code at a particular time to check the directory to see if a new HTML page needs to be generated.
Anybody's insight or ideas would be greatly appreciated.
Thanks in advance.
-- Mike
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
There are number of approaches:
Depending of your Operating System, if you're on Unix/linux you could schedule a process with "cron jobs", or if you're on windows you could use the "at" command.
Or another approach would just be to start a process with a thread that wakes up after a certain duration, does it's thing, then goes back to sleep.
Also, in Java 1.4 I think there are some new classes for doing this sort of thing:
java.util.Timer & java.util.TimerTask
I haven't looked into these though.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Mike,
Sounds to me like you need to use a full servlet approach here. What you are describing is creating a new static HTML page whenever new images are available (or possibly old images are no longer available). If you change your HTML to JSP (which is HTML with optional java code embedded) then there would be no need to periodically update the page. Or you could just use a servlet straight out. Either way, whenever a client requested the page, the servlet (or JSP) would read the images from the directory and generate the page on the fly.
Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1054
Hi Michael,
The reason I wanted to build a static page at certain intervals was that so, if there are hundreds of images, the user wouldn't have to wait for this page to be rebuilt everytime they came back from looking at one of the thumbnails in a preview window.
Do you think the page would get built quickly enough to not be objectionable?
If it would, this would simply what I need to do.
Thanks in advance for your reply.
-- Mike
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Mike,
Good point. Well you could keep up with what images were sent to each client with sessions and only use those images when the client returns to the page. Or you could you a sort of in between approach and have the servlet cache up the images in a Collection and reread the image directory at a specified time interval to update the cache.
Hope this helps,
Michael Morris
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1054
Thanks again for your reply.
I'm not sure how to have a servlet cache into a collection, but you have given me some good ideas to think about.
Perhaps the best way to proceed would be to "assume" that the page re-loading everytime wouldn't be too intrusive and, if it is, investigate some other methodology.
Thanks again.
-- Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to automate a process when a directory's contents have changed