【第20章】spring-mvc之定时任务


前言

定时任务是项目中比较常见的功能,常用于定时发送消息、拉取数据、数据备份等;

为什么要放到SpringMvc中来写呢,因为spring项目原来都是编码完成,写个测试类执行下,执行完成程序就结束了,也就是说程序无法持续地提供服务,SpringMvc可以借助容器,我们可以提供7*24不间断服务,执行定时任务也就不在话下了。


一、开启

1. 打开开关

package org.example.springmvc.config;

/**
 * Create by zjg on 2024/4/27
 */
@Configuration
@EnableWebMvc
@EnableScheduling
public class WebConfig implements WebMvcConfigurer {
}

2. 定时任务类

package org.example.springmvc.task;

import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Create by zjg on 2024/5/5
 */
@Log4j2
@Component
public class TaskServer {
    @Scheduled(cron = "0 */1 * * * *")
    public void task1(){//每分钟1次
        log.debug("task1");
    }
    @Scheduled(cron = "0 */10 * * * *")
    public void task2(){//每10分钟1次
        log.debug("task2");
    }
    @Scheduled(cron = "0 0 * * * *")
    public void task3(){//每小时1次
        log.debug("task3");
    }
}

3. 执行结果

[2024-05-05 16:00:00.005][pool-3-thread-1][DEBUG]- org.example.springmvc.task.TaskServer.task2(TaskServer.java:19) - task2
[2024-05-05 16:00:00.006][pool-3-thread-1][DEBUG]- org.example.springmvc.task.TaskServer.task1(TaskServer.java:15) - task1
[2024-05-05 16:00:00.006][pool-3-thread-1][DEBUG]- org.example.springmvc.task.TaskServer.task3(TaskServer.java:23) - task3

二、定时任务线程池

不知道大家注意到上面的结果没有,上面的三个任务都是同一个线程去执行的,要是第一个任务很慢的话,那后面的定时任务就只能等着,我们使用线程池来解决这个问题。

1.定义线程池

<!--定时任务线程池-->
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
   <property name="corePoolSize" value="5"/>
    <property name="maxPoolSize" value="10"/>
    <property name="queueCapacity" value="25"/>
</bean>

2.开启异步

@Configuration
@EnableWebMvc
@EnableScheduling
@EnableAsync
public class WebConfig implements WebMvcConfigurer {
}

3. 定时任务类

package org.example.springmvc.task;

import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Create by zjg on 2024/5/5
 */
@Log4j2
@Component
@Async
public class TaskServer {
    @Scheduled(cron = "0 */1 * * * *")
    public void task1(){//每分钟1次
        log.debug("task1");
    }
    @Scheduled(cron = "0 */10 * * * *")
    public void task2(){//每10分钟1次
        log.debug("task2");
    }
    @Scheduled(cron = "0 0 * * * *")
    public void task3(){//每小时1次
        log.debug("task3");
    }
}

4. 执行结果

[2024-05-05 17:00:00.013][taskExecutor-5][DEBUG]- org.example.springmvc.task.TaskServer.task3(TaskServer.java:25) - task3
[2024-05-05 17:00:00.013][taskExecutor-1][DEBUG]- org.example.springmvc.task.TaskServer.task1(TaskServer.java:17) - task1
[2024-05-05 17:00:00.013][taskExecutor-2][DEBUG]- org.example.springmvc.task.TaskServer.task2(TaskServer.java:21) - task2

三、cron

关于周日的设计就很包容,0或7是周日,或者你直接写英文也是可以的。

在这里插入图片描述

Cron 表达式描述
* * * * * * 每秒钟执行一次
0 */1 * * * *每分钟执行一次
0 */10 * * * *每10分钟执行一次
0 0/30 * * * *每半小时执行一次
0 0 * * * *每小时执行一次
0 0 0 * * *每天执行一次
0 0 8-10 * * *每天8,9,10点执行一次
0 0 6,19 * * *每天6,19点执行一次
0 0/30 8-10 * * *每天8:00, 8:30, 9:00, 9:30, 10:00 and 10:30 执行一次
0 0 0 1 * *月初第一天执行一次
0 0 0 L * *月底最后一天执行一次
0 0 0 1 1 *1月月初第一天执行一次
0 0 0 L 12 *12月月底最后一天执行一次
0 0 0 ? * 5#2一个月的第二个星期五
0 0 0 ? * MON#1一个月的第一个星期一

总结

回到顶部
官方文档
官方cron
cron表达式
【第23章】spring-async(异步)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值