forked from grails/grails-quartz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuartzGrailsPlugin.groovy
370 lines (310 loc) · 14.5 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Copyright (c) 2011 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 grails.plugins.quartz.GrailsJobClassConstants as Constants
import grails.plugins.quartz.listeners.ExceptionPrinterJobListener
import grails.plugins.quartz.listeners.SessionBinderJobListener
import grails.util.GrailsUtil
import org.codehaus.groovy.grails.commons.ConfigurationHolder
import org.springframework.beans.factory.config.MethodInvokingFactoryBean
import org.springframework.context.ApplicationContext
import org.springframework.scheduling.quartz.SchedulerFactoryBean
import grails.plugins.quartz.*
import org.quartz.*
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
/**
* A plug-in that configures Quartz job support for Grails.
*
*
* @author Graeme Rocher
* @author Marcel Overdijk
* @author Sergey Nebolsin
* @author Ryan Vanderwerf
*/
class QuartzGrailsPlugin {
def version = "1.0-RC5"
def grailsVersion = "1.2 > *"
def author = "Sergey Nebolsin, Graeme Rocher, Ryan Vanderwerf"
def authorEmail = "[email protected]"
def title = "Quartz plugin for Grails"
def description = '''\
This plugin adds Quartz job scheduling features to Grails application.
'''
def documentation = "http://grails.org/plugin/quartz"
def pluginExcludes = ['grails-app/jobs/**']
def license = "APACHE"
def issueManagement = [system: "GitHub Issues", url: "http://jira.grails.org/browse/GPQUARTZ"]
def scm = [url: "http://github.com/grails-plugins/grails-quartz"]
def loadAfter = ['core', 'hibernate', 'datasources']
def watchedResources = [
"file:./grails-app/jobs/**/*Job.groovy",
"file:./plugins/*/grails-app/jobs/**/*Job.groovy"
]
def artefacts = [new JobArtefactHandler()]
def doWithSpring = { context ->
def config = loadQuartzConfig()
application.jobClasses.each { jobClass ->
configureJobBeans.delegate = delegate
configureJobBeans(jobClass, manager.hasGrailsPlugin("hibernate"))
}
if (manager?.hasGrailsPlugin("hibernate")) {
// 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) {
quartzProperties = config._properties
// delay scheduler startup to after-bootstrap stage
autoStartup = false
if (config.jdbcStore) {
dataSource = ref('dataSource')
transactionManager = ref('transactionManager')
}
waitForJobsToCompleteOnShutdown = config.waitForJobsToCompleteOnShutdown
exposeSchedulerInRepository = config.exposeSchedulerInRepository
jobFactory = quartzJobFactory
if (manager?.hasGrailsPlugin("hibernate")) {
globalJobListeners = [ref("${SessionBinderJobListener.NAME}"), ref("${ExceptionPrinterJobListener.NAME}")]
} else {
globalJobListeners = [ref("${ExceptionPrinterJobListener.NAME}")]
}
}
}
def doWithDynamicMethods = {ctx ->
def random = new Random()
Scheduler quartzScheduler = ctx.getBean('quartzScheduler')
application.jobClasses.each {GrailsJobClass 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 = newTrigger()
.withIdentity(generateTriggerName(),Constants.DEFAULT_TRIGGERS_GROUP)
.withPriority(6)
.forJob(jobName,jobGroup)
.withSchedule(CronScheduleBuilder.cronSchedule(cronExpression))
.build();
if (params) trigger.jobDataMap.putAll(params)
quartzScheduler.scheduleJob(trigger)
}
mc.'static'.schedule = {Long interval, Integer repeatCount = SimpleTrigger.REPEAT_INDEFINITELY, Map params = null ->
Trigger trigger = newTrigger()
.withIdentity(generateTriggerName(),Constants.DEFAULT_TRIGGERS_GROUP)
.withPriority(6)
.forJob(jobName,jobGroup)
.withSchedule(simpleSchedule()
.withIntervalInMilliseconds(interval)
.repeatForever())
.build();
if (params) trigger.jobDataMap.putAll(params)
quartzScheduler.scheduleJob(trigger)
}
mc.'static'.schedule = {Date scheduleDate ->
Trigger trigger = newTrigger()
.withIdentity(generateTriggerName(),Constants.DEFAULT_TRIGGERS_GROUP)
.withPriority(6)
.forJob(jobName,jobGroup)
.startAt(scheduleDate)
.build();
quartzScheduler.scheduleJob(trigger)
}
mc.'static'.schedule = {Date scheduleDate, Map params ->
Trigger trigger = newTrigger()
.withIdentity(generateTriggerName(),Constants.DEFAULT_TRIGGERS_GROUP)
.withPriority(6)
.forJob(jobName,jobGroup)
.startAt(scheduleDate)
.build();
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 ->
quartzScheduler.triggerJob(new JobKey(jobName, jobGroup), params ? new JobDataMap(params) : null)
}
mc.'static'.removeJob = {
quartzScheduler.deleteJob(new JobKey(jobName, jobGroup))
}
mc.'static'.reschedule = { Trigger trigger ->
trigger.jobName = jobName
trigger.jobGroup = jobGroup
quartzScheduler.rescheduleJob(trigger.getKey(), trigger)
}
mc.'static'.unschedule = { String triggerName, String triggerGroup = Constants.DEFAULT_TRIGGERS_GROUP ->
quartzScheduler.unscheduleJob(TriggerKey.triggerKey(triggerName, triggerGroup))
}
}
}
def doWithApplicationContext = {applicationContext ->
application.jobClasses.each {jobClass ->
scheduleJob.delegate = delegate
scheduleJob(jobClass, applicationContext)
}
log.debug("Scheduled Job Classes Count:"+application.jobClasses.size())
}
def onChange = {event ->
if (application.isArtefactOfType(JobArtefactHandler.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.getJobClass(event.source?.name)
if (jobClass) {
def jobKey = new org.quartz.JobKey(jobClass.fullName, jobClass.group)
scheduler.deleteJob(jobKey)
log.debug("Job ${jobClass.fullName} deleted from the scheduler")
}
// add job artefact to application
jobClass = application.addArtefact(JobArtefactHandler.TYPE, event.source)
// configure and register job beans
def fullName = jobClass.fullName
def beans = beans {
configureJobBeans.delegate = delegate
configureJobBeans(jobClass, manager.hasGrailsPlugin("hibernate"))
}
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 = {GrailsJobClass jobClass, ApplicationContext ctx ->
def scheduler = ctx.getBean("quartzScheduler")
if (scheduler) {
def fullName = jobClass.fullName
// add job to scheduler, and associate triggers with it
if (ctx.getBean("${fullName}Detail")) {
scheduler.addJob(ctx.getBean("${fullName}Detail"), true)
jobClass.triggers.each {key, trigger ->
TriggerKey triggerKey = new TriggerKey(trigger.triggerAttributes.name,trigger.triggerAttributes.group)
log.debug("Scheduling $fullName with trigger $key: ${trigger}")
if (scheduler.getTrigger(triggerKey)) {
scheduler.rescheduleJob(triggerKey, ctx.getBean("${key}Trigger"))
} else {
scheduler.scheduleJob(ctx.getBean("${key}Trigger"))
}
}
log.debug("Job ${jobClass.fullName} scheduled")
} else {
log.error("Error scheduling job, ${fullName}Detail not found in ApplicationContext!")
}
} else {
log.error("Failed to register job triggers: scheduler not found")
}
}
def configureJobBeans = {GrailsJobClass jobClass, boolean hasHibernate = true ->
def fullName = jobClass.fullName
try {
"${fullName}Class"(MethodInvokingFactoryBean) {
targetObject = ref("grailsApplication", true)
targetMethod = "getArtefact"
arguments = [JobArtefactHandler.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
durability = jobClass.durability
requestsRecovery = jobClass.requestsRecovery
if (hasHibernate && jobClass.sessionRequired) {
jobListenerNames = ["${SessionBinderJobListener.NAME}"] as String[]
}
}
} catch (Exception e) {
log.error("Error declaring ${fullName}Detail bean in context",e)
}
// registering triggers
try {
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
}
}
}
} catch (Exception te) {
log.error("Error registering triggers",te)
}
}
/*
* Load the various configs.
* Order of priority has been "fixed" in 1.0-RC2 to be:
*
* 1. DefaultQuartzConfig is loaded
* 2. App's Config.groovy is loaded in and overwrites anything from DQC
* 3. QuartzConfig is loaded and overwrites anything from DQC or AppConfig
* 4. quartz.properties are loaded into config as quartz._props
*/
private ConfigObject loadQuartzConfig() {
def config = ConfigurationHolder.config
def classLoader = new GroovyClassLoader(getClass().classLoader)
// Note here the order of objects when calling merge - merge OVERWRITES values in the target object
// Load default Quartz config as a basis
def newConfig = new ConfigSlurper(GrailsUtil.environment).parse(classLoader.loadClass('DefaultQuartzConfig'))
// Overwrite defaults with what Config.groovy has supplied, perhaps from external files
newConfig.merge(config)
// Overwrite with contents of QuartzConfig
try {
newConfig.merge(new ConfigSlurper(GrailsUtil.environment).parse(classLoader.loadClass('QuartzConfig')))
} catch (Exception ignored) {
// ignore, just use the defaults
}
// Now merge our correctly merged DefaultQuartzConfig and QuartzConfig into the main config
config.merge(newConfig)
// And now load quartz properties into main config
def properties = new Properties()
def resource = classLoader.getResourceAsStream("quartz.properties")
if (resource != null) {
properties.load(resource)
}
if (config.quartz.containsKey('props')) {
properties << config.quartz.props.toProperties('org.quartz')
}
config.quartz._properties = properties
return config.quartz
}
}