-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAsynchronousMailGrailsPlugin.groovy
More file actions
80 lines (69 loc) · 3.59 KB
/
AsynchronousMailGrailsPlugin.groovy
File metadata and controls
80 lines (69 loc) · 3.59 KB
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
package grails.plugin.asyncmail
import grails.plugins.Plugin
import grails.plugins.quartz.JobDescriptor
import grails.plugins.quartz.JobManagerService
import grails.plugins.quartz.TriggerDescriptor
import groovy.util.logging.Slf4j
import org.quartz.Scheduler
import org.quartz.TriggerKey
@Slf4j
@SuppressWarnings('unused')
class AsynchronousMailGrailsPlugin extends Plugin {
def grailsVersion = "6.0.0 > *"
def dependsOn = [mail: "* > 4.0.0"]
def loadAfter = ['mail', 'quartz', 'hibernate', 'hibernate3', 'hibernate4', 'hibernate5', 'mongodb']
@Override
Closure doWithSpring() {
{ ->
//noinspection GrUnresolvedAccess
asynchronousMailMessageBuilderFactory(AsynchronousMailMessageBuilderFactory) { it.autowire = true }
asynchronousMailService(AsynchronousMailService) { it.autowire = true }
//noinspection GrUnresolvedAccess
springConfig.addAlias 'asyncMailService', 'asynchronousMailService'
}
}
@Override
void onStartup(Map<String, Object> event) {
startJobs(applicationContext)
}
/**
* Start the send job and the messages collector.
*
* If the plugin is used in cluster we have to remove old triggers.
*/
def startJobs(applicationContext) {
def config = grailsApplication.config
if (!config.getProperty('asynchronous.mail.disable', Boolean)) {
JobManagerService jobManagerService = applicationContext.jobManagerService
Scheduler quartzScheduler = applicationContext.getBean('quartzScheduler', Scheduler)
// Get our jobs
List<JobDescriptor> jobDescriptors = jobManagerService.getJobs("AsynchronousMail")
// Remove old triggers for the send job
log.debug("Removing old triggers for the AsynchronousMailJob")
JobDescriptor sjd = jobDescriptors.find { it.name == 'grails.plugin.asyncmail.AsynchronousMailJob' }
sjd?.triggerDescriptors?.each {TriggerDescriptor td ->
def triggerKey = new TriggerKey(td.name, td.group)
quartzScheduler.unscheduleJob(triggerKey)
log.debug("Removed the trigger ${triggerKey} for the AsynchronousMailJob")
}
// Schedule the send job
def sendInterval = config.getProperty('asynchronous.mail.send.repeat.interval', Long)
log.debug("Scheduling the AsynchronousMailJob with repeat interval ${sendInterval}ms")
AsynchronousMailJob.schedule(sendInterval)
log.debug("Scheduled the AsynchronousMailJob with repeat interval ${sendInterval}ms")
// Remove old triggers for the collector job
log.debug("Removing old triggers for the ExpiredMessagesCollectorJob")
JobDescriptor cjd = jobDescriptors.find { it.name == 'grails.plugin.asyncmail.ExpiredMessagesCollectorJob' }
cjd?.triggerDescriptors?.each {TriggerDescriptor td ->
def triggerKey = new TriggerKey(td.name, td.group)
quartzScheduler.unscheduleJob(triggerKey)
log.debug("Removed the trigger ${triggerKey} for the ExpiredMessagesCollectorJob")
}
// Schedule the collector job
def collectInterval = config.getProperty('asynchronous.mail.expired.collector.repeat.interval', Long)
log.debug("Scheduling the ExpiredMessagesCollectorJob with repeat interval ${collectInterval}ms")
ExpiredMessagesCollectorJob.schedule(collectInterval)
log.debug("Scheduled the ExpiredMessagesCollectorJob with repeat interval ${collectInterval}ms")
}
}
}