简介
---01---
核心接口:
调度器通过触发器执行任务。
---02---
触发器:
大前提是给定的时间
shi
大前提是每隔一段时间
年可以省略的。
---03---
简单的案例:
代码:
第一步:我们首先看下这个HelloJob:
简单的触发示例:
/*
* All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
package cn.itcast.quartz.example;
import static org.quartz.DateBuilder.evenMinuteDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
/**
* This Example will demonstrate how to start and shutdown the Quartz scheduler and how to schedule
* a job to run in Quartz.
*
* @author Bill Kratzer
*/
public class SimpleExample {
public void run() throws Exception {
Logger log = LoggerFactory.getLogger(SimpleExample.class);
log.info("------- Initializing ----------------------");
// 定义调度器
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
log.info("------- Initialization Complete -----------");
// 获取当前时间的下一分钟
Date runTime = evenMinuteDate(new Date());
log.info("------- Scheduling Job -------------------");
// 定义job
// 在quartz中,有组的概念,组+job名称 唯一的
JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();
// 定义触发器,在下一分钟启动
Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();
// 将job注册到调度器
sched.scheduleJob(job, trigger);
log.info(job.getKey() + " will run at: " + runTime);
// 启动调度器
sched.start();
log.info("------- Started Scheduler -----------------");
// 等待65秒
log.info("------- Waiting 65 seconds... -------------");
try {
// wait 65 seconds to show job
Thread.sleep(65L * 1000L);
// executing...
} catch (Exception e) {
//
}
// 关闭调度器
log.info("------- Shutting Down ---------------------");
sched.shutdown(true);
log.info("------- Shutdown Complete -----------------");
}
public static void main(String[] args) throws Exception {
SimpleExample example = new SimpleExample();
example.run();
}
}
---04---
复杂的调度:
/*
* All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
package cn.itcast.quartz.example;
import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.DateBuilder.evenMinuteDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
/**
* This Example will demonstrate how to start and shutdown the Quartz scheduler and how to schedule
* a job to run in Quartz.
*
* @author Bill Kratzer
*/
public class SimpleCronExample {
public static int a = 0;
public static int b = 0;
public void run() throws Exception {
Logger log = LoggerFactory.getLogger(SimpleCronExample.class);
// log.info("------- Initializing ----------------------");
// 定义调度器
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
// log.info("------- Initialization Complete -----------");
// 获取当前时间的下一分钟
Date runTime = evenMinuteDate(new Date());
// log.info("------- Scheduling Job -------------------");
// 定义job
JobDetail job = newJob(HelloJob.class).withIdentity("job", "group1").build();
// 定义触发器,每2秒执行一次
Trigger trigger = newTrigger().withIdentity("trigger", "group1")
.withSchedule(cronSchedule("0/1 * * * * ?")).build();
// 将job注册到调度器
sched.scheduleJob(job, trigger);
// log.info(job.getKey() + " will run at: " + runTime);
// 启动调度器
sched.start();
// try {
// Thread.sleep(6L * 1000L);
// } catch (Exception e) {
// //
// }
// Trigger trigger1 = newTrigger().withIdentity("trigger1", "group1")
// .withSchedule(cronSchedule("0/1 * * * * ?")).build();
// JobDetail job1 = newJob(HelloJob1.class).withIdentity("job1", "group1").build();
// sched.scheduleJob(job1,trigger1);
// log.info("------- Started Scheduler -----------------");
// 等待1分钟
// log.info("------- Waiting 60 seconds... -------------");
try {
Thread.sleep(600L * 10000L);
} catch (Exception e) {
//
}
// 关闭调度器
log.info("------- Shutting Down ---------------------");
sched.shutdown(true);
log.info("------- Shutdown Complete -----------------");
}
public static void main(String[] args) throws Exception {
SimpleCronExample example = new SimpleCronExample();
example.run();
}
}
---05---
和spring整合。
---06--
一个触发只能由一个job,一个job可以由多个触发。
---07---
关闭淘宝订单。
mvc的标签的用法:https://www.cnblogs.com/afeng2010/p/10133797.html
---08---