forked from nebolsin/grails-quartz
-
Notifications
You must be signed in to change notification settings - Fork 1
/
QuartzGrailsPlugin.groovy
294 lines (258 loc) · 12 KB
/
QuartzGrailsPlugin.groovy
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* Copyright 2004-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.codehaus.groovy.grails.plugins.quartz.*
import org.codehaus.groovy.grails.plugins.quartz.GrailsTaskClassProperty as GTCP
import org.codehaus.groovy.grails.plugins.quartz.listeners.*
import org.codehaus.groovy.grails.commons.*
import org.springframework.beans.factory.config.MethodInvokingFactoryBean
import org.springframework.scheduling.quartz.SchedulerFactoryBean
import grails.util.GrailsUtil
import org.springframework.context.ApplicationContext
import org.quartz.Scheduler
import org.quartz.CronTrigger
import org.quartz.Trigger
import org.quartz.SimpleTrigger
import org.quartz.JobDataMap
/**
* A plug-in that configures Quartz job support for Grails.
*
*
* @author Graeme Rocher
* @author Marcel Overdijk
* @author Sergey Nebolsin
*/
class QuartzGrailsPlugin {
def version = "0.4.2"
def author = "Sergey Nebolsin"
def authorEmail = "[email protected]"
def title = "This plugin adds Quartz job scheduling features to Grails application."
def description = '''\
Quartz plugin allows your Grails application 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.
'''
def documentation = "http://grails.org/Quartz+plugin"
def loadAfter = ['core', 'hibernate']
def watchedResources = [
"file:./grails-app/jobs/**/*Job.groovy",
"file:./plugins/*/grails-app/jobs/**/*Job.groovy"
]
def artefacts = [new TaskArtefactHandler()]
def doWithSpring = {
def config = loadQuartzConfig()
application.taskClasses.each {jobClass ->
configureJobBeans.delegate = delegate
configureJobBeans(jobClass)
}
// register SessionBinderJobListener to bind Hibernate Session to each Job's thread
"${SessionBinderJobListener.NAME}"(SessionBinderJobListener) {bean ->
bean.autowire = "byName"
}
// register global ExceptionPrinterJobListener which will log exceptions occured
// during job's execution
"${ExceptionPrinterJobListener.NAME}"(ExceptionPrinterJobListener)
quartzJobFactory(GrailsJobFactory)
quartzScheduler(SchedulerFactoryBean) {
// delay scheduler startup to after-bootstrap stage
configLocation = "classpath:quartz.properties"
autoStartup = false
if(config.jdbcStore) {
dataSource = ref('dataSource')
transactionManager = ref('transactionManager')
}
jobFactory = quartzJobFactory
jobListeners = [ref("${SessionBinderJobListener.NAME}")]
globalJobListeners = [ref("${ExceptionPrinterJobListener.NAME}")]
}
}
def doWithDynamicMethods = {ctx ->
def random = new Random()
Scheduler quartzScheduler = ctx.getBean('quartzScheduler')
application.taskClasses.each {GrailsTaskClass tc ->
def mc = tc.metaClass
def jobName = tc.getFullName()
def jobGroup = tc.getGroup()
def generateTriggerName = { ->
long r = random.nextLong()
if (r < 0) {
r = -r;
}
return "GRAILS_" + Long.toString(r, 30 + (int) (System.currentTimeMillis() % 7));
}
mc.'static'.schedule = { String cronExpression, Map params = null ->
Trigger trigger = new CronTrigger(generateTriggerName(), GTCP.DEFAULT_TRIGGERS_GROUP, jobName, jobGroup, cronExpression)
if(tc.getVolatility()) trigger.setVolatility(true)
if(params) trigger.jobDataMap.putAll(params)
quartzScheduler.scheduleJob(trigger)
}
mc.'static'.schedule = {Long interval, Integer repeatCount = SimpleTrigger.REPEAT_INDEFINITELY, Map params = null ->
Trigger trigger = new SimpleTrigger(generateTriggerName(), GTCP.DEFAULT_TRIGGERS_GROUP, jobName, jobGroup, new Date(), null, repeatCount, interval)
if(tc.getVolatility()) trigger.setVolatility(true)
if(params) trigger.jobDataMap.putAll(params)
quartzScheduler.scheduleJob(trigger)
}
mc.'static'.schedule = {Date scheduleDate ->
Trigger trigger = new SimpleTrigger(generateTriggerName(), GTCP.DEFAULT_TRIGGERS_GROUP, jobName, jobGroup, scheduleDate, null, 0, 0)
if(tc.getVolatility()) trigger.setVolatility(true)
quartzScheduler.scheduleJob(trigger)
}
mc.'static'.schedule = {Date scheduleDate, Map params ->
Trigger trigger = new SimpleTrigger(generateTriggerName(), GTCP.DEFAULT_TRIGGERS_GROUP, jobName, jobGroup, scheduleDate, null, 0, 0)
if(tc.getVolatility()) trigger.setVolatility(true)
if(params) trigger.jobDataMap.putAll(params)
quartzScheduler.scheduleJob(trigger)
}
mc.'static'.schedule = {Trigger trigger ->
trigger.jobName = jobName
trigger.jobGroup = jobGroup
quartzScheduler.scheduleJob(trigger)
}
mc.'static'.triggerNow = { Map params = null ->
if(tc.getVolatility()){
quartzScheduler.triggerJobWithVolatileTrigger(jobName, jobGroup, params ? new JobDataMap(params) : null)
} else {
quartzScheduler.triggerJob(jobName, jobGroup, params ? new JobDataMap(params) : null)
}
}
mc.'static'.removeJob = {
quartzScheduler.deleteJob(jobName, jobGroup)
}
mc.'static'.reschedule = {Trigger trigger ->
trigger.jobName = jobName
trigger.jobGroup = jobGroup
quartzScheduler.rescheduleJob(trigger.name, trigger.group, trigger)
}
mc.'static'.unschedule = {String triggerName, String triggerGroup = GTCP.DEFAULT_TRIGGERS_GROUP ->
quartzScheduler.unscheduleJob(triggerName, triggerGroup)
}
}
}
def doWithApplicationContext = {applicationContext ->
application.taskClasses.each {jobClass ->
scheduleJob.delegate = delegate
scheduleJob(jobClass, applicationContext)
}
}
def onShutdown = {event ->
def ctx = event.ctx
if(!ctx) {
log.error("Application context not found. Cannot execute shutdown code.")
return
}
def scheduler = ctx.getBean("quartzScheduler")
if(scheduler) {
scheduler.shutdown()
}
}
def onChange = {event ->
if(application.isArtefactOfType(TaskArtefactHandler.TYPE, event.source)) {
log.debug("Job ${event.source} changed. Reloading...")
def context = event.ctx
def scheduler = context?.getBean("quartzScheduler")
// get quartz scheduler
if(context && scheduler) {
// if job already exists, delete it from scheduler
def jobClass = application.getTaskClass(event.source?.name)
if(jobClass) {
scheduler.deleteJob(jobClass.fullName, jobClass.group)
log.debug("Job ${jobClass.fullName} deleted from the scheduler")
}
// add job artefact to application
jobClass = application.addArtefact(TaskArtefactHandler.TYPE, event.source)
// configure and register job beans
def fullName = jobClass.fullName
def beans = beans {
configureJobBeans.delegate = delegate
configureJobBeans(jobClass)
}
context.registerBeanDefinition("${fullName}Class", beans.getBeanDefinition("${fullName}Class"))
context.registerBeanDefinition("${fullName}", beans.getBeanDefinition("${fullName}"))
context.registerBeanDefinition("${fullName}Detail", beans.getBeanDefinition("${fullName}Detail"))
jobClass.triggers.each {name, trigger ->
event.ctx.registerBeanDefinition("${name}Trigger", beans.getBeanDefinition("${name}Trigger"))
}
scheduleJob(jobClass, event.ctx)
} else {
log.error("Application context or Quartz Scheduler not found. Can't reload Quartz plugin.")
}
}
}
def scheduleJob = {GrailsTaskClass jobClass, ApplicationContext ctx ->
def scheduler = ctx.getBean("quartzScheduler")
if(scheduler) {
def fullName = jobClass.fullName
// add job to scheduler, and associate triggers with it
scheduler.addJob(ctx.getBean("${fullName}Detail"), true)
jobClass.triggers.each {key, trigger ->
log.debug("Scheduling $fullName with trigger $key: ${trigger}")
if(scheduler.getTrigger(trigger.triggerAttributes.name, trigger.triggerAttributes.group)) {
scheduler.rescheduleJob(trigger.triggerAttributes.name, trigger.triggerAttributes.group, ctx.getBean("${key}Trigger"))
} else {
scheduler.scheduleJob(ctx.getBean("${key}Trigger"))
}
}
log.debug("Job ${jobClass.fullName} scheduled")
} else {
log.error("Failed to register job triggers: scheduler not found")
}
}
def configureJobBeans = {GrailsTaskClass jobClass ->
def fullName = jobClass.fullName
"${fullName}Class"(MethodInvokingFactoryBean) {
targetObject = ref("grailsApplication", true)
targetMethod = "getArtefact"
arguments = [TaskArtefactHandler.TYPE, jobClass.fullName]
}
"${fullName}"(ref("${fullName}Class")) {bean ->
bean.factoryMethod = "newInstance"
bean.autowire = "byName"
bean.scope = "prototype"
}
"${fullName}Detail"(JobDetailFactoryBean) {
name = fullName
group = jobClass.group
concurrent = jobClass.concurrent
volatility = jobClass.volatility
durability = jobClass.durability
if(jobClass.sessionRequired) {
jobListenerNames = ["${SessionBinderJobListener.NAME}"] as String[]
}
}
// registering triggers
jobClass.triggers.each {name, trigger ->
"${name}Trigger"(trigger.clazz) {
jobDetail = ref("${fullName}Detail")
trigger.properties.findAll {it.key != 'clazz'}.each {
delegate["${it.key}"] = it.value
}
}
}
}
private ConfigObject loadQuartzConfig() {
def config = ConfigurationHolder.config
GroovyClassLoader classLoader = new GroovyClassLoader(getClass().classLoader)
// merging default Quartz config into main application config
config.merge(new ConfigSlurper(GrailsUtil.environment).parse(classLoader.loadClass('DefaultQuartzConfig')))
// merging user-defined Quartz config into main application config if provided
try {
config.merge(new ConfigSlurper(GrailsUtil.environment).parse(classLoader.loadClass('QuartzConfig')))
} catch (Exception ignored) {
// ignore, just use the defaults
}
return config.quartz
}
}