- 今日大纲
- 学习Quartz
- 基于Quartz实现定时关闭超时未付款的订单
- 基于Solr实现商品的搜索
- 爬虫抓到的京东的商品数据、图片
- Quartz
- 大纲
- Quartz是什么?
- 简单的一些例子
- Quartz框架学习
- Quartz是什么
简单的认为Quartz是是一个定时器。
-
- 下载
- http://www.quartz-scheduler.org/downloads
-
- 核心接口
-
- Tigger
- SimpleTrigger
- 简单的触发
- CronTrigger
- 表达式触发
-
-
- SimpleTrigger
-
-
-
- CronTrigger
-
-
- Cron Expressions
-
- 表达式生成工具
这个工具不完美:
- 缺少秒
- 缺少L、W表达式
- 学习Quartz
- 导入quartz
-
- 依赖
-
- 示例
- Job
- 示例
public class HelloJob implements Job {
private static Logger _log = LoggerFactory.getLogger(HelloJob.class);
/**
*
* Empty constructor for job initilization
*
*
* Quartz requires a public empty constructor so that the
* scheduler can instantiate the class whenever it needs.
*
*/
public HelloJob() {
}
/**
*
* Called by the {@link org.quartz.Scheduler}
when a
* {@link org.quartz.Trigger}
fires that is associated with
* the Job
.
*
* @throws JobExecutionException
* if there is an exception while executing the job.
*/
public void execute(JobExecutionContext context)
throws JobExecutionException {
// Say Hello to the World and display the date/time
_log.info("Hello World! - " + new Date());
}
}
-
-
- 简单触发示例
-
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();
}
}
-
-
- 表达式触发示例
-
public class SimpleCronExample {
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("job1", "group1").build();
// 定义触发器,每2秒执行一次
Trigger trigger = newTrigger().withIdentity("trigger1", "group1")
.withSchedule(cronSchedule("0/5 * * * * ?")).build();
// 将job注册到调度器
sched.scheduleJob(job, trigger);
log.info(job.getKey() + " will run at: " + runTime);
// 启动调度器
sched.start();
log.info("------- Started Scheduler -----------------");
// 等待1分钟
log.info("------- Waiting 60 seconds... -------------");
try {
Thread.sleep(60L * 1000L);
} 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();
}
}
-
- 通过Spring使用Quartz
- 导入依赖
- 通过Spring使用Quartz
-
-
- 编写Job
-
-
-
- 编写配置文件文
-
- 定义job
- 定义触发
- 定义调度器,并且注册触发
-
- 启动Spring容器(启动调度器)
-
-
- 触发和job之间关系
一个触发能都多个job吗? 不能!
一个job能有多个触发吗? 是的!
- 关闭超时2天未付款的订单
- 分析
- 在订单系统中导入相关的依赖
- 定义job
- 扫描订单表,修改订单的状态为关闭状态
- 扫描条件:创建时间
关注打赏
- ArrayList c.toArray might (incorrectly) not return Object[] (see 6260652)
- Spring框架实体bean转json返回前端报错:Null key for a Map not allowed in JSON (use a converting NullKeySerializer
- python将数据写入txt文本文件
- Python学习随笔:使用xlwings读取和操作Execl文件
- org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized t
- ‘gbk‘ codec can‘t decode byte 0x80 in position 2: illegal multibyte sequence
- SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3: truncated \UX
- 关于Mybatis中keyProperty属性
- 解决 Mybatis 报错 org.apache.ibatis.ognl.NoSuchPropertyException: XXXCriteria$Criterion.noValue
- mysql报错 is longer than the server configured value of ‘wait_timeout‘