springBoot 实现定时任务-scheduled

springBoot 实现定时任务-scheduled

在 Spring + SpringMVC 环境中,一般来说,要实现定时任务,我们有两中方案,一种是使用 Spring 自带的定时任务处理器 @Scheduled 注解,另一种就是使用第三方框架 Quartz ,Spring Boot 源自 Spring+SpringMVC ,因此天然具备这两个 Spring 中的定时任务实现策略,当然也支持 Quartz,今天我们就先总结下Spring 自带的定时任务处理器。废话不多说,直接开搞。

创建一个springBoot 工程(引入web模块和lombok组件)

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--使用 slf4j 日志门面 日志注解-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

编写一个简单定时任务

package com.example.scheduledtest;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ldy
 * 使用 slf4j 日志门面需要引用lombok依赖(此时类才可以正常使用日志)
 * EnableScheduling 开启定时任务自动化配置
 */
@SpringBootApplication
@EnableScheduling
@Slf4j
public class ScheduledTestApplication {

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

    /**
     * fixedRate 代表距离上次任务开始时间2秒后执行 -- 相同属性 fixedRateString (把数字改成字符串,优点:
     *    可以通过 ${time} 注入配置文件中的时间)
     *    zone 代表使用服务器本地时区
     *  两次任务开始之间的时间间隔(第二次任务开始,第一次任务可能没结束)
     */
    @Scheduled(fixedRate = 2000,zone = "")
    public void fixedRate(){
         SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         log.info("fixedRate测试,时间:"+dateFormat.format(new Date()));
    }

    /**
     * fixedDelay 代表距离上次任务结束后2秒执行  -- 相同属性同上有个支持String
     * 两次任务执行之间的时间间隔,第一次任务结束距离第二次任务开始之间的时间间隔
     */
    @Scheduled(fixedDelay = 2000)
    public void fixedDelay(){
          SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          log.info("fixedDelay测试,时间:"+dateFormat.format(new Date()));
    }

    /**
     * initialDelay 表示任务首次执行的延时时间 2 秒 -- 相同属性同上有个支持String
     */
    @Scheduled(fixedRate = 2000,initialDelay = 2000)
    public void initialDelay(){
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("fixedDelay测试,时间:"+dateFormat.format(new Date()));
    }

    /**
     *在方法上使用 @Scheduled,表示此方法定时执行。cron 时间表达式表示多长时间执行。
     *  支持注入配置文件中的 Cron 表达式 ${time}
     */
    @Scheduled(cron = "0/5 * * * * *")
    public void cron(){
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("cron定时任务执行,时间:"+dateFormat.format(new Date()));
    }
}

通过springBoot原生的方式实现定时任务还是比较简单的。其中要想实现复杂的定时效果需要用到 cron 表达式。推荐阅读下面链接的文章。[@Scheduled注解各参数详解]: https://blog.csdn.net/tiguer/article/details/94597097

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值