我是 Groovy 和 Grails(和 Java)的新手,我有一個 Quartz 調度程序工作(下面的代碼),想知道1.)第一次調用作業(或在應用程序啟動時,例如在另一個文件中)如何做一些初始化工作(檢查數據庫并初始化局部變量),但是如何在這個作業中設置計數器變量?2.) 變量在調用作業之間是否保持它們的值?如果沒有,我該怎么做?class MyJob { static triggers = { simple repeatInterval: 1000l // execute job every 1 second } // These need to be initiated (with values from a DB) the first time the job is run: long myCounter1, myCounter2, myCounter3 def execute() { if(first time job is run / application startup) { // get values for counters defined above, from DB } // else values should persist from last job run // Get stuff from database, passing in counter values}我正在使用 Grails 的 Quartz 插件https://grails-plugins.github.io/grails-quartz/guide/introduction.html它使用 Quartz 調度程序http://www.quartz-scheduler.org/documentation/2.4 .0-SNAPSHOT/quick-start-guide.html
1 回答

吃雞游戲
TA貢獻1829條經驗 獲得超7個贊
@PersistJobDataAfterExecution使用JobDataMap注釋您的作業并在執行之間存儲/檢索數據。
import org.quartz.*;
@PersistJobDataAfterExecution
public class ExampleJob {
static triggers = {
simple repeatInterval: 1000l // execute job every 1 second
}
@Override
void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
JobDataMap jobDataMap = jobExecutionContext.jobDetails.jobDataMap
Integer count = jobDataMap.get("count") ?: 0
jobDataMap.put("count", ++count)
}
}
添加回答
舉報
0/150
提交
取消