zoukankan      html  css  js  c++  java
  • 16点睛Spring4.1-TaskScheduler

    转发:https://www.iteye.com/blog/wiselyman-2213049

    16.1 TaskScheduler

    • 提供对计划任务提供支持;
    • 使用@EnableScheduling开启计划任务支持
    • 使用@Scheduled来注解计划任务的方法;

    16.2 示例

    演示后台间断执行任务和定时计划任务

    16.2.1 计划任务的配置

    @Configuration
    @EnableScheduling
    public class DemoConfig {
    
    }
    

    16.2.2 计划配置任务类

    package com.wisely.task.scheduler;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Component
    public class DemoScheduledTask {
    
      private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    
      @Scheduled(fixedRate = 5000) //每五秒执行一次
      public void reportCurrentTime() {
           System.out.println("每隔五秒执行一次 " + dateFormat.format(new Date()));
       }
    
      @Scheduled(cron = "0 22 11 ? * *"  ) //每天上午11点22执行
      public void fixTimeExecution(){
          System.out.println("在指定时间 " + dateFormat.format(new Date())+"执行");
      }
    
    
    
    }
    

    16.2.3 测试

    package com.wisely.task.scheduler;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class Main {
    
        @SuppressWarnings({ "unused", "resource" })
        public static void main(String[] args) {
            AnnotationConfigApplicationContext context =
                    new AnnotationConfigApplicationContext("com.wisely.task.scheduler");
    
        }
    
    }
    

    输出结果

    每隔五秒执行一次 11:21:42
    每隔五秒执行一次 11:21:47
    每隔五秒执行一次 11:21:52
    每隔五秒执行一次 11:21:57
    在指定时间 11:22:00执行
    每隔五秒执行一次 11:22:02
  • 相关阅读:
    MySQL调优篇 | 逻辑架构解读(1)
    SQLPlus 在连接时通常有四种方式
    Oracle解决索引碎片功能
    windows2003 ftp 无法下载 解决
    bat记录
    ACCESS字符串操作函数
    缓存和RAID如何提高磁盘IO性能
    TortoiseSVN 命令 (命令行执行工具)
    在RHEL5下实现磁盘分区和磁盘配额
    PS 命令详解
  • 原文地址:https://www.cnblogs.com/Jeely/p/11949994.html
Copyright © 2011-2022 走看看