build.gradle
依賴://定時器 compile 'org.grails.plugins:quartz:2.0.0.M4'
建立一個job,執行命令:bash
create-job com.test.Test
修改TestJob:gradle
package com.test class TestJob { static triggers = {} def execute(context) { // execute job println "執行..." // 獲取傳遞過來的參數 def msg = context.mergedJobDataMap.get("tip") println msg } }
def personPush() { println "xxxxx" // 傳參到job中 TestJob.schedule(new Date(),[tip: "執行了定時器。。。"]) }
def jobManagerService def index() { // 中止全部定時任務 jobManagerService.pauseAll() render "Stop all Job." }
jobManagerService.resumeAll()
JobManagerService
服務中經常使用方法Map <String , List<JobDescriptor>> getAllJobs() {} List<JobDescriptor> getJobs(String group) {} def getRunningJobs() {} def pauseJob(String group, String name) {} def resumeJob(String group, String name) {} def pauseTrigger(String group, String name) {} def resumeTrigger(String group, String name) {} def pauseTriggerGroup(String group) {} def resumeTriggerGroup(String group) {} def pauseJobGroup(String group) {} def resumeJobGroup(String group) {} def pauseAll() {} def resumeAll() {} def removeJob(String group, String name) {} def unscheduleJob(String group, String name) {} def interruptJob(String group, String name) {}
grails.plugins.quartz.JobManagerService
類MyJob.schedule(String cronExpression, Map params) // 建立cron觸發器 MyJob.schedule(Long repeatInterval, Integer repeatCount, Map params) // 建立簡單的觸發器:以repeatInterval毫秒的延遲重複做業repeatCount + 1次 MyJob.schedule(Date scheduleDate, Map params) // 將一個做業執行安排到特定日期 MyJob.schedule(Trigger trigger) // 使用自定義觸發器來安排做業的執行 MyJob.triggerNow(Map params) // 強制當即執行工做