• springboot中定时任务


     1 import java.text.SimpleDateFormat;
     2 import java.util.Date;
     3 
     4 import org.springframework.context.annotation.Configuration;
     5 import org.springframework.scheduling.annotation.EnableScheduling;
     6 import org.springframework.scheduling.annotation.Scheduled;
     7 import org.springframework.stereotype.Component;
     8 
     9 /**
    10  * 测试spring定时任务执行
    11  */
    12 @Component
    13 @Configuration
    14 @EnableScheduling
    15 public class MyTestTask {
    16     private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    17 
    18     @Scheduled(cron="* * * * * *") // 关于定时任务执行时间,后续会在别的文章中添加
    19     public void executeUpdateYqTask() {
    20         System.out.println(Thread.currentThread().getName() + " >>> task one " + format.format(new Date()));
    21     }
    22 
    23     @Scheduled(cron="* * * * * *")
    24     public void executeRepaymentTask() throws InterruptedException {
    25         System.out.println(Thread.currentThread().getName() + " >>> task two " + format.format(new Date()));
    26     }
    27 }
     1 import java.util.concurrent.Executor;
     2 
     3 import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
     4 import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
     5 import org.springframework.context.annotation.Bean;
     6 import org.springframework.context.annotation.Configuration;
     7 import org.springframework.scheduling.TaskScheduler;
     8 import org.springframework.scheduling.annotation.AsyncConfigurer;
     9 import org.springframework.scheduling.annotation.EnableScheduling;
    10 import org.springframework.scheduling.annotation.SchedulingConfigurer;
    11 import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
    12 import org.springframework.scheduling.config.ScheduledTaskRegistrar;
    13 
    14 @Configuration
    15 @EnableScheduling
    16 public class ScheduleConfig implements SchedulingConfigurer, AsyncConfigurer{
    17 
    18     /** 异步处理 */
    19     public void configureTasks(ScheduledTaskRegistrar taskRegistrar){
    20         TaskScheduler taskScheduler = taskScheduler();
    21         taskRegistrar.setTaskScheduler(taskScheduler);
    22     }
    23 
    24     /** 定时任务多线程处理 */
    25     @Bean(destroyMethod = "shutdown")
    26     public ThreadPoolTaskScheduler taskScheduler(){
    27         ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    28         scheduler.setPoolSize(20);
    29         scheduler.setThreadNamePrefix("task-");
    30         scheduler.setAwaitTerminationSeconds(60);
    31         scheduler.setWaitForTasksToCompleteOnShutdown(true);
    32         return scheduler;
    33     }
    34 
    35     /** 异步处理 */
    36     public Executor getAsyncExecutor(){
    37         Executor executor = taskScheduler();
    38         return executor;
    39     }
    40 
    41     /** 异步处理 异常 */
    42     public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler(){
    43         return new SimpleAsyncUncaughtExceptionHandler();
    44     }
    45 }

    最后:

    在启动类加上    @EnableScheduling 就可以了

    1 /**
    2  * 启动类
    3  */
    4 @SpringBootApplication
    5 @EnableScheduling
    6 public class AppApplication extends SpringBootServletInitializer {}
  • 相关阅读:
    灵魂拷问!浏览器输入「xxxxhub」的背后.....
    趣谈 DHCP 协议,有点意思。
    规约模式,颤抖吧产品经理!再也不怕你乱改需求了
    订阅者模式,公众号、B站、快手用了都说好!
    设计模式,你相信吗,只用两个函数实现事务!
    Linux系统中安装Jenkins
    Linux系统在线安装和查看git版本
    VIM中的保存和退出命令
    @JsonProperty爆红
    手动运行jar包
  • 原文地址:https://www.cnblogs.com/wdpnodecodes/p/8109108.html
Copyright © 2020-2023  润新知