zoukankan      html  css  js  c++  java
  • SpringBoot定时任务Schedule使用

    在开发中很多时候会用到定时任务, 以前用自定义类继承***TimerTask***

    public class CustomTask extends TimerTask{
    	@Override
    	public void run() {
    		// 执行业务代码
    	}
    }
    
    class Main {
    	public static void main(String[] args) {
    		// 调用
    		Timer timer= new Timer();
    		Calendar calendar= Calendar.getInstance(); // 设置定时时间, 当然还有其他方式
    		calendar.set(Calendar.MINUTE, 0);
    		calendar.set(Calendar.SECOND, 0);
    		calendar.set(Calendar.MILLISECOND, 0);
    		calendar.scheduleAtFixedRate(new CustomTask(),
    		timer.getTime(), 1000 * 60 * 10);
    	}
    }
    

    Timer和TimerTask详解:https://blog.csdn.net/xieyuooo/article/details/8607220

    下面进入正题, SpringBoot中优雅使用定时任务

    • 添加支持
      在SpringBoot的启动类①中添加注解 @EnableScheduling
    @SpringBootApplication
    @EnableScheduling  // ①
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
    • 使用定时任务
    /**
     * 检查小时均值报警
     */
     @Scheduled(cron = "0 1 * * * *")
     public void overproofAlert() {
    	 log.info("-----开始执行定时任务-----");
     }
    

    通过上面这一个注解 *@Scheduled(cron = "0 1 * * * ") 这样就可以开启定时任务了, 惊不惊喜!

    Cron表达式说明 详细说明

    Cron是字符串表达式, 并由’域’和空格组成。

    模版: Seconds Minutes Hours DayOfMonth Month DayOfWeek Year

    Year可选

    Sencods


    这样创建的定时任务是同步的,即顺序执行。 这会遇到一个问题,当某个任务中断后会阻塞掉后面的任务, 导致其他任务‘失效’。所以配置异步是非常有必要的, 步骤如下:

    • 添加Config
    @Configuration
    @EnableAsync // ① 添加注解
    public class AsyncConfig {
        @Bean
        public Executor taskExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(10);  
            executor.setMaxPoolSize(200);
            executor.setQueueCapacity(10);
            executor.initialize();
            return executor;
        }
    }
    
    • 在定时方法上添加 @Async 注解
    @Scheduled(cron = "0 0/1 * * * *")
    @Async   // ② 
    public void oneMinuteTask() {
    	log.info("-----开始执行定时任务-----");
    }
    

    大功告成 !

    有梦为马,游历天涯!
  • 相关阅读:
    3D Slicer 体系结构2
    3D Slicer 体系结构1
    3D Slicer FAQ-3
    3D Slicer FAQ-2
    3D Slicer FAQ-1
    3D Slicer 编译、调试、规范化的开发
    3D Slicer 开发者必晓ABC
    3D Slicer 模块管理(二)场景视图、体数据、体绘制
    3D Slicer 模块管理(一)颜色、DCM、数据、模型、注释
    3D Slicer 数据与帮助
  • 原文地址:https://www.cnblogs.com/qijianguo/p/10180874.html
Copyright © 2011-2022 走看看