如何在服务器端实现Quartz Scheduler?我想要做的所有步骤是什么?

时间:2014-04-19 06:05:06

标签: java quartz-scheduler

public static void setupSchedule() {
    try {
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler scheduler = sf.getScheduler();
        JobDetail job = newJob(Reader.class).withIdentity("job1", "group1").build();
        String Frequency = Props.getProps().getProperty("BIRTHDAYFREQUENCY");
        CronTrigger ct = newTrigger().withIdentity("trigger1", "group1")
            .withSchedule(cronSchedule(Frequency)).build();

        scheduler.scheduleJob(job, ct);

    } catch (SchedulerException e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

如果您要求在Web应用程序中实现quartz scheduler,那么回答是肯定的。 您只需将所有调度程序配置放在servlet init方法中。就像你已经完成了你的问题。 Init方法只在应用程序启动时或第一次请求时调用。因此,所有配置和调度程序时间都将在那时设置。确保包含所有石英依赖项。

private Scheduler scheduler ;
public void init(ServletConfig config) throws ServletException 
    {
JobKey jobKeyA = new JobKey("jobA", "group1");
        JobDetail jobA = JobBuilder.newJob(CallJob.class)
        .withIdentity(jobKeyA).build();
        //jobA.getJobDataMap().put("queue_setup", queue_setup);
        Trigger trigger1 = TriggerBuilder
        .newTrigger()
        .withIdentity("triggerJobA", "group1")
        .withSchedule(
                SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInMinutes(1).repeatForever()).build();
//runs every minute
scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(jobA, trigger1);

}

Job class
public class CallJob implements Job {
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException 
    {
System.out.println("Yes job is running");
}
}