SimpleTrigger:
它只能用于指定任务在一个特定时间内运行,可指定任务的重复(时间,次数)与间隔(时间,次数)
代码说明了一切,请参见注释!
工程图片
SimpleJob.java
SimpleTriggerExample.java
工程请参见附件!
它只能用于指定任务在一个特定时间内运行,可指定任务的重复(时间,次数)与间隔(时间,次数)
代码说明了一切,请参见注释!
工程图片
SimpleJob.java
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.quartz.Job;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- import org.quartz.JobKey;
- public class SimpleJob implements Job {
- private static Logger _log = LoggerFactory.getLogger(SimpleJob.class);
- public SimpleJob() { }
- public void execute(JobExecutionContext context)
- throws JobExecutionException {
- JobKey jobKey = context.getJobDetail().getKey();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- _log.error("Quarta JobKey : " + jobKey + " 执行时间: " + sdf.format(new Date()));
- }
- }
SimpleTriggerExample.java
- import static org.quartz.JobBuilder.newJob;
- import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
- import static org.quartz.TriggerBuilder.newTrigger;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.quartz.DateBuilder;
- import org.quartz.JobDetail;
- import org.quartz.Scheduler;
- import org.quartz.SchedulerFactory;
- import org.quartz.SchedulerMetaData;
- import org.quartz.SimpleTrigger;
- import org.quartz.impl.StdSchedulerFactory;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- /**
- * SimpleTrigger:它只能用于指定任务在一个特定时间内运行,可指定任务的重复(时间,次数)与间隔(时间,次数)
- * */
- public class SimpleTriggerExample {
- Logger log = LoggerFactory.getLogger(SimpleTriggerExample.class);
- public void run() throws Exception {
- // 通过SchedulerFactory获取一个调度器实例
- SchedulerFactory sf = new StdSchedulerFactory();
- Scheduler sched = sf.getScheduler();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- // 在当前时间15秒后运行
- Date startTime = DateBuilder.nextGivenSecondDate(null,15);
- // 通过过JobDetail封装SimpleJob,同时指定Job在Scheduler中所属组及名称,这里,组名为group1,而名称为job1。
- JobDetail job = newJob(SimpleJob.class).withIdentity("job1", "group1").build();
- // 创建一个SimpleTrigger实例,指定该Trigger在Scheduler中所属组及名称。
- // 接着设置调度的时间规则.当前时间15秒后运行,每10秒运行一次,共运行5次
- /*SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger1", "group1")
- .startAt(startTime).withSchedule(simpleSchedule()
- .withIntervalInSeconds(10)
- .withRepeatCount(5)
- )
- .build();*/
- //从当前时间开始,每10秒钟执行一次,直到程序结束为止
- /*SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger1", "group1")
- .startAt(startTime).withSchedule(simpleSchedule()
- .withIntervalInSeconds(10)
- .repeatForever()
- )
- .build();*/
- //从当前时间开始,每一分钟执行一次,在设置withIntervalInSeconds(10)无效
- /*SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger1", "group1")
- .startAt(startTime).withSchedule(simpleSchedule()
- //.withIntervalInSeconds(10)
- .repeatMinutelyForever()
- )
- .build();*/
- //从当前时间开始,repeatMinutelyForever(10),不带参数为每分钟执行一次,带了参数等于: 60*10
- /*SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger1", "group1")
- .startAt(startTime).withSchedule(simpleSchedule()
- .repeatMinutelyForever(10)
- )
- .build();*/
- //从当前时间开始,每秒钟执行一次,当然带了参数等于1*? ^_^
- /*SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger1", "group1")
- .startAt(startTime).withSchedule(simpleSchedule()
- .repeatSecondlyForever()
- )
- .build();*/
- //repeatHourlyForever()方法是第1小时执行一次,带了参数等于1*?
- //从当前时间开始,每1分钟执行一次,并且只执行4次
- /*SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger1", "group1")
- .startAt(startTime).withSchedule(simpleSchedule()
- .repeatMinutelyForTotalCount(5)
- )
- .build();*/
- //repeatSecondlyForTotalCount(5),反过来说:只执行4次,每次执行的间隔为1秒钟
- //当前时间的加上5分钟
- Date endTime = DateBuilder.nextGivenMinuteDate(null, 5);
- System.out.println("开始时间: "+ sdf.format(startTime)+",结束时间: "+sdf.format(endTime));
- //这个比较常用吧
- //指定从: startTime为开始时间,endTime为结束时间,规则:每2秒钟执行一次,repeatForever()方法表示重复执行,一直到结束时间
- SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger1", "group1")
- .startAt(startTime)
- .endAt(endTime)
- .withSchedule(
- simpleSchedule()
- .withIntervalInSeconds(2)
- .repeatForever()
- )
- .build();
- // 注册并进行调度
- Date ft = sched.scheduleJob(job, trigger);
- //getRepeatInterval()指:执行任务间的间隔时间,getRepeatCount()表示总共执行多少次,-1表示未知
- log.error(job.getKey() + " will run at: " + sdf.format(ft)
- + " and repeat: " + trigger.getRepeatCount() + " times, every "
- + trigger.getRepeatInterval() / 1000 + " seconds");
- // 调度启动
- sched.start();
- try {
- //1分钟
- Thread.sleep(600L * 1000L);
- } catch (Exception e) {
- }
- //调度器停止
- sched.shutdown(true);
- SchedulerMetaData metaData = sched.getMetaData();
- //总共运行了多少个任务
- log.error("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
- }
- public static void main(String[] args) throws Exception {
- SimpleTriggerExample example = new SimpleTriggerExample();
- example.run();
- }
- }
工程请参见附件!