zoukankan      html  css  js  c++  java
  • SpringBoot-定时任务

    springboot-定时任务

    everthing will be okay....

    实现结果如下:

    UTOOLS1602489797623.png

    1. 添加依赖包
    <dependencies>
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter</artifactId>
    	</dependency>
    </dependencies>
    
    1. 添加相关注释

      @SpringBootApplication
      @EnableScheduling
      public class Application {
      
      	public static void main(String[] args) {
      		SpringApplication.run(Application.class, args);
      	}
      }
      
    2. 定时任务实现(CRON表达式与自定义时间)

      package com.empirefree.component;
      
      import org.springframework.scheduling.annotation.Scheduled;
      import org.springframework.stereotype.Component;
      
      /**
       * @ProjectName: SchedulerTask
       * @Package: com.empirefree.component
       * @Description:
       * @Author: huyuqiao
       * @CreateDate: 2020/10/12 16:01
       */
      @Component
      public class SchedulerTask {
      
          private int count=0;
      
          @Scheduled(cron="*/6 * * * * ?")
          private void process(){
              System.out.println("this is scheduler task runing  "+(count++));
          }
      
      }
      
      package com.empirefree.component;
      
      import org.springframework.scheduling.annotation.Scheduled;
      import org.springframework.stereotype.Component;
      
      import java.text.SimpleDateFormat;
      import java.util.Date;
      
      /**
       * @ProjectName: Scheduler2Task
       * @Package: com.empirefree.component
       * @Description:
       * @Author: huyuqiao
       * @CreateDate: 2020/10/12 16:01
       */
      @Component
      public class Scheduler2Task {
      
          private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
      
          @Scheduled(fixedRate = 6000)
          public void reportCurrentTime() {
              System.out.println("现在时间:" + dateFormat.format(new Date()));
          }
      
      }
      
  • 相关阅读:
    模拟实现ajax加载框
    京东晒单的js实现
    微信分享js代码(转载)
    css模拟实现selec下拉框
    网页端实现输入卡号四字隔开
    返回顶部的动态显示与隐藏
    js等比例缩放图片(转载)
    css模拟实现checkbox
    quartz的使用 Mr
    Spring声明式事务配置管理方法 Mr
  • 原文地址:https://www.cnblogs.com/meditation5201314/p/13803587.html
Copyright © 2011-2022 走看看