springboot项目@Scheduled注解实现定时任务

博客 动态
0 167
优雅殿下
优雅殿下 2022-03-03 16:56:02
悬赏:0 积分 收藏

springboot项目 @Scheduled注解 实现定时任务

使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式:一、基于注解(@Scheduled)二、基于接口(SchedulingConfigurer) 前者相信大家都很熟悉,但是实际使用中我们往往想从数据库中读取指定时间来动态执行定时任务,这时候基于接口的定时任务就派上用场了。三、基于注解设定多线程定时任务

一、静态:基于注解

1、创建定时器

使用SpringBoot基于注解来创建定时任务非常简单,只需几行代码便可完成。 代码如下:

复制代码
@Component@Configuration //主要用于标记配置类,兼备component的效果@EnableScheduling  //开启定时任务public class StaticScheduleTask {    @Resource    RealTimeMonitorServiceImpl realTimeMonitorService;    //添加定时任务 4小时/4小时/4小时/    @Scheduled(cron = "0 0 0/4 * * ?")    private void configureTasks() {        log.info("执行静态定时任务时间: " + LocalDateTime.now());    }}
复制代码

关于Cron表达式介绍

cronExpression定义时间规则,Cron表达式由6或7个空格分隔的时间字段组成:秒 分钟 小时 日期 月份 星期 年(可选)

字段  允许值  允许的特殊字符 秒       0-59     , - * / 分       0-59     , - * / 小时      0-23     , - * / 日期      1-31     , - * ? / L W C 月份      1-12     , - * / 星期      1-7       , - * ? / L C # 年     1970-2099   , - * /

表达式网站生成:   http://cron.qqe2.com/  

 

 

二、动态:基于接口

基于接口(SchedulingConfigurer)

复制代码
复制代码
<parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter</artifactId>        <version>2.0.4.RELEASE</version>    </parent>    <dependencies>        <dependency><!--添加Web依赖 -->            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency><!--添加MySql依赖 -->             <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>        </dependency>        <dependency><!--添加Mybatis依赖 配置mybatis的一些初始化的东西-->            <groupId>org.mybatis.spring.boot</groupId>            <artifactId>mybatis-spring-boot-starter</artifactId>            <version>1.3.1</version>        </dependency>        <dependency><!-- 添加mybatis依赖 -->            <groupId>org.mybatis</groupId>            <artifactId>mybatis</artifactId>            <version>3.4.5</version>            <scope>compile</scope>        </dependency>    </dependencies>
复制代码
复制代码

2、添加数据库记录:

开启本地数据库mysql,随便打开查询窗口,然后执行脚本内容,如下:

复制代码
复制代码
DROP DATABASE IF EXISTS `socks`;CREATE DATABASE `socks`;USE `SOCKS`;DROP TABLE IF EXISTS `cron`;CREATE TABLE `cron`  (  `cron_id` varchar(30) NOT NULL PRIMARY KEY,  `cron` varchar(30) NOT NULL  );INSERT INTO `cron` VALUES ('1', '0/5 * * * * ?');
复制代码
复制代码

 

 

然后在项目中的application.yml 添加数据源:

spring:  datasource:    url: jdbc:mysql://localhost:3306/socks    username: root    password: 123456

3、创建定时器

数据库准备好数据之后,我们编写定时任务,注意这里添加的是TriggerTask,目的是循环读取我们在数据库设置好的执行周期,以及执行相关定时任务的内容。
具体代码如下:

复制代码
复制代码
@Component@Configuration      //1.主要用于标记配置类,兼备Component的效果。@EnableScheduling   // 2.开启定时任务public class DynamicScheduleTask implements SchedulingConfigurer {    @Mapper    public interface CronMapper {        @Select("select cron from cron limit 1")        public String getCron();    }    @Autowired      //注入mapper    @SuppressWarnings("all")    CronMapper cronMapper;    /**     * 执行定时任务.     */    @Override    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {        taskRegistrar.addTriggerTask(                //1.添加任务内容(Runnable)                () -> System.out.println("执行动态定时任务: " + LocalDateTime.now().toLocalTime()),                //2.设置执行周期(Trigger)                triggerContext -> {                    //2.1 从数据库获取执行周期                    String cron = cronMapper.getCron();                    //2.2 合法性校验.                    if (StringUtils.isEmpty(cron)) {                        // Omitted Code ..                    }                    //2.3 返回执行周期(Date)                    return new CronTrigger(cron).nextExecutionTime(triggerContext);                }        );    }}
复制代码
复制代码

三、多线程定时任务

基于注解设定多线程定时任务

1、创建多线程定时任务

复制代码
复制代码
//@Component注解用于对那些比较中立的类进行注释;//相对与在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释@Component@EnableScheduling   // 1.开启定时任务@EnableAsync        // 2.开启多线程public class MultithreadScheduleTask {        @Async        @Scheduled(fixedDelay = 1000)  //间隔1秒        public void first() throws InterruptedException {            System.out.println("第一个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName());            System.out.println();            Thread.sleep(1000 * 10);        }        @Async        @Scheduled(fixedDelay = 2000)        public void second() {            System.out.println("第二个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName());            System.out.println();        }    }
复制代码
复制代码

 

代码地址:https://github.com/mmzsblog/springboot-schedule

 转载:https://www.cnblogs.com/mmzs/p/10161936.html

posted @ 2022-03-03 16:31 jiuchengi 阅读(0) 评论(0) 编辑 收藏 举报
回帖
    优雅殿下

    优雅殿下 (王者 段位)

    2018 积分 (2)粉丝 (47)源码

    小小码农,大大世界

     

    温馨提示

    亦奇源码

    最新会员