我是 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
提交
取消