I need to write a component to sit on the server and check some records in a database every hour. I need this component to run on the server 24 hrs a day. If the component finds a record that meets a certain criteria, then it will send off an e-mail.
The question that I have is what is the best architecture for this component. I thought about just putting a servlet on the webserver and make that check the DB every hour, but there must be a better way.
I wouldn't use a servlet for this job (it's not what servlets are for).
In my opinion, you have two viable options:
1. launch a standalone java program using an OS-specific timer (e.g. cron in unix).
2. start a standalone java program upon server startup and use the standalone program's thread to wake up and send emails once in an hour.
I would opt for the second approach, but that's just because I'm a bit suspicious about using operating system specific stuff...
I agree with Lasse. Check out java.util.Timer and java.util.TimerTask.
Hi, because you mentioned Serlvet, you should not implement a servlet to do this, rather implement a ServletContextListener, that runs a thread that does this on any interval you want. That way you can be sure the thread runs while your ServletContext is running and is deleted if the ServletContext dies, (garbage collected).
At least because you're thinking of implementing a servlet to do this, this should be more correct way for you to deal with threads running as a part of an WebApplication.
hope it helps