zoukankan      html  css  js  c++  java
  • spring scheduling 计划任务

    执行计划任务

    首先定义要执行的类

    @scheulde加在方法上,代表此方法是计划任务

    加在类上,代表所有的方法都是计划任务

    package com.example.demo.qin.scheduling;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Service;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    /**
     * @program: springboot_test
     * @description: 计划任务类
     * @author: Mr.qin
     * @create: 2018-12-19 23:24
     **/
    @Service
    public class SchedulingTaskService {
        private  static  final SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss");
        @Scheduled(fixedRate = 5000)
        public void currentTime(){
            System.out.println("每隔五秒执行一次任务"+sd.format(new Date()));
        }
    }
    

    之后在启动类上加 上@enableschuduling

    package com.example.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    @SpringBootApplication
    @EnableScheduling
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    

      

  • 相关阅读:
    windows下在yii中使用mongodb
    yii框架便利类CVarDumper使用
    64位虚拟机创建注意事项
    C#中的委托和事件
    Attribute
    NuGet安装及使用教程
    WPF+WEB+WinForm->>表现层共用类
    C#报修系统Ⅱ
    C#带小括号的运算
    工厂模式提供数据源
  • 原文地址:https://www.cnblogs.com/qin-up/p/10152964.html
Copyright © 2011-2022 走看看