SpringBoot中的定时器应用(用Cron同步中间表,清空再插入)

SpringBoot中定时任务使用

1、引入依赖

只需要引入spring-boot-starter依赖就可以。

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>
</dependencies>

2、 开启定时任务

在启动类上面加上@EnableScheduling注解即可开启定时任务。

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

@SpringBootApplication
@EnableScheduling
public class SpringBootMybatisMysqlApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMybatisMysqlApplication.class, args);
    }
}

3、创建定时任务类

创建一个定时任务类,在方法上加上@Scheduled注解,注解的参数可以配置时间。注意不要忘记加上@Component。

因为要操作表,先mapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yeexun.mapper.TestSchedulerMapper">

    <!--先清空表-->
    <delete id="clearTestScheduler1">
        delete from testscheduler1
    </delete>

    <!--向表中插入数据-->
    <insert id="insertTestScheduler1">
        insert into testscheduler1 (select * from testscheduler limit 1)
    </insert>

</mapper>

mapper文件

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface TestSchedulerMapper {
    public int clearTestScheduler1();
    public int insertTestScheduler1();
}

service文件

public interface TestSchedulerService {
    public void clearTestScheduler1();
    public void insertTestScheduler1();
}

serviceImpl

import com.yeexun.mapper.TestSchedulerMapper;
import com.yeexun.service.TestSchedulerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Service
@Component
public class TestSchedulerServiceImpl implements TestSchedulerService {

    @Autowired
    private TestSchedulerMapper testSchedulerMapper;

    @Override
    @Scheduled(fixedRate = 10000)
    public void clearTestScheduler1() {
        int status = testSchedulerMapper.clearTestScheduler1();
        System.out.println("删除操作"+status);
    }

    @Override
    @Scheduled(cron = "*/5 * * * * ?")
    public void insertTestScheduler1() {
        int status = testSchedulerMapper.insertTestScheduler1();
        System.out.println("插入操作"+status);
    }
}

结果为

一直插入两条,清空全部两条。

4、@Scheduled注解参数说明

       参数说明:
@Scheduled 参数可以接受两种定时的设置,一种是我们常用的cron="*/5 * * * * ?",一种是 fixedRate = 5000,两种都表示每隔五秒打印一下内容。

    fixedRate 说明:

    @Scheduled(fixedRate = 5000) :上一次开始执行时间点之后5秒再执行。
    @Scheduled(fixedDelay = 5000) :上一次执行完毕时间点之后5秒再执行。
    @Scheduled(initialDelay=1000, fixedRate=5000) :第一次延迟1秒后执行,之后按fixedRate的规则每5秒执行一次。

5、Cron表达式与生成工具

Cron表达式的时间字段除允许设置数值外,还可使用一些特殊的字符,提供列表、范围、通配符等功能,细说如下:

●星号(*):可用在所有字段中,表示对应时间域的每一个时刻,例如,*在分钟字段时,表示“每分钟”;

●问号(?):该字符只在日期和星期字段中使用,它通常指定为“无意义的值”,相当于点位符;

●减号(-):表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12;

●逗号(,):表达一个列表值,如在星期字段中使用“MON,WED,FRI”,则表示星期一,星期三和星期五;

●斜杠(/):x/y表达一个等步长序列,x为起始值,y为增量步长值。如在分钟字段中使用0/15,则表示为0,15,30和45秒,而5/15在分钟字段中表示5,20,35,50,你也可以使用*/y,它等同于0/y;

●L:该字符只在日期和星期字段中使用,代表“Last”的意思,但它在两个字段中意思不同。L在日期字段中,表示这个月份的最后一天,如一月的31号,非闰年二月的28号;如果L用在星期中,则表示星期六,等同于7。但是,如果L出现在星期字段里,而且在前面有一个数值 X,则表示“这个月的最后X天”,例如,6L表示该月的最后星期五;

●W:该字符只能出现在日期字段里,是对前导日期的修饰,表示离该日期最近的工作日。例如15W表示离该月15号最近的工作日,如果该月15号是星期六,则匹配14号星期五;如果15日是星期日,则匹配16号星期一;如果15号是星期二,那结果就是15号星期二。但必须注意关联的匹配日期不能够跨月,如你指定1W,如果1号是星期六,结果匹配的是3号星期一,而非上个月最后的那天。W字符串只能指定单一日期,而不能指定日期范围;

●LW组合:在日期字段可以组合使用LW,它的意思是当月的最后一个工作日;

●井号(#):该字符只能在星期字段中使用,表示当月某个工作日。如6#3表示当月的第三个星期五(6表示星期五,#3表示当前的第三个),而4#5表示当月的第五个星期三,假设当月没有第五个星期三,忽略不触发;

● C:该字符只在日期和星期字段中使用,代表“Calendar”的意思。它的意思是计划所关联的日期,如果日期没有被关联,则相当于日历中所有日期。例如5C在日期字段中就相当于日历5日以后的第一天。1C在星期字段中相当于星期日后的第一天。

Cron表达式对特殊字符的大小写不敏感,对代表星期的缩写英文大小写也不敏感。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值