Email Notifications and Reminders in Java Web app using Quartz Scheduler

旧街凉风 提交于 2019-12-07 17:41:02

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/4905671/email-notifications-and-reminders-in-java-web-app-using-quartz-scheduler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!