0


SpringBoot项目中@Scheduled定时任务配置线程池

SpringBoot项目中定时任务配置线程池

使用spring的定时器 @Scheduled 的话,因为 @Scheduled 默认是单线程执行的,所以在需要的时候,我们可以设置一个线程池去执行定时任务。

1 在启动类上加入@EnableScheduling注解

importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.scheduling.annotation.EnableScheduling;importjava.net.InetAddress;importlombok.extern.slf4j.Slf4j;@EnableScheduling@SpringBootApplication@Slf4jpublicclassSynchronizationApplication{publicstaticvoidmain(String[] args)throwsUnknownHostException{SpringApplication app =newSpringApplication(SynchronizationApplication.class);//同名Bean允许覆盖,不设置时默认为true
        app.setAllowBeanDefinitionOverriding(true);Environment env = app.run(args).getEnvironment();//服务名称String appName = env.getProperty("spring.application.name");//端口号String port = env.getProperty("server.port");//服务对外ip地址String ip =InetAddress.getLocalHost().getHostAddress();}}

2 通过实现SchedulingConfigurer接口来将定时线程池放入

importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.scheduling.TaskScheduler;importorg.springframework.scheduling.annotation.SchedulingConfigurer;importorg.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;importorg.springframework.scheduling.config.ScheduledTaskRegistrar;importjava.util.concurrent.ArrayBlockingQueue;importjava.util.concurrent.ThreadFactory;importjava.util.concurrent.ThreadPoolExecutor;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.atomic.AtomicInteger;@ConfigurationpublicclassTaskConfigimplementsSchedulingConfigurer{@BeanpublicTaskSchedulertaskScheduler(){ThreadPoolTaskScheduler executor =newThreadPoolTaskScheduler();
        executor.setPoolSize(10);
        executor.setThreadNamePrefix("task-thread");//设置饱和策略//CallerRunsPolicy:线程池的饱和策略之一,当线程池使用饱和后,直接使用调用者所在的线程来执行任务;如果执行程序已关闭,则会丢弃该任务
        executor.setRejectedExecutionHandler(newThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();return executor;}//配置@Scheduled 定时器所使用的线程池//配置任务注册器:ScheduledTaskRegistrar 的任务调度器@OverridepublicvoidconfigureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar){//可配置两种类型:TaskScheduler、ScheduledExecutorService//scheduledTaskRegistrar.setScheduler(taskScheduler());//只可配置一种类型:taskScheduler
        scheduledTaskRegistrar.setTaskScheduler(taskScheduler());}}
线程池的饱和策略:

如果当前同时运行的线程数量达到最大线程数量并且队列也已经被放满了,线程池会执行饱和策略。

  • ThreadPoolExecutor.AbortPolicy:抛出 RejectedExecutionException来拒绝新任务的处理。
  • ThreadPoolExecutor.CallerRunsPolicy:当线程池使用饱和后,直接使用调用者所在的线程来执行任务;如果执行程序已关闭,则会丢弃该任务。
  • ThreadPoolExecutor.DiscardPolicy:不处理新任务,直接丢弃掉。
  • ThreadPoolExecutor.DiscardOldestPolicy: 此策略将丢弃最早的未处理的任务请求。

3 编写定时任务

cron在线生成网址

importlombok.extern.slf4j.Slf4j;importorg.springframework.scheduling.annotation.Scheduled;importorg.springframework.stereotype.Component;//注册为spring容器的组件@Component@Slf4jpublicclassSchedulerTask{//定时任务// 5 * * * * ? 在每分钟的5秒执行@Scheduled(cron =" 5 * * * * ? ")publicvoidscheduleTask(){try{
            log.info("定时任务: 开始执行");//todo:执行业务
            log.info("定时任务: 执行完毕");}catch(Exception e){
            log.error("定时任务执行出错", e);}}
标签: spring boot java spring

本文转载自: https://blog.csdn.net/m0_46440313/article/details/126360644
版权归原作者 恺阿恺 所有, 如有侵权,请联系我们删除。

“SpringBoot项目中@Scheduled定时任务配置线程池”的评论:

还没有评论