SpringBoot定时任务

一、导入依赖

1
2
3
4
5
6
7
<dependencies>
<!-- SpringBoot 核心组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

二、添加注解

在SpringBoot启动类上,使用@EnableScheduling 注解开启功能,自动扫描

1
2
3
4
5
6
7
8
9
10
@EnableFeignClients(basePackages = {"cn.mall.cscc"})
@EnableDiscoveryClient
@SpringBootApplication
@MapperScan(basePackages = "cn.mall.cscc.goods.mapper")
@EnableScheduling //定时任务*
public class CsccGoodsApplication {
public static void main(String[] args) {
SpringApplication.run(CsccGoodsApplication.class,args);
}
}

三、创建任务类

  • 要在任务的类上写@Component
  • 要在任务方法上写@Scheduled
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package cn.mall.cscc.goods.task;
import cn.mall.cscc.goods.entity.User;
import cn.mall.cscc.goods.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class UserTask {

@Autowired
private IUserService userService;

@Scheduled(cron = "0/15 * * * * ?")
public void test(){
long millis = System.currentTimeMillis();
System.out.println("---------------任务开始了-----------------------");
User byId = userService.getById(1);
byId.setName("太极张三丰").setUpdateTime(new Date());
userService.updateById(byId);
long l = System.currentTimeMillis();
long millis1 = l-millis;
System.out.println("---------任务结束了,花费时间:"+millis1+"ms-----------");
}
}

这里使用的是cron表示式,表示从0秒开始每15秒执行一次。

执行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
---------------任务开始了-----------------------
==> Preparing: SELECT id,name,age,phone,cdate_time,update_time,flag FROM cs_user WHERE id=? AND flag=0
==> Parameters: 1(Integer)
<== Columns: id, name, age, phone, cdate_time, update_time, flag
<== Row: 1, 太极张三丰, 33, 33, 2021-10-20 16:02:59, 2021-11-29 16:16:15, 0
<== Total: 1

==> Preparing: UPDATE cs_user SET name=?, age=?, phone=?, cdate_time=?, update_time=? WHERE id=? AND flag=0
==> Parameters: 太极张三丰(String), 33(Integer), 33(String), 2021-10-20 16:02:59.0(Timestamp), 2021-11-29 16:27:00.307(Timestamp), 1(String)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2141381f]
---------任务结束了,花费时间:359ms-----------

第二次执行

---------------任务开始了-----------------------
==> Preparing: SELECT id,name,age,phone,cdate_time,update_time,flag FROM cs_user WHERE id=? AND flag=0
==> Parameters: 1(Integer)
<== Columns: id, name, age, phone, cdate_time, update_time, flag
<== Row: 1, 太极张三丰, 33, 33, 2021-10-20 16:02:59, 2021-11-29 16:27:00, 0
<== Total: 1

==> Preparing: UPDATE cs_user SET name=?, age=?, phone=?, cdate_time=?, update_time=? WHERE id=? AND flag=0
==> Parameters: 太极张三丰(String), 33(Integer), 33(String), 2021-10-20 16:02:59.0(Timestamp), 2021-11-29 16:27:15.05(Timestamp), 1(String)
<== Updates: 1
---------任务结束了,花费时间:82ms-----------

四、总结

这里使用的是cron表示式,还有其他写法,请自行百度。

  • cron表达式:比如你要设置每天什么时候执行,就可以用它
    cron表达式:有专门的语法,http://cron.ciding.cc/ 此网站可以在线生成表达式,可以自己多试试。
    cron一共有7位,但是最后一位是年,可以留空,所以我们写6位

每位解释

1
2
3
4
5
6
7
8
* 第一位,表示秒,取值0-59
* 第二位,表示分,取值0-59
* 第三位,表示小时,取值0-23
* 第四位,日期天/日,取值1-31
* 第五位,日期月份,取值1-12
* 第六位,星期,取值1-7,星期一,星期二...,注:不是第1周,第二周的意思
另外:1表示星期天,2表示星期一。
* 第7为,年份,可以留空,取值1970-2099

cron中,还有一些特殊的符号,含义如下:

1
2
3
4
5
(*)星号:可以理解为每的意思,每秒,每分,每天,每月,每年...
(?)问号:问号只能出现在日期和星期这两个位置。
(-)减号:表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12
(,)逗号:表达一个列表值,如在星期字段中使用“1,2,4”,则表示星期一,星期二,星期四
(/)斜杠:如:x/y,x是开始值,y是步长,比如在第一位(秒) 0/15就是,从0秒开始,每15秒,最后就是0,15,30,45,60 另:*/y,等同于0/y

举例

1
2
3
4
5
0 0 3 * * ?     每天3点执行
0 5 3 * * ? 每天3点5分执行
0 5 3 ? * * 每天3点5分执行,与上面作用相同
0 5/10 3 * * ? 每天3点的 5分,15分,25分,35分,45分,55分这几个时间点执行
0 10 3 ? * 1 每周星期天,3点10分 执行,注:1表示星期天