I want to develop a simple Java Web app to send an email notifications after some task is done, such as a request submitted for approval, and reminders (to say approvers) at regular intervals. I want to do this using the Quartz Scheduler. I am a newbie, so can anyone help me to start on this.
Thanks in Advance.
I copy and pasted the JAR file : quartz-1.8.0 in WEB-INF\lib and even in common\lib, then it is not found while importing in my Java file. :(
Create a servlet that starts at web-app init.
<web-app>
...
<servlet>
<servlet-name>Emailer</servlet-name>
<servlet-class>my.servlet.Emailer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
...
</web-app>
In init()
of the servlet configure your scheduler (the example below triggers every 10 minutes)
SchedulerFactory schFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler sched = schFact.getScheduler();
JobDetail job = new JobDetail("job1", "group1", EmailerJob.class);
CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1", "* 0/10 * * * ?");
sched.addJob(job, true);
sched.start();
Write a Class inplementing Job interface of Quartz.
EmailerJob implement Job{
public void execute(JobExecutionContext arg0) throws JobExecutionException {
//Code to send mails goes here
}
}
P.S. The code above is not-tested, but it gives you a fair idea what to do.
As @jmort253 rightly pointed, Quartz tutorial is the best resource and if I remember correctly, they have an scheduled emailer example done somewhere in that.
Update
Alright, Google to solve your issue. And here is your most detailed solution that anyone can give to you! Java – Job Scheduling in web application with quartz API
Edit#1 You may use ContextListener
instead of servlet to initiate Quartz scheduler.
Update 1
As @jhouse rightly mentioned that instead of writing your own Job
that handle mailing, you can ask Quartz predefined SendMailJob
class to do the same. Thanks @jhouse.