Java框架_SpringBoot定时任务器Scheduled

本文详细介绍如何在Spring框架中配置和使用Scheduled定时任务器,包括创建项目、添加依赖、编写定时任务类、启动定时任务等内容,并解释了Cron表达式的使用方法。

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

一、Scheduled定时任务器

Scheduled定时任务器:是Spring3.0以后自带的一个定时任务器。

1、创建项目

参考https://blog.youkuaiyun.com/lxp18850413505/article/details/80731931

2、在pom.xml文件添加scheduled的依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.lxp</groupId>
	<artifactId>framework</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	
	<!-- Inherit defaults from Spring Boot -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.RELEASE</version>
	</parent>
	
	<properties>
		<java.version>1.7</java.version>
		<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
		<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
	</properties>
	
	<!-- Add typical dependencies for a web application -->
	<dependencies>
	
		<!-- Web 启动器  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- Junit测试  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        
        <!-- thymeleaf -->
        <dependency>
	        <groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-starter-thymeleaf</artifactId>
	    </dependency>
		  
	    <!-- mybatis -->
        <dependency>
	        <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
	    </dependency>
	    
	    <!-- mysql数据库驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
	    
	    <!-- druid 数据连接池 -->
        <dependency>
	        <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
	    </dependency>
    
    	<!-- spring data JPA  -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		
		<!-- Spring Boot缓存支持启动器 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-cache</artifactId>
		</dependency>

		<!-- Ehcache -->
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache</artifactId>
		</dependency>
		
		<!-- Scheduled -->
		<dependency>
	        <groupId>org.springframework</groupId>
	        <artifactId>spring-context-support</artifactId>
	    </dependency>

    </dependencies>
</project>

3、编写定时任务类
@Component
public class ScheduledDemo {

	@Scheduled(cron = "0/2 * * * * ?")
	public void ScheduledDemoMethod() {
		System.out.println("定时任务触发:" + new Date());
	}
}

4、在启动器里面开启定时任务
@SpringBootApplication
@MapperScan("com.lxp.mapper")
@EnableScheduling
public class AppStart {

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

}
5、con表达式

Cron表达式是一个字符串,分为67个域,每一个域代表一个含义

Cron有如下两种语法格式:

1 Seconds Minutes Hours Day MonthWeek Year

2Seconds Minutes Hours Day Month Week

一、结构

corn从左到右(用空格隔开):秒小时月份中的日期月份星期中的日期年份

二、各字段的含义

位置

时间域名

允许值

允许的特殊字符

1

0-59

, - * /

2

分钟

0-59

, - * /

3

小时

0-23

, - * /

4

1-31

, - * / L W C

5

1-12

, - * /

6

星期

1-7

, - * ? / L C #

7

(可选)

1970-2099

, - * /


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

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

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

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

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

斜杠(/)x/y表达一个等步长序列,x为起始值,y为增量步长值。如在分钟字段中使用0/15,则表示为0,15,3045秒,而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表达式对特殊字符的大小写不敏感,对代表星期的缩写英文大小写也不敏感。

例子:

@Scheduled(cron = "0 0 1 1 1?")//每年一月的一号的1:00:00 执行一次

@Scheduled(cron = "0 0 1  1 1,6?") //一月和六月的一号的1:00:00 执行一次

@Scheduled(cron = "0 0 1  11,4,7,10 ?") //每个季度的第一个月的一号的1:00:00 执行一次

@Scheduled(cron = "0 0 1  1 *?")//每月一号 1:00:00 执行一次

@Scheduled(cron="0 0 1 * * *")//每天凌晨1点执行一次

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值