• spring的@EnableScheduling注解


    分析

    要实现计划任务,首先通过在配置类注解@EnableScheduling来开启对计划任务的支持,然后在要执行计划任务的方法上注解@Scheduled,声明这是一个计划任务。

    Spring通过@Scheduled支持多种类型的计划任务,包含cron、fixDelay、fixRate等。

    在本示例中:
    使用cron属性可按照指定时间执行,本例写的是每天20点07分执行;

    示例

    计划任务执行类

    在这个类中的方法上需要@Scheduled注解配合@EnableScheduling使用。

    1.  
      package cn.hncu.p3.p3_taskscheduler;
    2.  
       
    3.  
      import org.springframework.scheduling.annotation.Scheduled;
    4.  
      import org.springframework.stereotype.Service;
    5.  
       
    6.  
      import java.text.SimpleDateFormat;
    7.  
      import java.util.Date;
    8.  
       
    9.  
      /**
    10.  
      * Created with IntelliJ IDEA.
    11.  
      * User: 陈浩翔.
    12.  
      * Date: 2016/11/22.
    13.  
      * Time: 下午 10:25.
    14.  
      * Explain:计划任务执行类
    15.  
      */
    16.  
      @Service
    17.  
      public class ScheduledTaskService {
    18.  
      private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    19.  
       
    20.  
      @Scheduled(fixedRate = 5000) //通过@Scheduled声明该方法是计划任务,使用fixedRate属性每隔固定时间执行
    21.  
      public void reportCurrentTime(){
    22.  
      System.out.println("每隔5秒执行一次 "+dateFormat.format(new Date()));
    23.  
      }
    24.  
       
    25.  
      @Scheduled(cron = "0 07 20 ? * *" ) //使用cron属性可按照指定时间执行,本例指的是每天20点07分执行;
    26.  
      //cron是UNIX和类UNIX(Linux)系统下的定时任务
    27.  
      public void fixTimeExecution(){
    28.  
      System.out.println("在指定时间 "+dateFormat.format(new Date())+" 执行");
    29.  
      }
    30.  
      }

    配置类

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

    1.  
      package cn.hncu.p3.p3_taskscheduler;
    2.  
       
    3.  
      import org.springframework.context.annotation.ComponentScan;
    4.  
      import org.springframework.context.annotation.Configuration;
    5.  
      import org.springframework.scheduling.annotation.EnableScheduling;
    6.  
       
    7.  
      /**
    8.  
      * Created with IntelliJ IDEA.
    9.  
      * User: 陈浩翔.
    10.  
      * Date: 2016/11/22.
    11.  
      * Time: 下午 10:32.
    12.  
      * Explain:配置类
    13.  
      */
    14.  
       
    15.  
      @Configuration
    16.  
      @ComponentScan("cn.hncu.p3.p3_taskscheduler")
    17.  
      @EnableScheduling //通过@EnableScheduling注解开启对计划任务的支持
    18.  
      public class TaskScheduleConfig {
    19.  
      }

    运行类

    1.  
      package cn.hncu.p3.p3_taskscheduler;
    2.  
       
    3.  
      import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    4.  
       
    5.  
      /**
    6.  
      * Created with IntelliJ IDEA.
    7.  
      * User: 陈浩翔.
    8.  
      * Date: 2016/11/22.
    9.  
      * Time: 下午 10:34.
    10.  
      * Explain:运行类
    11.  
      */
    12.  
      public class Main {
    13.  
      public static void main(String[] args) {
    14.  
      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskScheduleConfig.class);
    15.  
      }
    16.  
      }

    运行结果

  • 相关阅读:
    【项目】项目188
    【项目】项目190
    【项目】项目192
    【项目】项目189
    【项目】项目185
    SpringBoot统一Api接口返回格式
    MySQL判断库、表、列是否存在
    java.net.SocketException: Connection reset by peer: socket write error
    NOI Online 2022 一游
    寒假 杂题题解
  • 原文地址:https://www.cnblogs.com/YuyuanNo1/p/12511483.html
Copyright © 2020-2023  润新知