1. 程式人生 > >Grails定時器和傳統定時器

Grails定時器和傳統定時器

grails可以使用兩種方法來編寫quartz定時任務

一、使用grails quartz plugin 
關於使用quartz外掛來做定時任務非常的方便,這裡只是把grails對quartz做的外掛文件搬了過來,一看應該就明白了

1.引用quartz

這裡我使用的是2.0.0.M4版本,官方文件上是1.0.2,這裡隨意,大丈夫

dependencies {
    compile 'org.grails.plugins:quartz:2.0.0.M4'
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

2.使用quartz

★★Scheduling Jobs 
To create a new job run the “grails create-job” command and enter the name of the job. Grails will create a new job and place it in the “grails-app/jobs” directory: 
這裡使用的是命令列建立,如果不想用命令列,直接在grails-app/jobs下建立Job類即可,但是路徑必須是grails-app/jobs這個路徑,否則會出錯。

class MyJob {
  static triggers = {
    simple name: 'mySimpleTrigger', startDelay: 60000, repeatInterval: 1000  
  }
  def group = "MyGroup"
  def description = "Example job with Simple Trigger"
  def execute(){
    print "Job run!"
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

The above example will wait for 1 minute and after that will call the ‘execute’ method every second. The ‘repeatInterval’ and ‘startDelay’ properties are specified in milliseconds and must have Integer or Long type. If these properties are not specified default values are applied (1 minute for ‘repeatInterval’ property and 30 seconds for ‘startDelay’ property). Jobs can optionally be placed in different groups. The triggers name property must be unique across all triggers in the application. 
simple name:triggers 名稱——整個專案中不能重複

 
startDelay:延遲時間(毫秒單位)——多少毫秒後開始執行任務 
repeatInterval:任務迴圈時間(毫秒單位)——任務以多少毫秒迴圈執行(注意:0:表示執行一次,-1:表示不執行) 
By default, jobs will not be executed when running under the test environment. 
(預設 任務在test環境下不會執行)

★★Scheduling a Cron Job 
Jobs can be scheduled using a cron expression. For those unfamiliar with “cron”, this means being able to create a firing schedule such as: “At 8:00am every Monday through Friday” or “At 1:30am every last Friday of the month”. (See the API docs for the CronTrigger class in Quartz for more info on cron expressions):

class MyJob  {
  static triggers = {
    cron name: 'myTrigger', cronExpression: "0 0 6 * * ?"
  }
 def group = "MyGroup"
 def description = "Example job with Cron Trigger"
 def execute(){
   print "Job run!"
 }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

The fields in the cronExpression are: (summarizing the Quartz CronTrigger Tutorial)

cronExpression: "s m h D M W Y"
                 | | | | | | `- Year [optional]
                 | | | | | `- Day of Week, 1-7 or SUN-SAT, ?
                 | | | | `- Month, 1-12 or JAN-DEC
                 | | | `- Day of Month, 1-31, ?
                 | | `- Hour, 0-23
                 | `- Minute, 0-59
                 `- Second, 0-59
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Year is the only optional field and may be omitted, the rest are mandatory 
Day-of-Week and Month are case insensitive, so “DEC” = “dec” = “Dec” 
Either Day-of-Week or Day-of-Month must be “?”, or you will get an error since support by the underlying library is not complete. So you can’t specify both fields, nor leave both as the all values wildcard “*”; this is a departure from the unix crontab specification. 
See the CronTrigger Tutorial for an explanation of all the special characters you may use. 
這裡主要說的是quartz的一種定時設定規則,詳細的規則可以搜尋quartz的定時規則,這裡也只是粗略的說了一下

3.配置quartz 
以上面的方式配置完畢後,任務預設會以配置的定時方式在專案開啟時執行,如果不想設定自啟動,可修改配置

Since 0.3 version plugin supports configuration file which is stored in grails-app/conf/QuartzConfig.groovy. The syntax is the same as default Grails configuration file Config.groovy . You could also use per-environment configuration feature (more info).

To have an initial Quartz config file generated for you, type the following in the command line: ‘grails install-quartz-config’ . This will generate a file that looks like this:


quartz {
    autoStartup = true
    jdbcStore = false
}
environments {
    test {
        quartz {
            autoStartup = false
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Currently supported options: 
●autoStartup :controls automatic startup of the Quartz scheduler during application bootstrap (default: true ) 
●jdbcStore :set to true if you want Quartz to persist jobs in your DB (default: false ), you’ll also need to provide quartz.properties file and make sure that required tables exist in your db (see Clustering section below for the sample config and automatic tables creation usinghibernate)

You could also create grails-app/conf/quartz.properties file and provide different options to the Quartz scheduler (see Quartz configuration reference for details). 
Logging 
A log is auto-injected into your task Job class without having to enable it. To set the logging level, just add something like this to your grails-app/conf/Config.groovy log4j configuration.

debug 'grails.app.jobs'
  • 1
  • 1

Hibernate Sessions and Jobs 
Jobs are configured by default to have Hibernate Session bounded to thread each time job is executed. This is required if you are using Hibernate code which requires open session (such as lazy loading of collections) or working with domain objects with unique persistent constraint (it uses Hibernate Session behind the scene). If you want to override this behavior (rarely useful) you can use ‘sessionRequired’ property:

def sessionRequired = false
  • 1
  • 1

Configuring concurrent execution 
By default Jobs are executed in concurrent fashion, so new Job execution can start even if previous execution of the same Job is still running. If you want to override this behavior you can use ‘concurrent’ property, in this case Quartz’s StatefulJob will be used (you can find more info about it here):

def concurrent = false
  • 1
  • 1

Configuring description 
Quartz allows for each job to have a short description. This may be configured by adding a description field to your Job. The description can be accessed at runtime using the JobManagerService and inspecting the JobDetail object.

def description = "Example Job Description"
  • 1
  • 1

Clustering 
●Quartz plugin doesn’t support clustering out-of-the-box now. However, you could use standard Quartz clustering configuration. Take a look at the example provided by Burt Beckwith. You’ll also need to set jdbcStore configuration option to true . 
●There are also two parameters for configuring store/clustering on jobs ( volatility and durability , both are true by default) and one for triggers ( volatility , also true by default). Volatile job and trigger will not persist between Quartz runs, and durable job will live even when there is no triggers referring to it. 
●Read Quartz documentation for more information on clustering and job stores as well as volatility and durability. 
●Now that the plugin supports Quartz 2.1.x, you can now use current versions of open source Terracotta seehttps://github.com/rvanderwerf/terracotta-grails-demo for an example app.

4.延伸理解trigger Understanding Triggers - Reference Documentation

Scheduling configuration syntax 
Currently plugin supports three types of triggers:

●simple — executes once per defined interval (ex. “every 10 seconds”); 
●cron — executes job with cron expression (ex. “at 8:00am every Monday through Friday”); 
●custom — your implementation of Trigger interface. 
Multiple triggers per job are allowed.

class MyJob {
  static triggers = {
    simple name:'simpleTrigger', startDelay:10000, repeatInterval: 30000, repeatCount: 10
    cron name:'cronTrigger', startDelay:10000, cronExpression: '0/6 * 15 * * ?'
    custom name:'customTrigger', triggerClass:MyTriggerClass, myParam:myValue, myAnotherParam:myAnotherValue
  }
  def execute() {
    println "Job run!"
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

With this configuration job will be executed 11 times with 30 seconds interval with first run in 10 seconds after scheduler startup (simple trigger), also it’ll be executed each 6 second during 15th hour (15:00:00, 15:00:06, 15:00:12, … — this configured by cron trigger) and also it’ll be executed each time your custom trigger will fire. 
Three kinds of triggers are supported with the following parameters: 
●simple: 
name — the name that identifies the trigger; 
startDelay — delay (in milliseconds) between scheduler startup and first job’s execution; 
repeatInterval — timeout (in milliseconds) between consecutive job’s executions; 
repeatCount — trigger will fire job execution (1 + repeatCount) times and stop after that (specify 0 here to have one-shot job or -1 to repeat job executions indefinitely); 
●cron: 
name — the name that identifies the trigger; 
startDelay — delay (in milliseconds) between scheduler startup and first job’s execution; 
cronExpression — cron expression 
●custom: 
triggerClass — your class which implements Trigger interface;

any params needed by your trigger.

Dynamic Jobs Scheduling

Starting from 0.4.1 version you have the ability to schedule job executions dynamically. 
These methods are available:

// creates cron trigger;
MyJob.schedule(String cronExpression, Map params?)
//  creates simple trigger: repeats job repeatCount+1 times with delay of repeatInterval milliseconds;
MyJob.schedule(Long repeatInterval, Integer repeatCount?, Map params?) )

// schedules one job execution to the specific date;
MyJob.schedule(Date scheduleDate, Map params?)

//schedules job's execution with a custom trigger;
MyJob.schedule(Trigger trigger)

// force immediate execution of the job.
MyJob.triggerNow(Map params?)

// Each method (except the one for custom trigger) takes optional 'params' argument. 
// You can use it to pass some data to your job and then access it from the job:
class MyJob { 
  def execute(context) {
    println context.mergedJobDataMap.get('foo')
  }
}
// now in your controller (or service, or something else):

MyJob.triggerNow([foo:"It Works!"])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

二、使用傳統quartz的配置

1.編寫任務

class MyJob implements Job {

    public void execute(JobExecutionContext jobExecutionContext) {
        //編寫任務
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.配置quartz

/**
 * 定時任務配置
 * @param jobName 任務名稱
 * @param triggerName 定時器名稱
 * @param groupName 任務組名稱
 * @param regularRules 定時規則
 * @return
 */
class QuartzUtil {

    private static Scheduler scheduler ;

    private  static CronTrigger trigger;

    /**
     * 新增一個Job任務
     * @param jobName
     * @param triggerName
     * @param groupName
     * @param regularRules
     * @return
     * @throws Exception
     */
    static createJobAndStart(String jobName,String triggerName,String groupName,String regularRules) throws Exception{

        scheduler = new StdSchedulerFactory().getScheduler();
        JobDetail jobDetail = null;
        //建立job
        jobDetail = JobBuilder.newJob(MyJob.class)
                .withIdentity(jobName,groupName).build();

        // 建立trigger
        trigger = TriggerBuilder.newTrigger()
                .withIdentity(triggerName,groupName)
                .withSchedule(CronScheduleBuilder.cronSchedule(regularRules))
                .build();
        scheduler.scheduleJob(jobDetail, trigger);
        startQuartz();
    }

    /**
     * 啟動排程器
     */
    static startQuartz(){
        scheduler.start();
    }

    /**
     * 關閉排程器
     * @return
     */
    static shutdownQuartz(){
        scheduler.shutdown(false);
    }

    /**
     * 替換一個job
     */
    static replaceJob(String jobName,String groupName){
        JobDetail jobDetail = JobBuilder.newJob(MyOtherJob.class)
                .withIdentity(jobName,groupName).build();
        scheduler.addJob(jobDetail,true);
    }

    /**
    * 判斷trigger是否存在
    */
    static ifExistByTrigger(){
        if(trigger){
           return true;
        }
        else return false;
    }
    /**
     * 更新一個trigger
     */
    static boolean updateTrigger(String triggerName ,String groupName,String regularRules,String type){
        boolean flag = false;
        scheduler = new StdSchedulerFactory().getScheduler();
        try {
            if(trigger && trigger.getKey()){
                CronTrigger trigger1 = TriggerBuilder.newTrigger()
                        .withIdentity(triggerName,groupName)
                        .withSchedule(CronScheduleBuilder.cronSchedule(regularRules))
                        .build();
                scheduler.rescheduleJob(trigger.getKey(), trigger1);
            }
            else{
                createJobAndStart("job1",triggerName,groupName,regularRules,type)
            }
            flag = true;
        } catch (Exception e) {
            LogUtil.logError("Quartz定時器更新Trigger出錯,錯誤資訊:"+e.message);
        }
        return flag;
    }

    /**
     * @Description: 移除一個任務
     *
     * @param jobName
     * @param jobGroupName
     * @param triggerName
     * @param triggerGroupName
     */
    public static void removeJob(String jobName, String jobGroupName,
                                 String triggerName, String triggerGroupName) {
        try {
            scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.pauseTrigger(triggerName, triggerGroupName);// 停止觸發器
            scheduler.unscheduleJob(triggerName, triggerGroupName);// 移除觸發器
            scheduler.deleteJob(jobName, jobGroupName);// 刪除任務
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @Description: 移除類中的trigger
     *
     */
    public static void removeTrigger() {
        if(trigger != null){
            trigger = null;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128

3.啟動任務

try {
                            boolean quartzFlag = QuartzUtil.updateTrigger("trigger1","group1","0 0/10 * * * ?");
                        } catch (Exception e) {
                            LogUtil.logError(e.message);
                            return;
                        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

我這裡把建立和更新任務放到一個方法去了,因為業務涉及到了更新任務,如果不需要則可以直接呼叫建立方法即可