动态调度作业:在groovy中使用cron触发器

时间:2015-01-09 23:15:54

标签: grails groovy quartz-scheduler

我还在玩石英调度程序。我创建了以下工作 grails create-job,我想在这里做的很简单,就是创建一个 触发并尝试运行execute method.once这个基本代码运行我想要 在for中创建多个触发器,每个触发器具有不同的cron调度值 循环(具有不同执行时间的多个触发器)并运行执行 方法并对这些触发器的列表执行sched.scheduleJob(triggerName)

import org.quartz.*
import org.quartz.Trigger
import static org.quartz.JobBuilder.*;
import static org.quartz.CronScheduleBuilder.*;
import static org.quartz.TriggerBuilder.*;
public class TrialJob 
{
    public static void main(String[] args)
    {
       JobDetail job = JobBuilder.newJob(TestJob.class).withIdentity("dummyJobName1","group11").build();

       CronTrigger trigger = newTrigger().withIdentity("trigger","group1").withSchedule(cronSchedule("0 55 15 * * ?")).build();

       Scheduler scheduler = new StdSchedulerFactory().getScheduler();

       scheduler.scheduleJob(job,trigger);

       scheduler.start();

       //while(true){};
    }    

    public static class TestJob implements Job 
    { 
       public void execute(JobExecutionContext context) throws JobExecutionException 
       {
           println "inside execute "
       }        
    }
}

1 个答案:

答案 0 :(得分:2)

首先,提供的代码无法编译。尝试将类org.quartz.impl.StdSchedulerFactory的实例分配给声明为org.quartz.Scheduler的变量。

其次,程序运行良好,作业已安排,但在捕获任何输出之前它已存在。要证明它在下面的示例中使用未注释的//while(true){};行。该示例来自here

@Grab(group='org.quartz-scheduler', module='quartz', version='2.2.1') 

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class CronTriggerExample {
    public static void main( String[] args ) throws Exception {
        JobDetail job = JobBuilder.newJob(HelloJob.class)
        .withIdentity("dummyJobName1", "group11").build();

        Trigger trigger = TriggerBuilder
        .newTrigger()
        .withIdentity("dummyTriggerName1", "group11")
        .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
        .build();


        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job, trigger); 

        //while(true){};
    }
}

public class HelloJob implements Job {
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("Hello Quartz!");    
    }
}

希望能帮到你。