quartz 任务调度框架
简单的说:就是在特定的时间,干指定的事件,然后具体到某个对象去做
quartz初之体验:
1.pom.xml文件(导入jar包)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <dependencies> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> </dependencies> |
2. hello 创建一个任务调度
1 2 3 4 5 6 7 8 9 | public static void main(String[] args) throws Exception { //创建定时器对象 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); //开启定时器对象 scheduler.start(); System. out .println( "1234" ); //关闭定时器 scheduler.shutdown(); } |
在上面的基础上进行添加触发器和工作对象
创建一个类来实现job方法
1 2 3 4 5 6 | public class HelloJob implements Job { public void execute (JobExecutionContext context) throws JobExecutionException { System. out .println( "test01" ); } } |
使用触发器来调用工作对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public static void main(String[] args) throws Exception { // 创建定时器对象 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); // 定义一个工作对象 JobDetail job = JobBuilder.newJob(HelloJob.class) .withIdentity( "job1" , "group1" ).build(); // 定义一个触发器 Trigger withSchedule = TriggerBuilder.newTrigger() .withIdentity( "trigger1" , "group1" ).startNow() .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(1)) .build(); scheduler.scheduleJob(job, withSchedule); // 开启定时器对象 scheduler.start(); // 关闭定时器 // scheduler.shutdown(); } |
1 2 3 4 5 6 7 | 对象 TriggerBuilder 启动任务时间 startNow 立即启动 startAt ( Date ) 指定时间启动 对象 SimpleScheduleBuilder 进行简单任务重复执行 repeatSecondly …() 多少秒后重复执行 repeatminutely …() 多少分钟后重复执行 repeatHourly …() 多少小时后重复执行 |
使用上面的会产生对于时间的处理达不到理想的需求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public static void main(String[] args) throws Exception { // 创建定时器对象 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); // 定义一个工作对象 JobDetail job = JobBuilder.newJob(HelloJob.class) .withIdentity( "job1" , "group1" ).build(); // 定义一个触发器 每一秒都需要进行触发 Trigger withSchedule = TriggerBuilder.newTrigger() .withIdentity( "trigger1" , "group1" ).startNow() .withSchedule(CronScheduleBuilder.cronSchedule( "* * * * * ?" )) .build(); scheduler.scheduleJob(job, withSchedule); // 开启定时器对象 scheduler.start(); // 关闭定时器 // scheduler.shutdown(); } |
使用日历的模式进行处理 这样就会是的我们对于定时的时间可以实现我们的大部分的需求
那些字符代表什么意思呢 从位数上讲
1 2 3 4 5 6 7 | 1. Seconds 秒 2. Minutes 分钟 3. Hours 小时 4. Day - of - Month 月中的天 5. Month 月 6. Day - of -Week 周中的天 7. Year (optional field) 年(可选的域) |
其中使用通配符来进行设置(*,/,?,L,w,#)
|
|
||||
'/' 字符 |
表示值的增量,例如, 如果分钟域中放入'0/15',它表示“每隔 15 分钟, |
||||
'?'字符 |
可以用在 day-of-month 及 day-of-week 域中,它用来表示“没有指定值”。 |
||||
'L'字符 |
可以在 day-of-month 及 day-of-week 中使用,这个字符是"last"的简写, |
||||
'W' 字符 |
用来指定距离给定日最接近的周几(在 day-of-week 域中指定)。例如: |
||||
'#'字符 |
表示表示月中的第几个周几。例如:day-of-week 域中的"6#3" 或者 "FRI#3" |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步