惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
博客园 - 【当耐特】
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
MyScale Blog
MyScale Blog
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Hugging Face - Blog
Hugging Face - Blog
I
InfoQ
B
Blog RSS Feed
腾讯CDC
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
雷峰网
雷峰网
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
DataBreaches.Net
The Cloudflare Blog
V
V2EX
S
SegmentFault 最新的问题

小松鼠的博客

记录一次线上k8s工作节点无法创建容器的问题排查思路与解决办法 记一次线上GoLang项目OOM排查过程 从LastPass转向拥抱开源KeePass的心路历程 故障定位与 AI 结合前后端编码实践 FileBeat收集nginx-ingress-controller日志 K8s云原生环境下文件描述符占用过高查询思路 2024年最新关闭火绒安全工具的开机自启方法 Kubernetes任务调度实践-Go语言实现Job和CronJob对比分析 离线更新k8s环境下的trivy漏洞库方法 使用Go语言接入Choerodon实现基于OAuth2的统一身份认证登录 在Vue2中自定义Switch组件并实现父子组件双向数据绑定 关于docker jdk1.8镜像中的GB18030-2022标准支持及验证 Go框架gin中的session存储gin-contrib-sessions和go-session 关于修改node_module中的源码问题记录 docker-compose网络和内网服务IP冲突问题 慎用存储过程:一条语句引发的数据库存储100%占用 Spring Boot中4种文件下载方法的实现 避坑-不能将specific类型的gitlab-runner改变为share类型 Docker compose中的MySQL主从复制模式和percona-toolkit工具使用 在minio中开启https访问以及使用rclone备份minio桶 在多机Docker环境下部署Choerodon的解决方案 Prometheus中Monitor添加对SpringBoot Actuator的Basic认证 在Nginx的容器镜像中隐藏Nginx的Server响应头 K8s中的两种nginx-ingress-controller及其区别 两个docker工具:runlike和whaler Grafana中的邮件报警和截图插件grafana-image-enderer K8s中externalName-service和services-without-selectors maven配置文件settings.xml中的一些概念总结 K8s中flexvolume插件驱动的安装 K8s中的coredns无法解析svc问题排查
浅谈Spring定时任务的使用(Scheduled注解)
ycyin · 2020-11-04 · via 小松鼠的博客

2020年11月3日大约 6 分钟SpringScheduled定时任务


环境说明

  使用maven3、Spring4.3构建、jdk7编译、运行在tomcat7.0中。

定时任务的基本配置

pom.xml:加入依赖

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <org.springframework.version>4.3.29.RELEASE</org.springframework.version>
  </properties>

  <dependencies>
    <!--SpringMVC依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${org.springframework.version}</version>
    </dependency>
  </dependencies>

添加Spring配置文件applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd
       http://www.springframework.org/schema/task
       http://www.springframework.org/schema/task/spring-task-4.3.xsd">

    <context:component-scan base-package="com.yyc"></context:component-scan>
    <task:annotation-driven />

</beans>

web.xml:配置如下

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <!--一定要有这个监听器-->  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

使用@Scheduled注解开启定时任务

package com.yyc;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;

@Component
public class TaskImpl implements ITask{

    @Override
    @Scheduled(cron = "0/5 * * * * ? ") // cron表达式,每5秒执行一次
    public void taskMethod() {
        System.out.println("exec-"+new Date()+"--"+Thread.currentThread());
    }
}

遇到的问题

  第一次使用@Scheduled注解进行定时任务使用,发现部署成功,但定时任务老是不执行。最终发现web.xml配置出现了问题,在web.xml中,只使用context-param标签初始化了Spring的配置文件,还需要加入ContextLoaderListener监听器的配置才可以。百度一波,网友说到:tomcat启动时会加载web.xml 、加载<listener> <context-param>属性,项目启动,会找这个监听器去查contextConfigLocation

关于scheduler(调度线程)

  当有多个@Scheduled定时任务时,我们可以配置Scheduler,即调度线程池配置,可以实现多个定时任务并行执行。

未配置Scheduler的执行情况

  先来看看未配置Scheduler的执行情况。定时任务1每5秒执行一次;定时任务2也是每5秒执行一次,但是在打印之前,我们sleep 10秒,所以定时任务2会每15秒输出一次。

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;

@Component
public class TaskImpl implements ITask{

    /**
     * 定时任务1
     */
    @Override
    //@Async
    @Scheduled(cron = "0/5 * * * * ? ")
    public void taskMethod1() {
        System.out.println("TASK1-"+new Date()+"--"+Thread.currentThread());
    }

    /**
     * 定时任务2
     * @throws InterruptedException
     */
    @Override
    @Scheduled(cron = "0/5 * * * * ? ")
    public void taskMethod2() throws InterruptedException {
        Thread.sleep(10*1000);
        System.out.println("TASK2-"+new Date()+"--"+Thread.currentThread());
    }
}

xml配置:

<context:component-scan base-package="com.yyc"></context:component-scan>
<task:annotation-driven />

输出结果:

TASK2-Wed Nov 04 15:13:30 CST 2020--Thread[pool-1-thread-1,5,RMI Runtime]
TASK1-Wed Nov 04 15:13:30 CST 2020--Thread[pool-1-thread-1,5,RMI Runtime]
TASK2-Wed Nov 04 15:13:45 CST 2020--Thread[pool-1-thread-1,5,RMI Runtime]
TASK1-Wed Nov 04 15:13:45 CST 2020--Thread[pool-1-thread-1,5,RMI Runtime]
TASK2-Wed Nov 04 15:14:00 CST 2020--Thread[pool-1-thread-1,5,RMI Runtime]
TASK1-Wed Nov 04 15:14:00 CST 2020--Thread[pool-1-thread-1,5,RMI Runtime]
TASK2-Wed Nov 04 15:14:15 CST 2020--Thread[pool-1-thread-1,5,RMI Runtime]
TASK1-Wed Nov 04 15:14:15 CST 2020--Thread[pool-1-thread-1,5,RMI Runtime]
TASK2-Wed Nov 04 15:14:30 CST 2020--Thread[pool-1-thread-1,5,RMI Runtime]
TASK1-Wed Nov 04 15:14:30 CST 2020--Thread[pool-1-thread-1,5,RMI Runtime]
TASK2-Wed Nov 04 15:14:45 CST 2020--Thread[pool-1-thread-1,5,RMI Runtime]
TASK1-Wed Nov 04 15:14:45 CST 2020--Thread[pool-1-thread-1,5,RMI Runtime]

可以清晰的看到,未配置Scheduler时,任务1和任务2是串行方式执行的,都是每过15秒才打印一次,说明任何一个任务都需要等待其它任务执行完成,都是线程pool-1-thread-1执行的。

配置Scheduler的执行情况

xml配置:

<context:component-scan base-package="com.yyc"></context:component-scan>
<task:annotation-driven scheduler="dataScheduler"/>
<task:scheduler id="dataScheduler" pool-size="2"/>

输出结果:

TASK1-Wed Nov 04 15:35:50 CST 2020--Thread[dataScheduler-1,5,RMI Runtime]
TASK1-Wed Nov 04 15:35:55 CST 2020--Thread[dataScheduler-1,5,RMI Runtime]
TASK1-Wed Nov 04 15:36:00 CST 2020--Thread[dataScheduler-1,5,RMI Runtime]
TASK2-Wed Nov 04 15:36:00 CST 2020--Thread[dataScheduler-2,5,RMI Runtime]
TASK1-Wed Nov 04 15:36:05 CST 2020--Thread[dataScheduler-1,5,RMI Runtime]
TASK1-Wed Nov 04 15:36:10 CST 2020--Thread[dataScheduler-1,5,RMI Runtime]
TASK2-Wed Nov 04 15:36:15 CST 2020--Thread[dataScheduler-2,5,RMI Runtime]
TASK1-Wed Nov 04 15:36:15 CST 2020--Thread[dataScheduler-1,5,RMI Runtime]
TASK1-Wed Nov 04 15:36:20 CST 2020--Thread[dataScheduler-1,5,RMI Runtime]
TASK1-Wed Nov 04 15:36:25 CST 2020--Thread[dataScheduler-1,5,RMI Runtime]
TASK2-Wed Nov 04 15:36:30 CST 2020--Thread[dataScheduler-2,5,RMI Runtime]

为了方便查看,我们有两个任务配置pool-size为2。通过输出可以发现任务1和任务2并行执行,任务1每5秒输出一次(调度线程dataScheduler-1),任务2每15秒输出一次(调度线程dataScheduler-2)。

关于executor(执行线程)

  这里引入另一个参数,可以看任务 1上方注释掉的 @Async 注解:这个注解,代表可以异步执行。异步执行的话,调度线程池就会不用当前调度线程来执行,而是交给 task:executor 这个执行线程池来执行。

我们使用@Async注解配置任务1,任务2不变:

@Async
@Scheduled(cron = "0/5 * * * * ? ")
public void taskMethod1() {
   System.out.println("TASK1-"+new Date()+"--"+Thread.currentThread());
}

xml配置:

<context:component-scan base-package="com.yyc"></context:component-scan>
<task:annotation-driven scheduler="dataScheduler" executor="dataExecutor"/>
<task:scheduler id="dataScheduler" pool-size="2"/>
<task:executor id="dataExecutor" pool-size="10"/>

输出结果:

TASK1-Wed Nov 04 15:58:25 CST 2020--Thread[dataExecutor-1,5,RMI Runtime]
TASK1-Wed Nov 04 15:58:30 CST 2020--Thread[dataExecutor-2,5,RMI Runtime]
TASK1-Wed Nov 04 15:58:35 CST 2020--Thread[dataExecutor-3,5,RMI Runtime]
TASK2-Wed Nov 04 15:58:35 CST 2020--Thread[dataScheduler-2,5,RMI Runtime]
TASK1-Wed Nov 04 15:58:40 CST 2020--Thread[dataExecutor-4,5,RMI Runtime]
TASK1-Wed Nov 04 15:58:45 CST 2020--Thread[dataExecutor-5,5,RMI Runtime]
TASK2-Wed Nov 04 15:58:50 CST 2020--Thread[dataScheduler-2,5,RMI Runtime]
TASK1-Wed Nov 04 15:58:50 CST 2020--Thread[dataExecutor-6,5,RMI Runtime]
TASK1-Wed Nov 04 15:58:55 CST 2020--Thread[dataExecutor-7,5,RMI Runtime]
TASK1-Wed Nov 04 15:59:00 CST 2020--Thread[dataExecutor-8,5,RMI Runtime]
TASK2-Wed Nov 04 15:59:05 CST 2020--Thread[dataScheduler-2,5,RMI Runtime]
TASK1-Wed Nov 04 15:59:05 CST 2020--Thread[dataExecutor-9,5,RMI Runtime]
TASK1-Wed Nov 04 15:59:10 CST 2020--Thread[dataExecutor-10,5,RMI Runtime]
TASK1-Wed Nov 04 15:59:15 CST 2020--Thread[dataExecutor-1,5,RMI Runtime]
TASK1-Wed Nov 04 15:59:20 CST 2020--Thread[dataExecutor-2,5,RMI Runtime]
TASK2-Wed Nov 04 15:59:20 CST 2020--Thread[dataScheduler-2,5,RMI Runtime]
TASK1-Wed Nov 04 15:59:25 CST 2020--Thread[dataExecutor-3,5,RMI Runtime]

可以发现,任务1使用了@Async注解后,任务1的线程是 dataExecutor-1 到 dataExecutor-10,说明 dataScheduler-1 这个调度线程调度了任务1,但是交给了线程池中的dataExecutor中的执行线程来具体执行的;而任务2没有使用@Async注解,所以还是使用dataScheduler-2 这个调度线程调度任务2。

源码下载

本文示例源码:点击下载

总结

  1. Spring定时任务,需要spring-context jar包,我这里为了方便直接用的mvc的包;
  2. 配置的时候,一定要细心,配置文件schemaLocation等配置完整;
  3. web.xml中一定要配置org.springframework.web.context.ContextLoaderListener这个监听器;
  4. 配置task:scheduler参数的线程池,是为了根据任务总数来分配调度线程池的大小;而配置task:executor,是为了某个任务如果要异步的执行时,实现当前任务内的多线程并发。

参考

https://blog.csdn.net/yx0628/article/details/80873774