Spring task定時任務

2021-09-05 09:18:22 字數 2200 閱讀 1745

第一次寫定時任務,簡訊通知定時,雖然寫完了,但是感覺**不夠簡潔,效能不行害怕跑蹦了,放到這裡一是為了記錄,更多的是希望大家提出寶貴的意見。。。。。

1.將使用者id拼接成字串,用來批量查詢使用者資訊。

string memberid = membertransactions.stream().map(membertransaction::getmemberid).map(string::valueof).collect(collectors.joining(","));
2.這段**是為了根據不同的商戶將使用者資料分組。

logger.debug("使用者資訊-------->{}", "memberinfo=" + jsonobject.tojsonstring(memberinfo));

map> resultmap = new hashmap<>();

for (memberdata memberdata : memberinfo) else

}

3.為了動態獲取定時的時間,將註解寫到配置檔案中,再動態獲取。

@component

@propertysource("classpath:timerconditiontask.properties")

public class smstask ")

public void repaytimetoday()

@scheduled(cron = "$")

public void repaytimeyesterday()

}

4定時從從資料庫中查出執行時間,拼接成註解格式,寫到配置檔案中

//屬性檔案的路徑

static string profilepath="src/main/resources/timerconditiontask.properties";

/*** 採用靜態方法

*/private static properties props = new properties();

static catch (exception e)

} //每天一點定時查資料庫,獲取執行時間

@scheduled(cron = "0 0 1 * * ?")

public void gettimercondition() ;

treemapmap = new treemap<>();

for (int i=0;i<2;i++) else

writeproperties(map);//將處理好的map寫到配置檔案中

logger.debug("獲取觸發條件----->結束");}}

//處理時間格式

private string formattime(string data)else

if( "00".equals(data.split(":")[1]))else

if( "00".equals(data.split(":")[2]))else

string value=one+" "+two+" "+three+" "+"* * ?";

return value;

}/**

* 更新(或插入)一對properties資訊(主鍵及其鍵值)

* 如果該主鍵已經存在,更新該主鍵的值;

* 如果該主鍵不存在,則外掛程式一對鍵值。

*/public static void writeproperties(mapmap)

// 以適合使用 load 方法載入到 properties 表中的格式,

// 將此 properties 表中的屬性列表(鍵和元素對)寫入輸出流

props.store(fos, "");

fos.close();

} catch (ioexception e)

}

5.生成的配置檔案

#

#sat dec 22 16:22:00 cst 2018

smsrepaytimeyesterday.schedule=0 0 12 * * ?

smsrepaytimetomorrow.schedule=0 0 12 * * ?

smsrepaytimetoday.schedule=0 0 12 * * ?

業務**不是重點,如果有錯誤,請諒解(抱拳)。

Spring Task 定時任務

所謂定時任務,就是根據我們設定的時間定時執行任務,就像定時發郵件一樣,設定時間到了,郵件就會自動傳送。在 spring 大行其道的今天,spring 也提供了其定時任務功能,spring task。同 spring 的其他功能一樣,我們既可以通過配置檔案也可以通過註解形式來實現。一 通過配置檔案 1...

Spring Task定時任務

spring task是spring3.0以後提供的定時任務工具。使用spring定時任務,除spring相關的包外不需要新增額外的jar包,支援註解和xml配置兩種形式。準備工作 在spring配置檔案頭中新增命名空間xmlns task及描述。1.1編寫註解的定時任務類 import org.s...

Spring Task定時任務

1.1.1.使用場景 1.1.2.定時任務對比 實現定時任務的方案如下 1 使用jdk的timer和timertask實現 可以實現簡單的間隔執行任務,無法實現按日曆去排程執行任務。2 使用quartz實現 quartz 是乙個非同步任務排程框架,功能豐富,可以實現按日曆排程。3 使用spring ...