• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mike London
Ranch Hand
Posts: 1208
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike London
Ranch Hand
Posts: 1208
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1208
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic