亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Grails 中的 Quartz 作業開始時運行一次函數,并在調用之間保持變量值?

如何在 Grails 中的 Quartz 作業開始時運行一次函數,并在調用之間保持變量值?

尚方寶劍之說 2022-07-14 08:58:57
我是 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)

    }

}


查看完整回答
反對 回復 2022-07-14
  • 1 回答
  • 0 關注
  • 90 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號