zoukankan      html  css  js  c++  java
  • springboot:定时任务

    在日常的开发过程中经常使用到定时任务,在springMVC的开发中,经常和quartz框架进行集成使用,但在springboot中没有这么做,而是使用了java的线程池来实现定时任务。

    一、概述

     在springboot中使用定时任务非常简单,只需要简单的几步即可完成。

    二、详述

    在springboot中要使用定时任务,首先要保证环境是springboot的,这里使用的是springboot-2.1.2.release版本。在启动类上加@EnableScheduling注解,如下,

    package com.example.demo;
    
    import com.example.demo.properties.ApplicationPro;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    @SpringBootApplication
    @EnableConfigurationProperties({ApplicationPro.class})
    //引入开启定时任务的注解
    @EnableScheduling
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }

    @EnableScheduling注解的作用就是开启对定时任务的支持,这个注解的作用是开启定时任务的自动配置。

    在使用了@EnableScheduling注解后便可以编写具体的定时任务的job类,该job类无需继承或实现任何接口,只要是一个被spring管理的类即可。为了使spring可以管理统一使用@Component注解标识。在定时任务的类中的方法上标识@Scheduled注解便可以定时执行该方法,@Scheduled注解上有几种不同的属性,看具体的该注解的定义,

    fixedDelay

    @Scheduled(fixedDelay=1000)/@Scheduled(fixedDelay="1000")的意思是该方法执行完后每隔1000ms执行一次。看具体的代码

    package com.example.demo.job;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    @Slf4j
    @Component
    public class SchedulFixedRelayTest {
    
        //@Scheduled(fixedDelay = 5000)
        public  void jobTest(){
    
            try {
                log.info("使用fixedDelay的定时任务");
                Thread.sleep(10*1000);
    
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
        }
    
    
    }

    看执行结果,

    2020-12-09 22:02:47.511  INFO 7368 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 940 ms
    2020-12-09 22:02:47.681  INFO 7368 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
    2020-12-09 22:02:47.782  INFO 7368 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
    2020-12-09 22:02:47.802  INFO 7368 --- [   scheduling-1] c.e.demo.job.SchedulFixedRelayTest       : 使用fixedDelay的定时任务
    2020-12-09 22:02:47.820  INFO 7368 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2020-12-09 22:02:47.823  INFO 7368 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 1.627 seconds (JVM running for 2.51)
    2020-12-09 22:03:02.819  INFO 7368 --- [   scheduling-1] c.e.demo.job.SchedulFixedRelayTest       : 使用fixedDelay的定时任务
    2020-12-09 22:03:17.834  INFO 7368 --- [   scheduling-1] c.e.demo.job.SchedulFixedRelayTest       : 使用fixedDelay的定时任务

    看上面打印的执行时间,第一次在2020-12-09 22:02:47,由于程序会睡眠10秒,也就是说回在22:02:57执行完,那么下次执行应该在22:03:02,看第二次打印的时间刚好和上面的一样,那就证明了,该配置是在方法执行完成后每隔XX秒执行一次。

    fixedRate

    @Scheduled(fixedRate=1000)/@Scheduled(fixedRate="1000")的意思是该方法执行完后每隔1000ms执行一次,但是如果任务执行的时间超过了配置的时间,则在任务执行完会再次执行。

    任务时间小于配置的时间

    package com.example.demo.job;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Slf4j
    @Component
    public class SchedulFixedRateTest {
    
        @Scheduled(fixedRate = 5000)
        public  void jobTest(){
    
            try {
                log.info("使用fixedRate的定时任务");
                Thread.sleep(1*1000);
    
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
        }
    
    
    }

    任务会睡眠1s也就是任务耗时1s,fixedRate配置的是每隔5s,由于任务时间小于配置的时间,所以会每隔5s执行一次,看执行结果,

    2020-12-09 22:16:16.156  INFO 2800 --- [   scheduling-1] c.example.demo.job.SchedulFixedRateTest  : 使用fixedRate的定时任务
    2020-12-09 22:16:16.188  INFO 2800 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2020-12-09 22:16:16.188  INFO 2800 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 1.625 seconds (JVM running for 2.462)
    2020-12-09 22:16:21.178  INFO 2800 --- [   scheduling-1] c.example.demo.job.SchedulFixedRateTest  : 使用fixedRate的定时任务
    2020-12-09 22:16:26.180  INFO 2800 --- [   scheduling-1] c.example.demo.job.SchedulFixedRateTest  : 使用fixedRate的定时任务

    看打印的任务时间都是每隔5s执行一次。

    任务时间大于配置的时间

    看任务的执行时间打印配置的时间的情况,

    package com.example.demo.job;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Slf4j
    @Component
    public class SchedulFixedRateTest {
    
        @Scheduled(fixedRate = 5000)
        public  void jobTest(){
    
            try {
                log.info("使用fixedRate的定时任务");
                Thread.sleep(10*1000);
    
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
        }
    
    
    }

    这里任务执行10s,配置的时间为5s。看执行结果,

    2020-12-09 22:17:44.070  INFO 12952 --- [   scheduling-1] c.example.demo.job.SchedulFixedRateTest  : 使用fixedRate的定时任务
    2020-12-09 22:17:44.102  INFO 12952 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2020-12-09 22:17:44.102  INFO 12952 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 1.626 seconds (JVM running for 2.462)
    2020-12-09 22:17:54.071  INFO 12952 --- [   scheduling-1] c.example.demo.job.SchedulFixedRateTest  : 使用fixedRate的定时任务
    2020-12-09 22:18:04.085  INFO 12952 --- [   scheduling-1] c.example.demo.job.SchedulFixedRateTest  : 使用fixedRate的定时任务

    从上面的结果可以看到任务是每隔10s执行一次,由于任务耗时大于了配置的时长,所以任务执行完以后便会进入下次的执行。

    cron

    cron表达式共7位,分别是秒、分、小时、日、月、周、年。cron表达式可以从网上找,如下,

    https://cron.qqe2.com/

    通过cron在线生成,分别设置值,如,0/5 * * * * ?  每隔5s执行一次,

    package com.example.demo.job;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Slf4j
    @Component
    public class SchedulCronTest {
    
        @Scheduled(cron = "0/5 * * * * ?")
        public  void jobTest(){
    
            try {
                log.info("使用cron的定时任务");
                Thread.sleep(10*1000);
    
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
        }
    
    
    }

    每隔5s执行一次,任务耗时10s,看下执行结果,

    2020-12-09 22:41:42.718  INFO 17828 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 1.609 seconds (JVM running for 2.453)
    2020-12-09 22:41:45.015  INFO 17828 --- [   scheduling-1] com.example.demo.job.SchedulCronTest     : 使用cron的定时任务
    2020-12-09 22:42:00.004  INFO 17828 --- [   scheduling-1] com.example.demo.job.SchedulCronTest     : 使用cron的定时任务
    2020-12-09 22:42:15.000  INFO 17828 --- [   scheduling-1] com.example.demo.job.SchedulCronTest     : 使用cron的定时任务

    可以看到是每隔15s执行一次,也就是如果配置的时间间隔小于任务耗时,那么在任务执行完后的时间间隔后再执行,在此种情况下和fixDelay的用法一致。

    三、总结

    本文分析了springboot中定时任务的使用,

    首先,使用@EnableScheduling开启定时任务的自动配置;

    其次,任务类必须受spring管理(使用@Component、@Service等注解均可);

    最后,任务方法使用@Scheduled注解标识,该注解有3中不同的属性配置,fixedDelay、fixedRate、cron;

    有不正之处,欢迎指正,感谢!

  • 相关阅读:
    crawler_URL编码原理详解
    linux_常用压缩,解压缩命令
    myql_链接丢失异常_mybaits _等框架_报错_The last packet successfully
    linux_shell_类似sql的orderby 取最大值
    php_cawler_html嵌套标签清洗
    vim_编码配置文件_utf8乱码解决
    python_random随机
    linux_shell_轮询触发启动脚本
    crawler_http关闭连接
    linux_mac_配置itrem2 rz sz_bug处理
  • 原文地址:https://www.cnblogs.com/teach/p/14106194.html
Copyright © 2011-2022 走看看