Wednesday, May 14, 2008

Stand alone quartz scheduler

Create a MyScheduler class:


public class MailScheduler implements Constants
{
private static Log log = LogFactory.getLog(MailScheduler.class);

public MailScheduler()
{
long frequency = 5 * TIME_DAY; // every 5 days
try
{
Scheduler sched = new StdSchedulerFactory().getScheduler();
sched.start();

Date startSending = DateHelper.incrementDays(new Date(), 1); //tomorrow

JobDetail jd = new JobDetail("gameReminder", Scheduler.DEFAULT_GROUP, GameScoreEntryReminderJob.class);
SimpleTrigger st = new SimpleTrigger("daily", Scheduler.DEFAULT_GROUP, startSending, null, SimpleTrigger.REPEAT_INDEFINITELY, frequency);
sched.scheduleJob(jd, st);
} catch (Exception e)
{
log.error("MailScheduler exception");
log.error(e.getMessage());
}
}


Create MyJob class:


public void execute(JobExecutionContext arg0) throws JobExecutionException
{
try
{
if (mailModule == null)
throw new NullPointerException("Please configure MailModule bean.");
List<TeamGame> teamGames = teamGameDao.fetchYesterdayGames();

for (TeamGame teamGame : teamGames)
{

List<Administrator> admins = teamGameDao.fetchAdmins(new Long(19));

for (Administrator admin : admins)
{
log.warn("Admin email is: " + admin.getUser().getEmail());
mailModule.sendGameReminder(teamGame, admin);
}
}

} catch (Exception e)
{
log.error("An error occured while executing the job - " + e.getMessage(), e);
}
}