If you are using EJB 3.0, you can use the @Timeout annotation to create simple EJBs that are invoked based on time. As they are still EJBs you get all the things that you can use with an EJB - transaction, jta, security etc.
You can programatically create a timer with the TimerService. Since your requirement is to create timers based on values in the database, I think this would be a good fit. The initial call to create timers will have to be made from another EJB or a servlet. You can read more about timers in the tutorial.
If you are using EJB 3.1, you have much more options and annotations to create timers along with the above mentioned TimerService. You can read about EJB 3.1 timers in this tutorial
SCJP 1.4, OCMJEA/SCEA 5.0.
Joined: Oct 16, 2012
How will I call the schedule from the database and invoke it? Meaning I will always call database just to check whether a schedule needs to run?
That is something you will have to decide based on your requirement.
For example, suppose you have developed a mechanism which reads the database and does the scheduling. Everything's fine and 2 days later, more schedules are added to the database.
First of all, how are these schedules added to the database? do you provide a screen where an admin guy does that? or is it going to be added directly to the database? in that case how will this be notified? You will have to consider all these things.
So, you will need two things. One is a piece of code which will read the database and create the scheduled jobs. Let's call it JobManager.
Second one is the actual Job - the process that runs in a Thread and does the job (the JobManager creates one thread each for one job - in your case, per user).
Now, you can have the JobManager run on application startup. This means when the application starts, the JobManager will read the db, create the schedules and everything will work fine. But in this case, if someone adds a new schedule to the database, it won't be picked up until the application restarts. So, you can have this JobManager periodically check the db - say, every day in the morning or every hour. Or, whenever the db is modified, you can notify the JobManager.
Now, If you are using EJB 3.1, you can have the JobManager (an EJB) run on startup of the app with the @Startup annotation (in combination with @Singleton) - the @PostConstruct method will be called when the app starts - so, you can create the schedules using TimerService programatically in this method. You can also use the @Schedule annotation on this bean to have this bean check the db periodically (every day or every hour - you can have more information on this in the JEE 6 tutorial link I provided earlier). In case notification is to be done, you can call the appropriate method too from a Servlet or another EJB. This way you can re-use the code too.
If you are using EJB 3.0, the startup should be done using a ServletContextListener (you can read this post for more info). In this case, the EJB's method will be called which can then create a programattic schedule for itself (for periodic check on the db) and also scheduels for the actual Job using the TimerService.
Note: I have all along talked about the options from a server point of view only. Note that this need not be the case. You can have this entire stuff run as a standalone Java application too.
In this case, the JobManager will run as a deamon process. This JobManager itself will have to be started via OS-based schedulers (ex.:Cron). This JobManager can be developed using Timer and TimerTask classes. Then, the JobManager can use the Concurrency API (ScheduledExecutorService) to create the schedules.
Joined: Oct 16, 2012
Hi Ranganathan Kaliyur Mannar ,
Our project is a web based, and it is already existed. We need to make a module that will generate report for every customer based on their schedule created. There is a page were the customer can set their own schedule of generating the report (for example: they can set it every thursday 5pm) and it will be saved into the database.
So what will i do now is to have a Job that will check the database every second if there is a schedule to generate reports, and if there is it will generate a report for that user? am I right?
Hisouka Devera wrote: So what will i do now is to have a Job that will check the database every second if there is a schedule to generate reports, and if there is it will generate a report for that user? am I right?
Yah, and I have pointed out various ways to program this task.