quartz---的SimpleTrigger

            quartz---的SimpleTrigger

 1 package com.imooc.demo.helloQuartz;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 
 6 import org.quartz.JobBuilder;
 7 import org.quartz.JobDetail;
 8 import org.quartz.Scheduler;
 9 import org.quartz.SchedulerException;
10 import org.quartz.SchedulerFactory;
11 import org.quartz.SimpleScheduleBuilder;
12 import org.quartz.SimpleTrigger;
13 import org.quartz.TriggerBuilder;
14 import org.quartz.impl.StdSchedulerFactory;
15 
16 public class HelloScheduler {
17 
18     public static void main(String[] args) throws SchedulerException {
19         Date date = new Date();
20         SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
21         System.out.println("Current Time is:"+sf.format(date));
22         // 创建一个jobDatail实例,将该实力与helloJob绑定
23         JobDetail detail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob")
24                 .build();
25 
26         // 创建一个Trigger实例,4秒后执行第一次,并且在每隔2秒重复执行
27         date.setTime(date.getTime()+4000L);
28         SimpleTrigger trigger = (SimpleTrigger)TriggerBuilder
29                 .newTrigger()
30                 .withIdentity("myTrigger", "group1")
31                 .startAt(date)
32                 .withSchedule(SimpleScheduleBuilder
33                         .simpleSchedule()
34                         .withIntervalInSeconds(2)
35                         //无限执行:SimpleTrigger.REPEAT_INDEFINITELY)
36                         .withRepeatCount(3))
37                 .build();
38         // 创建Scheduler实例
39         SchedulerFactory factory = new StdSchedulerFactory();
40         Scheduler scheduler = factory.getScheduler();
41         scheduler.start();
42         // 把jobDetail和Trigger与scheuler绑定在一起:触发启动
43         scheduler.scheduleJob(detail, trigger);
44     }
45 }

    

 1 package com.imooc.demo.helloQuartz;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 
 6 import org.quartz.Job;
 7 import org.quartz.JobExecutionContext;
 8 import org.quartz.JobExecutionException;
 9 
10 public class HelloJob implements Job {
11 
12     @Override
13     public void execute(JobExecutionContext context) throws JobExecutionException {
14         // 打印当前执行的时间,格式为2017-05-06 00:00:00
15         Date date = new Date();
16         SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
17         System.out.println("Current Exec Time Is:" + sf.format(date));
18         System.out.println("Hello World");
19 
20     }
21 }


quartz:
      SimpleTrigger://每隔几秒无限执行执行或执行几次
        startAt(time);withRepeatValInSeconds(2秒)WithRepeatCount(3次)

 

 

 1 package com.imooc.demo.helloQuartz;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 
 6 import org.quartz.JobBuilder;
 7 import org.quartz.JobDetail;
 8 import org.quartz.Scheduler;
 9 import org.quartz.SchedulerException;
10 import org.quartz.SchedulerFactory;
11 import org.quartz.SimpleScheduleBuilder;
12 import org.quartz.SimpleTrigger;
13 import org.quartz.TriggerBuilder;
14 import org.quartz.impl.StdSchedulerFactory;
15 
16 public class HelloScheduler {
17 
18     public static void main(String[] args) throws SchedulerException {
19         Date date = new Date();
20         SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
21         System.out.println("Current Time is:"+sf.format(date));
22         // 创建一个jobDatail实例,将该实力与helloJob绑定
23         JobDetail detail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob")
24                 .build();
25 
26         //获取秒之后的时间
27         Date endDate=new Date();
28         endDate.setTime(endDate.getTime()+6000);
29         // 创建一个Trigger实例,4秒后执行第一次,并且在每隔2秒重复执行
30         date.setTime(date.getTime()+4000L);
31         //endTime可以覆盖执行次数
32         SimpleTrigger trigger = (SimpleTrigger)TriggerBuilder
33                 .newTrigger()
34                 .withIdentity("myTrigger", "group1")
35                 .startAt(date)
36                 .endAt(endDate)
37                 .withSchedule(SimpleScheduleBuilder
38                         .simpleSchedule()
39                         .withIntervalInSeconds(2)
40                         //无限执行:SimpleTrigger.REPEAT_INDEFINITELY)
41                         .withRepeatCount(3))
42                 .build();
43         // 创建Scheduler实例
44         SchedulerFactory factory = new StdSchedulerFactory();
45         Scheduler scheduler = factory.getScheduler();
46         scheduler.start();
47         // 把jobDetail和Trigger与scheuler绑定在一起:触发启动
48         scheduler.scheduleJob(detail, trigger);
49     }
50 }

 

    endTime可以覆盖执行次数

 

转载于:https://www.cnblogs.com/meiLinYa/p/9020280.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值