spring boot之schedule

来源:互联网 发布:澳洲排油丸 知乎 编辑:程序博客网 时间:2024/06/03 22:39

这次我们来简单讲一下spring boot中的轮询机制。

一.如何实现轮询

定时任务实现方式:

1.Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。
2.使用Quartz框架,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂,有空介绍。
3.本文主要介绍的SpringBoot自带的Scheduled,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多。

二、创建调度任务

1.实现一个调度类

package com.god.schedule;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.time.LocalTime;/** * Created by wujiaqi on 17/10/30. */@Component@EnableSchedulingpublic class ScheduleTest {    private static final Logger logger = LoggerFactory.getLogger(ScheduleTest.class);    @Scheduled(cron = " * */1 * * * ?")    public void scheduleTest1() {        LocalTime localTime = LocalTime.now();        Thread current = Thread.currentThread();        System.out.println(localTime.toString() + ": currentId: " + current.getId() + "currentId: " + current.getName() + ": this is test 1");    }    @Scheduled(fixedRate = 5000)    public void scheduleTest2() {        LocalTime localTime = LocalTime.now();        Thread current = Thread.currentThread();        System.out.println(localTime.toString() + ": currentId: " + current.getId() + "currentId: " + current.getName() + ": this is test 2");    }    @Scheduled(fixedDelay = 5000)    public void scheduleTest3() {        LocalTime localTime = LocalTime.now();        Thread current = Thread.currentThread();        System.out.println(localTime.toString() + ": currentId: " + current.getId() + "currentId: " + current.getName() + ": this is test 3");    }    @Scheduled(fixedDelayString = "5000")    public void scheduleTest4() {        LocalTime localTime = LocalTime.now();        Thread current = Thread.currentThread();        System.out.println(localTime.toString() + ": currentId: " + current.getId() + "currentId: " + current.getName() + ": this is test 4");    }    @Scheduled(initialDelay = 5000, fixedDelay = 5000)    public void scheduleTest5() {        LocalTime localTime = LocalTime.now();        Thread current = Thread.currentThread();        System.out.println(localTime.toString() + ": currentId: " + current.getId() + "currentId: " + current.getName() + ": this is test 5");    }    @Scheduled(fixedRate = 3000)    public void timerRate() {        LocalTime localTime = LocalTime.now();        System.out.println(localTime.toString());    }}

需要注意的是:
1.在你的轮询类上加上@EnableScheduling注解开启轮询,也可加载启动类上在全局开启。
2.需要加上@component类型注解交由spring管理
3.initialDelay该属性需要配合其他属性一起使用,否则会产生java.lang.IllegalArgumentException
4.cron、fixedDelay、fixedRate 三者之间不能共存!!!

三、注解以及属性讲解

下面描述下@Scheduled中的参数:
@Scheduled(zone=”“): 解析cron所在时区,默认为当前时区
@Scheduled(fixedRate=5000):上一次开始执行时间点后5秒再次执行;
@Scheduled(fixedDelay=5000):上一次执行完毕时间点后5秒再次执行;
@Scheduled(fixedRateString=5000):上一次开始执行时间点后5秒再次执行(参数为String);
@Scheduled(fixedDelayString=5000):上一次执行完毕时间点后5秒再次执行(参数为String);
@Scheduled(initialDelay=5000, fixedDelay=5000):第一次延迟5秒执行,然后在上一次执行完毕时间点后5秒再次执行,属性可以组合使用;
@Scheduled(cron=”* * * * * ?”):按cron规则执行。具体可以参考我的另外一篇博文:Linux之cron

四、实现并行轮询

接下来我们来看一下我们的日志输出:

21:33:45.005: currentId: 18currentId: pool-2-thread-1: this is test 121:33:46.003: currentId: 18currentId: pool-2-thread-1: this is test 121:33:47.004: currentId: 18currentId: pool-2-thread-1: this is test 121:33:47.30821:33:48.001: currentId: 18currentId: pool-2-thread-1: this is test 121:33:49.001: currentId: 18currentId: pool-2-thread-1: this is test 121:33:49.308: currentId: 18currentId: pool-2-thread-1: this is test 221:33:49.308: currentId: 18currentId: pool-2-thread-1: this is test 521:33:49.311: currentId: 18currentId: pool-2-thread-1: this is test 321:33:49.311: currentId: 18currentId: pool-2-thread-1: this is test 421:33:50.002: currentId: 18currentId: pool-2-thread-1: this is test 121:33:50.30821:33:51.004: currentId: 18currentId: pool-2-thread-1: this is test 121:33:52.002: currentId: 18currentId: pool-2-thread-1: this is test 121:33:53.005: currentId: 18currentId: pool-2-thread-1: this is test 121:33:53.30521:33:54.004: currentId: 18currentId: pool-2-thread-1: this is test 121:33:54.305: currentId: 18currentId: pool-2-thread-1: this is test 221:33:54.309: currentId: 18currentId: pool-2-thread-1: this is test 521:33:54.312: currentId: 18currentId: pool-2-thread-1: this is test 321:33:54.313: currentId: 18currentId: pool-2-thread-1: this is test 421:33:55.003: currentId: 18currentId: pool-2-thread-1: this is test 1

我们可以看到日志根据我们的规则进行输出,很重要的一点我们需要注意到,每个方法调用的线程都是pool-2-thread-1,也就是说我们的轮询都由同一条线程实现,是单线程任务。那么我们怎么来实现多线程任务呢?
下面我们来看看如何实现并行轮询,竟然spring boot 的目的就是为了去除配置文件,那么下面就不会通过配置文件来实现,我们来看看如何通过接口来实现,下面直接上代码。

    @Configuration@EnableAsync(        mode = AdviceMode.PROXY, proxyTargetClass = false,        order = Ordered.HIGHEST_PRECEDENCE)public class SchduleConfig implements SchedulingConfigurer {    @Override    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {        scheduledTaskRegistrar.setScheduler(taskExecutor());    }    @Bean(destroyMethod="shutdown")    public Executor taskExecutor() {        return Executors.newScheduledThreadPool(100);    }}

只需要创建多个线程池就好啦
让我们来看一下日志,很明显已经实现了多线程:

22:01:20.346: currentId: 74 currentName: pool-2-thread-40: this is test 522:01:20.357: currentId: 54 currentName: pool-2-thread-20: this is test 422:01:20.357: currentId: 75 currentName: pool-2-thread-41: this is test 322:01:21.003: currentId: 22 currentName: pool-2-thread-5: this is test 122:01:21.32922:01:21.329: currentId: 45 currentName: pool-2-thread-11: this is test 222:01:21.347: currentId: 77 currentName: pool-2-thread-43: this is test 522:01:21.360: currentId: 55 currentName: pool-2-thread-21: this is test 4
原创粉丝点击