近期一个需求:
项目启动会将一张表中的数据存到 redis 服务器,但是要定时看下 redis 服务器是否依旧存在全量数据,如果 redis 服务器下不是全量数据,则重新将全量数据存入 redis 服务器。
那这里首先考虑到的肯定是写个定时器。但是 SpringBoot 项目和 SpringMVC 项目的定时器写法不一致,之前写过一年关于 SpringMVC 项目定时器任务的 例子,大家如果有需要的话可以看这篇:https://blog.csdn.net/qq_39415129/article/details/94734705
那如果是 SpringBoot 项目该怎么写呢?接下来我们就来看下:
1、配置文件添加(这里用的是SpringBoot 2.1.3版本,在2.0版本以上的版本都可以用)
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-start