spring boot之schedule

本文介绍了Spring Boot中实现轮询机制的方法,包括通过Java的Timer类、Quartz框架和SpringBoot自带的Scheduled。重点讲述了Scheduled的使用,如启用轮询、创建调度任务,并详细解释了@Scheduled注解的各个属性。最后探讨了如何实现并行轮询,通过创建多个线程池达到多线程任务的目的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这次我们来简单讲一下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
@EnableScheduling
public 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 1
21:33:46.003: currentId: 18currentId: pool-2-thread-1: this is test 1
21:33:47.004: currentId: 18currentId: pool-2-thread-1: this is test 1
21:33:47.308
21:33:48.001: currentId: 18currentId: pool-2-thread-1: this is test 1
21:33:49.001: currentId: 18currentId: pool-2-thread-1: this is test 1
21:33:49.308: currentId: 18currentId: pool-2-thread-1: this is test 2
21:33:49.308: currentId: 18currentId: pool-2-thread-1: this is test 5
21:33:49.311: currentId: 18currentId: pool-2-thread-1: this is test 3
21:33:49.311: currentId: 18currentId: pool-2-thread-1: this is test 4
21:33:50.002: currentId: 18currentId: pool-2-thread-1: this is test 1
21:33:50.308
21:33:51.004: currentId: 18currentId: pool-2-thread-1: this is test 1
21:33:52.002: currentId: 18currentId: pool-2-thread-1: this is test 1
21:33:53.005: currentId: 18currentId: pool-2-thread-1: this is test 1
21:33:53.305
21:33:54.004: currentId: 18currentId: pool-2-thread-1: this is test 1
21:33:54.305: currentId: 18currentId: pool-2-thread-1: this is test 2
21:33:54.309: currentId: 18currentId: pool-2-thread-1: this is test 5
21:33:54.312: currentId: 18currentId: pool-2-thread-1: this is test 3
21:33:54.313: currentId: 18currentId: pool-2-thread-1: this is test 4
21: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 5
22:01:20.357: currentId: 54 currentName: pool-2-thread-20: this is test 4
22:01:20.357: currentId: 75 currentName: pool-2-thread-41: this is test 3
22:01:21.003: currentId: 22 currentName: pool-2-thread-5: this is test 1
22:01:21.329
22:01:21.329: currentId: 45 currentName: pool-2-thread-11: this is test 2
22:01:21.347: currentId: 77 currentName: pool-2-thread-43: this is test 5
22:01:21.360: currentId: 55 currentName: pool-2-thread-21: this is test 4
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值