spring @Scheduled 并发
一.spring定时任务配置
applicationContext.xml:红色代码部分为需要配置的部分。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 定时器配置 -->
<task:annotation-driven/>
<task:scheduler id="myScheduler" pool-size="5"/>
注意事项:<task:scheduler id="myScheduler" pool-size="5"/>这段配置为非必须,配置这段的原因是spring定时任务默认是单线程的。配置了这段表示不同定时任务不论是否在同一时间点执行,任务之间互不影响(即多线程执行)。但是自己还是会影响自己。下面介绍具体问题以及解决方式。
二.测试1(不加<task:scheduler id="myScheduler" pool-size="5"/>)
方法代码:
public class QuartzServiceImpl implements QuartzService { //1.简易定时器-(每10秒执行一次,执行时间为20秒)
@Scheduled(cron = "0/10 * * * * ? ")
public void taskA() {
try {
System.out.println(DateUtils.forMatDate(new Date())+"||A任务每10秒执行一次..");
Thread.sleep(20*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} //2.简易定时器-每5秒执行一次
@Scheduled(cron="0/5 * * * * ? ")
public void taskB(){
System.err.println(DateUtils.forMatDate(new Date())+"||B任务每5秒执行一次进入测试");
} }
结果:很明显A任务和B任务会相互影响。B任务正常应该是从15秒到20秒,但是20秒的时候执行了A任务(A任务又执行了20秒),因此B任务在40秒的时候再次执行。
二.测试2(加<task:scheduler id="myScheduler" pool-size="5"/>)
方法代码:同上,这次加上了<task:scheduler id="myScheduler" pool-size="5"/>这段配置
结果:上面的配置将spirng定时任务变为多线程,因此任务之间不会相互影响了。很明显A任务不再影响B任务的执行。正常的按每隔5秒钟执行一次。但是A任务是10秒钟执行一次,按理说应该41分30秒的时候执行了一次,下一次执行应该是40秒,而这里却变为了40分00秒的执行才执行。并没有按照每隔10秒钟执行一次。这也是上面我提到的加了线程池之后自己任务的执行时间会影响自己的下次执行。
三.测试3(加@Async注解)
方法代码:两个方法上面分别加了 @Async注解-即异步执行
public class QuartzServiceImpl implements QuartzService { //1.简易定时器-(每10秒执行一次,执行时间为20秒)
@Scheduled(cron = "0/10 * * * * ? ")
@Async
public void taskA() {
try {
System.out.println(DateUtils.forMatDate(new Date())+"||A任务每10秒执行一次..");
Thread.sleep(20*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} //2.简易定时器-每5秒执行一次
@Scheduled(cron="0/5 * * * * ? ")
@Async
public void taskB(){
System.err.println(DateUtils.forMatDate(new Date())+"||B任务每5秒执行一次进入测试");
}
结果:结果A任务和B任务都不会影响,并且自己也不会因为自己的执行时间过长而影响自己。
A任务每10秒正常执行一次。B任务每5秒正常执行一次。
四.总结
1.简单使用定时任务测试一即可。
2.解决并发使用测试二或测试三即可(建议使用测试二)。
原因,如果你的代码真的出现了测试三的情况,即本任务的执行时间影响了本任务的下次执行,那么你应该优化你的代码,sql语句等。而不是,让程序一直"欠费"的执行下去。
spring @Scheduled 并发的更多相关文章
- Spring @Scheduled应用解析
最近,遇到的一个需求,需要执行定时任务,每个一定时间需要执行某个方法 因为项目是SpringMVC的项目,所以使用的是Spring @Scheduled(由于quartz应用起来太麻烦,所以没有采用) ...
- 使用轻量级Spring @Scheduled注解执行定时任务
WEB项目中需要加入一个定时执行任务,可以使用Quartz来实现,由于项目就一个定时任务,所以想简单点,不用去配置那些Quartz的配置文件,所以就采用了Spring @Scheduled注解来实现了 ...
- spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务
spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务 >>>>>>>>>>>> ...
- 定时任务 spring @Scheduled注解
使用spring @Scheduled注解执行定时任务: 运行!!! 关于Cron表达式(转载) 表达式网站生成: http://cron.qqe2.com/ 直接点击 cronExpression ...
- Spring @Scheduled Annotation
1.Overview 这里我们将会学习Spring @Scheduled 标签,了解它是如何配置,如何设置定时任务. 关于它的使用,有两点简单的规则需要记住: ※它的方法应该是一个void返回值类型 ...
- Spring @Scheduled @Async联合实现调度任务(2017.11.28更新)
定时任务之前一直用的是quartz之类,但是注意到Spring中其实也提供了一种简单的调度注释@Scheduled,也就想尝一下鲜.. 代码示意如下: @Component @EnableSchedu ...
- spring @Scheduled注解执行定时任务
以前框架使用quartz框架执行定时调度问题. 这配置太麻烦.每个调度都需要多加在spring的配置中. 能不能减少配置的量从而提高开发效率. 最近看了看spring的 scheduled的使用注解的 ...
- quartz 框架定时任务,使用spring @Scheduled注解执行定时任务
配置quartz 在spring中需要三个jar包: quartz-1.6.5.jar.commons-collections-3.2.jar.commons-logging-1.1.jar 首先要配 ...
- 使用spring @Scheduled注解执行定时任务
以前框架使用quartz框架执行定时调度问题. 老大说这配置太麻烦.每个调度都需要多加在spring的配置中. 能不能减少配置的量从而提高开发效率. 最近看了看spring的 scheduled的使用 ...
随机推荐
- Unity打开外部程序exe/Bat文件方案
Unity调用外部程序/Bat文件 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...
- Gym - 101617F :Move Away (圆的交点)
pro:给定N个圆,求离原点最远的点,满足它在N个圆里.输出这个距离.N<50; sol:关键点一定是圆与圆的交点. 圆与 圆心到原点的直线 的交点. 然后去验证这些关键点是否在N个圆内. 实际 ...
- Python全栈之路----函数----作用域
Python中,一个函数就是一个作用域. 局部变量放置在其作用域中,根据作用域来区分,函数属于你,函数属于我. 定义完成后,作用域已经生成,使用时顺着作用域链向上查找. 函数定义完成后,不管被在哪儿被 ...
- python中time模块常用功能
import time time模块提供了大量对时间进行处理的方法 time.time() # 获取当前时间戳,得到自1970年开始的秒数 >>>time.time() 155487 ...
- Eclipse 护眼背景色设置
链接地址:http://blog.chinaunix.net/uid-27183448-id-3509010.html 背景颜色推荐:色调:85,饱和度:123,亮度:205 文档都不再是刺眼的白底 ...
- MySQL5.7.32 通用版本安装
1 上传镜像,配置好yum源 2 下载MySQL相关的包 https://dev.mysql.com/downloads/mysql/ 3 解压安装: tar -zxvf mysql-5.7.23- ...
- java_GC
垃圾回收1 内存分配 垃圾回收 调用垃圾回收器 对象终结 调用垃圾回收器 System.gc()和Runtime.getRuntime(). ...
- Java线程及线程池状态
一.Java线程的六种状态 如上图1,JDK定义线程状态是不存在“运行中”状态,但为方便描述过程有些图中会画出运行中的状态. Java线程创建后调用start方法进入就绪状态,被OS调度选中后运行,运 ...
- sql 50题
/* Navicat MySQL Data Transfer Source Server : localhost Source Server Version : 50717 Source Host : ...
- Parallel Programming for FPGAs 学习笔记(1)
Parallel Programming for FPGAs 学习笔记(1)