Japanese Job Scheduling (Quartz)

Last updated by admin 3 years ago

????????????

??

Grails allows you to schedule jobs to be executed using a specified interval or cron expression. The underlying system uses the Quartz Enterprise Job Scheduler configured via Spring, but is made simpler by the coding by convention paradigm.

Grails???????????cron?????????????????????????????????Spring????????Quartz???????????????????????????????????????????????????????????????

???????? ???

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 create-job"????????????????????Grails????????????????"grails-app/jobs"???????????:

class MyJob {
  def startDelay = 100
  def timeout = 1000

def name = "MyJob" def group = "MyGroup"

def execute(){ print "Job run!" } }

The above example will call the execute method every second. The 'timeout' and 'startDelay' properties are specified in milliseconds. Jobs can optionally be placed groups and given a name.

?????????execute??????????????'timeout'?'startDelay'????????????????????????????????groups?name????????

????????????????

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):

?????cron?????????????????????"cron"??????????????????????????????????????8:00AM????????????????????1:30AM????????????????????????????? (cron??????????Quartz?CronTrigger????API docs???):

class MyJob  {
 def cronExpression = "0 0 6 * * ?"

def name = "MyTask" def group = "MyGroup"

def execute(){ print "Job run!" } }

Dependency Injection and Jobs

Jobs are configured for auto-wiring by name thus properties can be injected into them. To get hold of the data source you can do:

def javax.sql.DataSource dataSource
Or a service class:
def MyService myService
The Grails application context is configured using convention so most types can be injected simply by using their property name representation (ie the first letter being lower case in the majority of cases).