在开发中经常遇到一些定时任务,比如到点自动爬取数据、到点自动执行清除数据库信息之类,使用spring的Schedule功能就可以实现需求。
快速入门:从https://cron.qqe2.com/里面根据自己的需求获取cron表达式
编写对应的代码:
在启动类上面加一个注解:
执行效果:
相关参数: 多重任务(开启线程池):如果需要多个定时任务需要重写SchedulingConfigurer接口:
package com.github.binarywang.demo.wx.mp.config; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.config.ScheduledTaskRegistrar; @Configuration public class ScheduledConfig implements SchedulingConfigurer { private static final int TASK_POOL_SIZE = 5; private static final String TASK_THREAD_PREFIX = "cron-task-"; @Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { ThreadPoolTaskScheduler taskPool = new ThreadPoolTaskScheduler(); taskPool.setPoolSize(TASK_POOL_SIZE); taskPool.setThreadNamePrefix(TASK_THREAD_PREFIX); taskPool.initialize(); scheduledTaskRegistrar.setTaskScheduler(taskPool); } }
测试代码:
package com.github.binarywang.demo.wx.mp.utils; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Slf4j @Component public class myCron { @Scheduled(cron = "0/5 * * * * ? ") public void saying() { log.debug("========= 这里是定时任务 ================"); } @Scheduled(cron = "0/7 * * * * ? ") public void saying2() { log.error("========= 7秒的这里是定时任务 ================"); } @Scheduled(cron = "10 * * * * ? ") public void saying3() { log.warn("========= 10秒的这里是定时任务 ================"); } }
效果:
2021-10-24 10:05:42.004 ERROR 8684 --- [ cron-task-2] c.g.binarywang.demo.wx.mp.utils.myCron : ========= 7秒的这里是定时任务 ================ 2021-10-24 10:05:45.001 DEBUG 8684 --- [ cron-task-3] c.g.binarywang.demo.wx.mp.utils.myCron : ========= 这里是定时任务 ================ 2021-10-24 10:05:49.006 ERROR 8684 --- [ cron-task-1] c.g.binarywang.demo.wx.mp.utils.myCron : ========= 7秒的这里是定时任务 ================ 2021-10-24 10:05:50.004 DEBUG 8684 --- [ cron-task-4] c.g.binarywang.demo.wx.mp.utils.myCron : ========= 这里是定时任务 ================ 2021-10-24 10:05:55.002 DEBUG 8684 --- [ cron-task-4] c.g.binarywang.demo.wx.mp.utils.myCron : ========= 这里是定时任务 ================ 2021-10-24 10:05:56.007 ERROR 8684 --- [ cron-task-3] c.g.binarywang.demo.wx.mp.utils.myCron : ========= 7秒的这里是定时任务 ================ 2021-10-24 10:06:00.010 DEBUG 8684 --- [ cron-task-3] c.g.binarywang.demo.wx.mp.utils.myCron : ========= 这里是定时任务 ================ 2021-10-24 10:06:00.010 ERROR 8684 --- [ cron-task-2] c.g.binarywang.demo.wx.mp.utils.myCron : ========= 7秒的这里是定时任务 ================ 2021-10-24 10:06:05.006 DEBUG 8684 --- [ cron-task-3] c.g.binarywang.demo.wx.mp.utils.myCron : ========= 这里是定时任务 ================ 2021-10-24 10:06:07.004 ERROR 8684 --- [ cron-task-5] c.g.binarywang.demo.wx.mp.utils.myCron : ========= 7秒的这里是定时任务 ================ 2021-10-24 10:06:10.002 DEBUG 8684 --- [ cron-task-4] c.g.binarywang.demo.wx.mp.utils.myCron : ========= 这里是定时任务 ================ 2021-10-24 10:06:10.002 WARN 8684 --- [ cron-task-1] c.g.binarywang.demo.wx.mp.utils.myCron : ========= 10秒的这里是定时任务 ================ 2021-10-24 10:06:14.005 ERROR 8684 --- [ cron-task-2] c.g.binarywang.demo.wx.mp.utils.myCron : ========= 7秒的这里是定时任务 ================ 2021-10-24 10:06:15.012 DEBUG 8684 --- [ cron-task-3] c.g.binarywang.demo.wx.mp.utils.myCron : ========= 这里是定时任务 ================ 2021-10-24 10:06:20.012 DEBUG 8684 --- [ cron-task-3] c.g.binarywang.demo.wx.mp.utils.myCron : ========= 这里是定时任务 ================ 2021-10-24 10:06:21.014 ERROR 8684 --- [ cron-task-5] c.g.binarywang.demo.wx.mp.utils.myCron : ========= 7秒的这里是定时任务 ================ 2021-10-24 10:06:25.004 DEBUG 8684 --- [ cron-task-1] c.g.binarywang.demo.wx.mp.utils.myCron : ========= 这里是定时任务 ================ 2021-10-24 10:06:27.864 INFO 8684 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' Process finished with exit code 130
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)