1. 任务调度的2种方式
1. bean调用示例: 对应的类上加@Component或@Service注解, 类名.方法名(ryTask.ryNoParams) 类名首字母小写; 该类必须要在com.ruoyi 包下, 因为有校验
2. 权限类名调用示例: com.ruoyi.quartz.task.RyTask.ryNoParams
注意: class(类名)调用的时候, 只能调用com.ruoyi包中的类, 如果想要其它包也能调用, 则需修改配置
/*** 定时任务白名单配置(仅允许访问的包名,如其他需要可以自行添加)*/public static final String[] JOB_WHITELIST_STR = { "com.ruoyi" };
2. 默认定时任务
预留的三个定时任务
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;/*** 定时任务调度测试* * @author ruoyi*/
@Component("ryTask")
public class RyTask
{public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i){System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));}public void ryParams(String params){System.out.println("执行有参方法:" + params);}public void ryNoParams(){System.out.println("执行无参方法");}
}
开启ryNoParams()
执行效果:
执行无参方法
22:02:10.001 [quartzScheduler_Worker-7] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Preparing: insert into sys_job_log( job_name, job_group, invoke_target, job_message, status, create_time )values( ?, ?, ?, ?, ?, sysdate() )
22:02:10.001 [quartzScheduler_Worker-7] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Parameters: 系统默认(无参)(String), DEFAULT(String), ryTask.ryNoParams(String), 系统默认(无参) 总共耗时:0毫秒(String), 0(String)
22:02:10.007 [quartzScheduler_Worker-7] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - <== Updates: 1
执行无参方法
22:02:20.000 [quartzScheduler_Worker-8] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Preparing: insert into sys_job_log( job_name, job_group, invoke_target, job_message, status, create_time )values( ?, ?, ?, ?, ?, sysdate() )
22:02:20.000 [quartzScheduler_Worker-8] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Parameters: 系统默认(无参)(String), DEFAULT(String), ryTask.ryNoParams(String), 系统默认(无参) 总共耗时:0毫秒(String), 0(String)
22:02:20.012 [quartzScheduler_Worker-8] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - <== Updates: 1
开启 ryParams(String params)
执行效果:
执行有参方法:ry
22:05:15.014 [quartzScheduler_Worker-10] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Preparing: insert into sys_job_log( job_name, job_group, invoke_target, job_message, status, create_time )values( ?, ?, ?, ?, ?, sysdate() )
22:05:15.014 [quartzScheduler_Worker-10] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Parameters: 系统默认(有参)(String), DEFAULT(String), ryTask.ryParams('ry')(String), 系统默认(有参) 总共耗时:0毫秒(String), 0(String)
22:05:15.021 [quartzScheduler_Worker-10] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - <== Updates: 1
执行有参方法:ry
22:05:30.008 [quartzScheduler_Worker-1] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Preparing: insert into sys_job_log( job_name, job_group, invoke_target, job_message, status, create_time )values( ?, ?, ?, ?, ?, sysdate() )
22:05:30.008 [quartzScheduler_Worker-1] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Parameters: 系统默认(有参)(String), DEFAULT(String), ryTask.ryParams('ry')(String), 系统默认(有参) 总共耗时:0毫秒(String), 0(String)
22:05:30.021 [quartzScheduler_Worker-1] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - <== Updates: 1
开启 ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
执行效果:
执行多参方法: 字符串类型ry,布尔类型true,长整型2000,浮点型316.5,整形100
22:08:00.001 [quartzScheduler_Worker-9] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Preparing: insert into sys_job_log( job_name, job_group, invoke_target, job_message, status, create_time )values( ?, ?, ?, ?, ?, sysdate() )
22:08:00.001 [quartzScheduler_Worker-9] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Parameters: 系统默认(多参)(String), DEFAULT(String), ryTask.ryMultipleParams('ry', true, 2000L, 316.50D, 100)(String), 系统默认(多参) 总共耗时:0毫秒(String), 0(String)
22:08:00.001 [quartzScheduler_Worker-9] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - <== Updates: 1
执行多参方法: 字符串类型ry,布尔类型true,长整型2000,浮点型316.5,整形100
22:08:20.014 [quartzScheduler_Worker-10] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Preparing: insert into sys_job_log( job_name, job_group, invoke_target, job_message, status, create_time )values( ?, ?, ?, ?, ?, sysdate() )
22:08:20.014 [quartzScheduler_Worker-10] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Parameters: 系统默认(多参)(String), DEFAULT(String), ryTask.ryMultipleParams('ry', true, 2000L, 316.50D, 100)(String), 系统默认(多参) 总共耗时:0毫秒(String), 0(String)
22:08:20.018 [quartzScheduler_Worker-10] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - <== Updates: 1
3. 新增定时任务
自定义类 MyTaskTest
package com.ruoyi.ruoyiwx.myTask;import org.springframework.stereotype.Component;/*** 注意: 该类需要在com.ruoyi 包下才会生效, 因为有校验*/
@Component
public class MyTaskTest {public void myTask(){System.out.println("执行无参方法: ---------------myTask");}
}
界面上新增定时任务
执行效果:
执行无参方法: ---------------myTask
22:57:40.004 [quartzScheduler_Worker-6] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Preparing: insert into sys_job_log( job_name, job_group, invoke_target, job_message, status, create_time )values( ?, ?, ?, ?, ?, sysdate() )
22:57:40.004 [quartzScheduler_Worker-6] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Parameters: 我的定时任务(String), DEFAULT(String), myTaskTest.myTask(String), 我的定时任务 总共耗时:0毫秒(String), 0(String)
22:57:40.019 [quartzScheduler_Worker-6] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - <== Updates: 1
执行无参方法: ---------------myTask
22:57:50.003 [quartzScheduler_Worker-7] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Preparing: insert into sys_job_log( job_name, job_group, invoke_target, job_message, status, create_time )values( ?, ?, ?, ?, ?, sysdate() )
22:57:50.003 [quartzScheduler_Worker-7] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Parameters: 我的定时任务(String), DEFAULT(String), myTaskTest.myTask(String), 我的定时任务 总共耗时:0毫秒(String), 0(String)
22:57:50.013 [quartzScheduler_Worker-7] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - <== Updates: 1
4. 定时任务的执行策略
1. 立即执行: 立即执行策略会立即执行所有misfire的任务。例如,如果9点的任务misfire了,在10点15分系统恢复之后,9点和10点的misfire任务会立即执行一次。
2. 执行一次: 执行一次策略会合并部分的misfire任务,并执行下一个周期的任务。举个例子,假设9点和10点的任务都misfire了,系统在10点15分起来,只会执行一次misfire任务,然后继续按照正常的执行周期继续执行。
3. 放弃执行: 放弃执行策略会忽略所有的misfire任务,直接执行下一个周期的任务。