SpringBoot | 第二十二章:定时任务的使用 (3)

0.编写配置类,同时启用@Async注解:

@Configuration @EnableAsync public class Config { /** * 配置线程池 * @return */ @Bean(name = "scheduledPoolTaskExecutor") public ThreadPoolTaskExecutor getAsyncThreadPoolTaskExecutor() { ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(20); taskExecutor.setMaxPoolSize(200); taskExecutor.setQueueCapacity(25); taskExecutor.setKeepAliveSeconds(200); taskExecutor.setThreadNamePrefix("oKong-Scheduled-"); // 线程池对拒绝任务(无线程可用)的处理策略,目前只支持AbortPolicy、CallerRunsPolicy;默认为后者 taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //调度器shutdown被调用时等待当前被调度的任务完成 taskExecutor.setWaitForTasksToCompleteOnShutdown(true); //等待时长 taskExecutor.setAwaitTerminationSeconds(60); taskExecutor.initialize(); return taskExecutor; } }

1.调度类上加入@Async。

@Component @Slf4j public class ScheduledTask { /** * 自动扫描,启动时间点之后5秒执行一次 */ @Async("scheduledPoolTaskExecutor") @Scheduled(fixedRate=5000) public void getCurrentDate() { log.info("Scheduled定时任务执行:" + new Date()); } }

再次启动程序,可看见控制台输出,任务已经是不同线程下执行了:

2018-08-18 22:47:13.313 INFO 14212 --- [ong-Scheduled-1] c.l.l.s.c.controller.ScheduledTask : Scheduled定时任务执行:Sat Aug 18 22:47:13 CST 2018 2018-08-18 22:47:13.343 INFO 14212 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2018-08-18 22:47:13.348 INFO 14212 --- [ main] c.l.l.s.chapter22.Chapter22Application : Started Chapter22Application in 2.057 seconds (JVM running for 2.855) 2018-08-18 22:47:13.348 INFO 14212 --- [ main] c.l.l.s.chapter22.Chapter22Application : Chapter22启动! 2018-08-18 22:47:18.308 INFO 14212 --- [ong-Scheduled-2] c.l.l.s.c.controller.ScheduledTask : Scheduled定时任务执行:Sat Aug 18 22:47:18 CST 2018 动态添加定时任务

使用注解的方式,无法实现动态的修改或者添加新的定时任务的,这个使用就需要使用编程的方式进行任务的更新操作了。可直接使用ThreadPoolTaskScheduler或者SchedulingConfigurer接口进行自定义定时任务创建。

ThreadPoolTaskScheduler

ThreadPoolTaskScheduler是SpringTask的核心实现类,该类提供了大量的重载方法进行任务调度。这里简单示例下,具体的大家自行搜索下,用的少不太了解呀。

0.创建一个ThreadPoolTaskScheduler类。

@Bean("taskExecutor") public TaskScheduler taskExecutor() { ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler(); executor.setPoolSize(20); executor.setThreadNamePrefix("oKong-taskExecutor-"); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //调度器shutdown被调用时等待当前被调度的任务完成 executor.setWaitForTasksToCompleteOnShutdown(true); //等待时长 executor.setAwaitTerminationSeconds(60); return executor; }

1.编写一个控制类,动态设置定时任务:

@Autowired TaskScheduler taskScheduler; @GetMapping("/poolTask") public String threadPoolTaskScheduler() { taskScheduler.schedule(new Runnable() { @Override public void run() { log.info("ThreadPoolTaskScheduler定时任务:" + new Date()); } }, new CronTrigger("0/3 * * * * ?"));//每3秒执行一次 return "ThreadPoolTaskScheduler!"; }

2.启动后,访问接口,即可看见控制台每3秒输出一次:

2018-08-18 23:20:39.002 INFO 9120 --- [Kong-Executor-1] c.l.l.s.c.controller.TaskController : ThreadPoolTaskScheduler定时任务:Sat Aug 18 23:20:39 CST 2018 2018-08-18 23:20:42.000 INFO 9120 --- [Kong-Executor-1] c.l.l.s.c.controller.TaskController : ThreadPoolTaskScheduler定时任务:Sat Aug 18 23:20:42 CST 2018 2018-08-18 23:20:45.002 INFO 9120 --- [Kong-Executor-2] c.l.l.s.c.controller.TaskController : ThreadPoolTaskScheduler定时任务:Sat Aug 18 23:20:45 CST 2018 2018-08-18 23:20:48.001 INFO 9120 --- [Kong-Executor-1] c.l.l.s.c.controller.TaskController : ThreadPoolTaskScheduler定时任务:Sat Aug 18 23:20:48 CST 2018 SchedulingConfigurer

此类十个接口,直接实现其configurerTasks方法即可。

0.编写配置类:

@Configuration @Slf4j public class ScheduleConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.setTaskScheduler(taskExecutor()); taskRegistrar.getScheduler().schedule(new Runnable() { @Override public void run() { log.info("SchedulingConfigurer定时任务:" + new Date()); } }, new CronTrigger("0/3 * * * * ?"));//每3秒执行一次 } @Bean("taskExecutor") public TaskScheduler taskExecutor() { ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler(); executor.setPoolSize(20); executor.setThreadNamePrefix("oKong-Executor-"); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //调度器shutdown被调用时等待当前被调度的任务完成 executor.setWaitForTasksToCompleteOnShutdown(true); //等待时长 executor.setAwaitTerminationSeconds(60); return executor; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyfxfj.html