spring的同一定時任務(wù)上一次的任務(wù)未結(jié)束前不會啟動這次任務(wù)問題
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><!-- 定時任務(wù)每10秒執(zhí)行一次--> <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("等待結(jié)束"+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("執(zhí)行查詢操作"); 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(); } } }
執(zhí)行結(jié)果
[2018-06-05 19:43:50] [com.focus.support.job.TestJob] - TestJob.Start > 0
執(zhí)行查詢操作
[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
執(zhí)行查詢操作
[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
執(zhí)行查詢操作
[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
執(zhí)行查詢操作
[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
等待結(jié)束5
執(zhí)行查詢操作
[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
等待結(jié)束6
執(zhí)行查詢操作
[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
等待結(jié)束7
執(zhí)行查詢操作
[2018-06-05 19:46:50] [com.focus.support.job.TestJob] - TestJob.End > 0
從輸出結(jié)果的時間看出,有以下結(jié)論
1.上次任務(wù)未結(jié)束前,本次任務(wù)如果已到則不會啟動
2.上次任務(wù)結(jié)束時,如果與本次任務(wù)啟動的時間一致,則本次任務(wù)也不會啟動,而是會往后順延一次定時時間再啟動
好了,以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
2023最新版IDEA創(chuàng)建javaweb項目的詳細圖文教程
之前用的社區(qū)版IDEA無法部署JavaWeb項目,于是裝了一個最新版的IDEA,下面這篇文章主要給大家介紹了關(guān)于2023最新版IDEA創(chuàng)建javaweb項目的詳細圖文教程,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-06-06Spring?Boot攔截器和監(jiān)聽器實現(xiàn)對請求和響應(yīng)處理實戰(zhàn)
這篇文章主要介紹了Spring?Boot攔截器和監(jiān)聽器實現(xiàn)對請求和響應(yīng)處理實戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06Java基礎(chǔ)知識之BufferedReader流的使用
這篇文章主要介紹了Java基礎(chǔ)知識之BufferedReader流的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12@DynamicUpdate //自動更新updatetime的問題
這篇文章主要介紹了@DynamicUpdate //自動更新updatetime的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07java編程中拷貝數(shù)組的方式及相關(guān)問題分析
這篇文章主要介紹了java編程中拷貝數(shù)組的方式及相關(guān)問題分析,分享了Java中數(shù)組復(fù)制的四種方式,其次對二維數(shù)組的簡單使用有一段代碼示例,具有一定參考價值,需要的朋友可以了解下。2017-11-11Java Springboot之Spring家族的技術(shù)體系
今天帶大家來學習Spring家族的技術(shù)體系,文中有非常詳細的圖文介紹及代碼示例,對正在學習java的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05java?-jar啟動服務(wù)并輸出日志常用命令小結(jié)
這篇文章主要介紹了在Linux環(huán)境下運行JAR包的幾種方法,包括在命令結(jié)尾添加&使其在后臺運行,使用nohup使程序不掛斷運行,以及將日志輸出到指定文件或丟棄,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-03-03Java微信公眾平臺開發(fā)(6) 微信開發(fā)中的token獲取
這篇文章主要為大家詳細介紹了Java微信公眾平臺開發(fā)第六步,微信開發(fā)中的token獲取,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04