How to execute a task every year in a web application automatically? Is the Timer class helpful for this? I also heard about Quartz? I registered in their page but it seems not to be free. Thank you for your help.
3 Answers
Quartz is definitely a good option, it is free and opensource and uses the Apache 2.0 License (you don't have to register to download). You can also simply use cron if you are in a *nix environment.
You can also use a java.util.Timer
but I would only do so if your use case is extremely trivial and you make sure you restart your Timer
correctly based on some type of persistence layer on startup of your web app.

- 53,220
- 42
- 124
- 197
-
1cron => or a Scheduled Task in Windows. – Max Nov 24 '12 at 03:44
-
I, too, rather do a cron job, since it does not need a VM to run the whole year, just to be executed once. – Andy Nov 24 '12 at 08:24
-
Thank you for the answers but sorry how can I use a cronjob in a Windows environment? is that posible? I was reading about a cronexpression but it seems to be only for unix? – Diego Ramos Nov 24 '12 at 17:33
-
As @Max mentioned you can use Scheduled Task: http://windows.microsoft.com/en-US/windows7/schedule-a-task – Abdullah Jibaly Nov 25 '12 at 05:04
Consider checking with the business stakeholders if a job which runs only once a year should really be fully automated. From my experience, there's a good chance that there will be minor changes to the schedule (e.g. which day of the week should the job run this year?) or to the steps of the job after such a long time.
Also, will there be other scheduled jobs added to the systems in the future? If not - will it be worth adding the overhead (implementation, testing, monitoring etc) of an automated execution to the system if there's just one job per year? Something running this infrequently can easily be forgotten and nobody might notice when it's broken.
Consider this alternative: add a button somewhere in your app to kick off the job manually, maybe on an admin page. If your app has a dashboard or landing page, add some piece of information visible only for certain users, such as admins- for example: "The X job should has been running the last time on 11/23/2011- please initiate next run before 11/30/2012".

- 9,220
- 10
- 51
- 83
I would suggest that you use something like cron. Your requirement is very similar to the UNIX cron daemon's activity. There is a java library that provide the same capability called cron4j (http://www.sauronsoftware.it/projects/cron4j/).
Below are some schedulers that you can use.

- 1,185
- 2
- 12
- 30
-
-
1@DiegoRamos : Cron is for Linux environment. The equivalent of Cron for windows environment is windows task scheduler. One method would be to create a new task using that tool and set a jar file as the script. the jar file may contain the code that you need to run periodically. another method is you can create a command script and add it to the task scheduler instead of a jar file. See the following link. http://stackoverflow.com/questions/12034177/running-a-java-program-as-a-scheduled-task – lahiru madhumal Nov 25 '12 at 03:01