SpringBoot 定时器快速上手使用

/*---------------------个人学习记录---------------------*/

如果你想在springboot里面,想弄个简单的定时器,目标就是为了实现, 隔多久开始执行一个方法(函数),让它循环有规律执行,然后控制这个方法(函数)的停止,  那么,我这篇个人学习记录就对你有效了。

说到简单, 实现定时器的方法其实蛮多, 我个人在探索了一下之后,任务,最简单的就是注解了。(因为我不想搞那些杂七杂八的配置,我就想实现一下下为所欲为的控制方法执行而已)

--------------入正题--------------------------------------

①在springboot的main中开启 定时器注解: 

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;

/* 开启定时任务注解 */

@EnableScheduling
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class abcApplication {

   public static void main(String[] args) {
      SpringApplication.run(abcApplication.class, args);
   }
}

②新建一个Service,我这边只单纯为了举例,就不分什么impl了:

/**
 * @Author: JCccc
 * @CreateTime: 2018-09-21
 * @Description: 定时器任务测试
 */


@Service
public class TaskService {

   //方法
}

③在刚才建的Service类里面,开始添加方法,并使用@Scheduled  (这个用于定时器调度) :

举几个例子 做介绍:

@Scheduled(cron = "0 */1 * * * * ") public void reportCurrentByCron(){ System.out.println ("定时每分钟执行一次 : " + dateFormat().format (new Date ())); }

@Scheduled(fixedDelayString="5000")
public void delayLoop(){
    System.out.println("延迟5秒运行一次 :"+ dateFormat().format (new Date ()));

}
@Scheduled(fixedRate=1000)
public void delayLoop(){
    System.out.println("每秒运行一次 :"+ dateFormat().format (new Date ()));

}
@Scheduled(fixedDelay=1000)
public void delayLoop(){
    System.out.println("每秒运行一次 :"+ dateFormat().format (new Date ()));

}
@Scheduled(fixedRate=1000,initialDelay=2000)
 public void delayLoop(){

  System.out.println("运行后延迟2秒运行,再每隔一秒运行一次 :" + dateFormat().format(new Date()));
      
}
@Scheduled(cron = "0,1,2,3,4,5,6 * * * * MON-SAT")//每分钟的前面0,1,2,3,4,5,6秒都执行一次
public void runTask(){
  

        System.out.println("定时每分钟的前6秒发送推报 :" + new Date());
    
}

以上例子,看打印里面的中文就知道什么作用了。那么,我接下来介绍一下下。

首先,如果你认真看上面的例子,你就会发现,fixedRate 和 fixedDelay 这两个东西好像都是隔多少秒循环执行。

介绍及区别说明:

FixedRate----- > @Scheduled(fixedRate=5000)

 第一个任务开始时间秒数为00:00:30;那么在5秒之后,35秒的时候,第二个任务就会立刻执行的。 若第一个任务执行划分10,第二个任务的执行并不会受影响,还是会在00:00:35秒的时候执行。

FixedDelay---- > @Scheduled(fixedDelay=1000)

 第一个任务开始时间秒数为00:00:30;那么第二个任务执行的时候,必须是第一个任务执行完再等5秒之后才能执行。 若第一个任务执行花费10秒,那么就说00:00:40+5,也就是00:00:45秒的时候,第二个任务才会执行。

划重点!!!以下内容:--------------------------------------------!!!!!!!!!!!!---------------------------------------

上面都是关于定时器怎么调用的, 那么怎么关闭呢。

简单暴力,我举个例:

private static boolean isStop=true;//先搞个全局变量,默认是true
 @Scheduled(cron = "0 */1 *  * * * ")
    public void reportCurrentByCron(){
     if(isStop) {
         System.out.println("定时每分钟执行一次 : " + dateFormat().format(new Date()) + " " + isStop);
        }

    }

//上面可以看到, 在定时任务里面,加了个判断isStop,也就是说,默认是true,默认是允许定时器开的。

然后你想关闭定时器,只要多使用一个定时器,在里面定义时间,然后这个定时器就是为了把isStop的状态变成false,如下:

@Scheduled(fixedDelayString="10000")
    public void delayLoop1(){
    if(isStop) {
        System.out.println("延迟10秒运行一次 :" + dateFormat().format(new Date()) + " " + isStop);
        isStop=false;
        System.out.println("此刻的定时器开关为 : "+isStop);
    }
}

好了,以上都是供个人学习记录,介绍完毕。

猜你喜欢

转载自blog.csdn.net/qq_35387940/article/details/82800403
今日推荐