spring的同一定时任务上一次的任务未结束前不会启动这次任务问题
作者:asdfghzqlj
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:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" default-lazy-init="true" > <bean name="testJob" class="com.focus.support.job.TestJob" /> <task:scheduled-tasks><!-- 定时任务每10秒执行一次--> <task:scheduled ref="testJob" method="pushConFamily" cron="*/10 * * * * ?"/> </task:scheduled-tasks> </beans>
直接上代码
package com.focus.support.job; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author * @date 2018年6月5日 * @version 2.1.1 */ public class TestJob { private static final Logger log = LoggerFactory.getLogger(TestJob.class); private static Integer lastJobMaxPushIdStatus1 = 0;//上次推送的最大查询主键ID private static final String lockLastJobTimeStatus1 = "lockLastJobTimeStatus1"; private static int times = 0; public void pushConFamily(){ times++; log.info("TestJob.Start > "+lastJobMaxPushIdStatus1); List<Integer> list; if(lastJobMaxPushIdStatus1 == 0) { if(times != 1) System.out.println("等待开始"+times); synchronized (lockLastJobTimeStatus1) { if(times != 1) System.out.println("等待结束"+times); list = chaxunAll(); if (list.size() > 0) { lastJobMaxPushIdStatus1 = list.get(0); for(int i=0;i<list.size();i++) { pushData(); } } } } else { list = chaxunAll(); if (list.size() > 0) { lastJobMaxPushIdStatus1 = list.get(0); for(int i=0;i<list.size();i++) { pushData(); } } else { lastJobMaxPushIdStatus1 = 0; } } log.info("TestJob.End > "+lastJobMaxPushIdStatus1); } private List<Integer> chaxunAll() { System.out.println("执行查询操作"); int size = 0; int maxId = 0; if(times == 1) { size = 80; maxId = 80; } if(times == 2) { size = 30; maxId = 90; } if(times == 3) { size = 15; maxId = 95; } List<Integer> list = new ArrayList<>(size); for(int i=0;i<size;i++) { list.add(maxId); } return list; } private void pushData() { try { Thread.sleep(1000); } catch(Exception e) { e.printStackTrace(); } } }
执行结果
[2018-06-05 19:43:50] [com.focus.support.job.TestJob] - TestJob.Start > 0
执行查询操作
[2018-06-05 19:45:10] [com.focus.support.job.TestJob] - TestJob.End > 80
[2018-06-05 19:45:20] [com.focus.support.job.TestJob] - TestJob.Start > 80
执行查询操作
[2018-06-05 19:45:50] [com.focus.support.job.TestJob] - TestJob.End > 90
[2018-06-05 19:46:00] [com.focus.support.job.TestJob] - TestJob.Start > 90
执行查询操作
[2018-06-05 19:46:15] [com.focus.support.job.TestJob] - TestJob.End > 95
[2018-06-05 19:46:20] [com.focus.support.job.TestJob] - TestJob.Start > 95
执行查询操作
[2018-06-05 19:46:20] [com.focus.support.job.TestJob] - TestJob.End > 0
[2018-06-05 19:46:30] [com.focus.support.job.TestJob] - TestJob.Start > 0
等待开始5
等待结束5
执行查询操作
[2018-06-05 19:46:30] [com.focus.support.job.TestJob] - TestJob.End > 0
[2018-06-05 19:46:40] [com.focus.support.job.TestJob] - TestJob.Start > 0
等待开始6
等待结束6
执行查询操作
[2018-06-05 19:46:40] [com.focus.support.job.TestJob] - TestJob.End > 0
[2018-06-05 19:46:50] [com.focus.support.job.TestJob] - TestJob.Start > 0
等待开始7
等待结束7
执行查询操作
[2018-06-05 19:46:50] [com.focus.support.job.TestJob] - TestJob.End > 0
从输出结果的时间看出,有以下结论
1.上次任务未结束前,本次任务如果已到则不会启动
2.上次任务结束时,如果与本次任务启动的时间一致,则本次任务也不会启动,而是会往后顺延一次定时时间再启动
好了,以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。