quartz 关联多个 JobDetail和SimpleTrigger 多任务调度

1. import java.util.Date; 
   2.  
   3. import org.quartz.JobDetail; 
   4.  
   5. import org.quartz.Scheduler; 
   6.  
   7. import org.quartz.SchedulerFactory; 
   8.  
   9. import org.quartz.SimpleTrigger; 
  10.  
  11. import org.quartz.impl.StdSchedulerFactory; 
  12.  
  13. public class SimpleTriggerRunner { 
  14.  
  15.     public static void main(String args[]) { 
  16.  
  17.         try { 
  18.  
  19.             // ①创建一个JobDetail实例,指定SimpleJob 
  20.  
  21.             JobDetail jobDetail1 = new JobDetail("job1_1", "jGroup1", 
  22.                     SimpleJob.class); 
  23.  
  24.             // ②通过SimpleTrigger定义调度规则:马上启动,每2秒运行一次,共运行100次 
  25.  
  26.             SimpleTrigger simpleTrigger1 = new SimpleTrigger("trigger1_1", 
  27.                     "tgroup1"); 
  28.  
  29.             simpleTrigger1.setStartTime(new Date()); 
  30.  
  31.             simpleTrigger1.setRepeatInterval(1000); 
  32.  
  33.             simpleTrigger1.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY); 
  34.              
  35.              
  36.             // ①创建一个JobDetail实例,指定SimpleJob 
  37.  
  38.             JobDetail jobDetail2 = new JobDetail("job1_2", "jGroup2", 
  39.                     DemoJob.class); 
  40.  
  41.             // ②通过SimpleTrigger定义调度规则:马上启动,每2秒运行一次,共运行100次 
  42.  
  43.             SimpleTrigger simpleTrigger2 = new SimpleTrigger("trigger1_2", 
  44.                     "tgroup2"); 
  45.  
  46.             simpleTrigger2.setStartTime(new Date()); 
  47.  
  48.             simpleTrigger2.setRepeatInterval(3000); 
  49.  
  50.             simpleTrigger2.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY); 
  51.  
  52.             // ③通过SchedulerFactory获取一个调度器实例 
  53.  
  54.             SchedulerFactory schedulerFactory = new StdSchedulerFactory(); 
  55.  
  56.             Scheduler scheduler = schedulerFactory.getScheduler(); 
  57.  
  58.             scheduler.scheduleJob(jobDetail1, simpleTrigger1);// ④ 注册并进行调度 
  59.             scheduler.scheduleJob(jobDetail2, simpleTrigger2);// ④ 注册并进行调度 
  60.  
  61.             scheduler.start();// ⑤调度启动 
  62.  
  63.         } catch (Exception e) { 
  64.  
  65.             e.printStackTrace(); 
  66.  
  67.         } 
  68.  
  69.     } 
  70.  
  71. } 
  72. //=============================================== 
  73. import java.util.Date; 
  74.  
  75. import org.quartz.Job; 
  76.  
  77. import org.quartz.JobExecutionContext; 
  78.  
  79. import org.quartz.JobExecutionException; 
  80.  
  81. public class SimpleJob implements Job { 
  82.  
  83.     // ①实例Job接口方法 
  84.  
  85.     public void execute(JobExecutionContext jobCtx) 
  86.             throws JobExecutionException { 
  87.  
  88.         System.out.println(jobCtx.getTrigger().getName() 
  89.                 + " triggered. time is:" + (new Date())); 
  90.  
  91.     } 
  92.  
  93. } 

你可能感兴趣的:(quartz,jobdetail)