问题
I'm not really sure what is the best way to initialize Quartz to schedule a cron job. My environment is Tomcat. I have one job that needs to be triggered every day.
Should I create a separate Servlet to initialize Quartz and schedule my job?
I'm thinking of creating a Servlet and on the init() schedule my job something like this:
SchedulerFactory sf=new StdSchedulerFactory();
Scheduler sched=sf.getScheduler();
JobDetail jd=new JobDetail("job1","group1",CronJob.class);
CronTrigger ct=new CronTrigger("cronTrigger","group2","0 0/1 * * * ?");
sched.scheduleJob(jd,ct);
sched.start();
I'm new to Quartz but I guess I always need to keep a reference to the SchedulerFactory in order for Quartz to be running, therefore having that on a Servlet will be best option?
回答1:
You might want to take a look at the Cookbook section on the Quartz site.
There are two easy built-in methods for starting a Quartz Scheduler within a servlet environment, using either a <listener>
or <servlet>
in the app's web.xml.
来源:https://stackoverflow.com/questions/3257074/whats-the-best-way-to-initialize-quartz