I want to know the best method to schedule a code. I have a code that generates reports and sends mail to a set of people at interval of 24hrs. Its a console based java application. I want to know the best method to schedule that. Sometimes I may need to change that to 12hrs interval. However the application doesn’t perform any other task in between the interval.
Share
Here are few approach, from simplest to most comprehensive:
sleep():This approach is very simple, do the work and sleep for 24 hours. Actually it is a bit more complex because the report generation takes some time, so you have to sleep slightly shorter. All solutions below handle this transparently.
java.util.Timer#scheduleAtFixedRate()– simple, built-in Java solution.@Scheduledannotation in spring or@Schedulein ejb – more complex but also more powerful, e.g. accepts cron expressions:quartz-scheduler – full blown Java scheduler with clustering and fail-over, misfire handling, full cron support, etc. Very comprehensive:
or