3

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. :(

STW
  • 44,917
  • 17
  • 105
  • 161
shiny
  • 43
  • 1
  • 11
  • I copy and pasted the jar :quartz-1.8.0 in WEB-INF\lib, then it is not found while import in my java file :( – shiny Feb 07 '11 at 14:54
  • Are you sure you used the right import statement? Check the JAR file to make sure the package structure in the JAR file matches your import statement. Also, make sure your classpath is set correctly. Change your log level to DEBUG or ALL so that you have all the necessary information to solve the problem. – jamesmortensen Feb 08 '11 at 04:17

1 Answers1

7

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.

Nishant
  • 54,584
  • 13
  • 112
  • 127
  • Also, Quartz ships with a org.quartz.jobs.ee.mail.SendMailJob job class, that knows how to send e-mail, that will work for many applications, or serve as example code. – jhouse Feb 05 '11 at 16:40
  • @jhouse thanks for the information. I have updated this in the answer. – Nishant Feb 05 '11 at 19:17
  • I copy and pasted the jar :quartz-1.8.0 in WEB-INF\lib, then it is not found while import in my java file :( – shiny Feb 07 '11 at 14:55
  • @user604155 The Jar should be in classpath. `WEB-INF/lib` is a class-path when your server is running. Are you getting `ClassNotFound` during compilation or at run time? – Nishant Feb 07 '11 at 15:05