zoukankan      html  css  js  c++  java
  • spring boot 使用Schedule创建轻量级定时任务

    Scheduled

    SpringBoot配置定时任务可以直接使用自带的Scheduled,这相当于一个轻量级的Quartz,它可以让我们直接使用注解来完成定时任务的配置。

    Scheduled调度时间设置说明

    使用Scheduled设置任务调度时间有以下几种方式,可根据实际情况选取一种即可:

    //通过fixedRate属性,来设置上一次调度开始后再次调度的延时,fixedRate值的单位为毫秒,此例为每5秒执行1次
    //注意:fixedRate只要到达间隔时间就会调度执行,不关心上次任务是否已执行完成,这样就会存在重复执行的风险!
    @Scheduled(fixedRate = 5000)
    
    //该属性的功效与上面的fixedRate则是相反的,配置了该属性后会等到任务方法执行完成后,达到延迟配置的时间再次执行该方法
    @Scheduled(fixedDelay = 5000)
    
    //通过cron属性,使用Cron表达式来设置执行时间,此例为每10秒执行1次
    //在线Cron表达式生成器:http://cron.qqe2.com/
    @Scheduled(cron="*/10 * * * * ?")

    别外,可以通过initialDelay属性来设置第一次执行的延迟时间,只是做延迟的设定,并不会控制其他逻辑,需要配合fixedDelay或者fixedRate来使用

    //第一次延迟10秒执行,之后每间隔5秒执行一次
    @Scheduled(fixedDelay = 5000, initialDelay = 10000)

    调度任务(定时任务)实现类

    调度任务1

    package Scheduler;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Service;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    @Service
    public class SchedulingTask1 {
    
        private Integer count=0;
        private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        //通过fixedRate属性,来设置上一次调度开始后再次调度的延时,fixedRate值的单位为毫秒,此例为每5秒执行1次
        @Scheduled(fixedRate = 5000)
        private void process(){
            //输出
            System.out.println(String.format("第%s次执行任务SchedulingTask1 时间:%s", (++count).toString(), dateFormat.format(new Date())));
        }
    }

    调度任务2

    package Scheduler;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Service;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    @Service
    public class SchedulingTask2 {
    
        private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        //通过cron属性,使用Cron表达式来设置执行时间,此例为每10秒执行1次
        @Scheduled(cron="*/10 * * * * ?")
        private void process(){
            //输出
            System.out.println(String.format("SchedulingTask2执行... 时间:%s", dateFormat.format(new Date())));
        }
    }

    也可以将SchedulingTask2类中的方法写在SchedulingTask1类中。

    调度配置类

    @ComponentScan注解的作用就是根据定义的扫描路径,把符合扫描规则的类装配到spring容器中

    @EnableScheduling注解开启对计划任务的支持

    package Scheduler;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    @Configuration
    @ComponentScan("Scheduler")
    @EnableScheduling //通过@EnableScheduling注解开启对计划任务的支持
    public class SchedulerConfig {
    }

    启动类

    使用AnnotationConfigApplicationContext可以实现基于Java的配置类(包括各种注解)加载Spring的应用上下文。避免使用application.xml进行配置。相比XML配置,更加便捷。

    package com.lgt.demo2;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import Scheduler.SchedulerConfig;
    
    @SpringBootApplication
    public class Demo2Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Demo2Application.class, args);
            AnnotationConfigApplicationContext context =
                    new AnnotationConfigApplicationContext(SchedulerConfig.class);
        }
    
    }

    输出结果

    第1次执行任务SchedulingTask1 时间:2019-04-15 13:41:33
    第2次执行任务SchedulingTask1 时间:2019-04-15 13:41:38
    SchedulingTask2执行... 时间:2019-04-15 13:41:40
    第3次执行任务SchedulingTask1 时间:2019-04-15 13:41:43
    第4次执行任务SchedulingTask1 时间:2019-04-15 13:41:48
    SchedulingTask2执行... 时间:2019-04-15 13:41:50
    第5次执行任务SchedulingTask1 时间:2019-04-15 13:41:53
    第6次执行任务SchedulingTask1 时间:2019-04-15 13:41:58
    SchedulingTask2执行... 时间:2019-04-15 13:42:00
  • 相关阅读:
    Docker宿主机管理
    Docker常用命令
    Maven专题4——Maven测试
    Spring Boot 2.x 之 Logging
    spark高可用集群搭建立
    elastic插件安装
    单实例安装elastic和启动报错解决
    使用Turbine对集群进行监控
    Centos安装mysql5.6.33
    Centos6安装破解JIRA7.3.8
  • 原文地址:https://www.cnblogs.com/codecat/p/10710206.html
Copyright © 2011-2022 走看看