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)文章
springboot集成flyway自動創(chuàng)表的詳細配置
Flayway是一款數(shù)據(jù)庫版本控制管理工具,支持數(shù)據(jù)庫版本自動升級,Migrations可以寫成sql腳本,也可以寫在java代碼里;本文通過實例代碼給大家介紹springboot集成flyway自動創(chuàng)表的詳細過程,感興趣的朋友一起看看吧2021-06-06
MybatisPlus EntityWrapper如何自定義SQL
這篇文章主要介紹了MybatisPlus EntityWrapper如何自定義SQL,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Java 后端接收Request請求參數(shù)的多種方式匯總
本文給大家總結(jié)了后端接收Request請求參數(shù)的7種方式,本文結(jié)合實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-10-10
SpringBoot啟動報錯的11個高頻問題排查與解決終極指南
這篇文章主要為大家詳細介紹了SpringBoot啟動報錯的11個高頻問題的排查與解決,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2025-03-03

