-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGrailsKafkaGrailsPlugin.groovy
77 lines (66 loc) · 2.75 KB
/
GrailsKafkaGrailsPlugin.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
import smartthings.kafka.KafkaConfigHolder
import smartthings.kafka.ServiceWrapper
import smartthings.kafka.WrappedKafkaListener
import smartthings.konsumer.MessageProcessor
class GrailsKafkaGrailsPlugin {
// the plugin version
def version = "0.0.4-SNAPSHOT"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "2.3 > *"
// resources that are excluded from plugin packaging
def pluginExcludes = [
"grails-app/views/error.gsp"
]
def title = "Grails Kafka Plugin"
def author = "Charlie Knudsen"
def authorEmail = "[email protected]"
def description = '''\
Plugin for setting up kafka consumers.
'''
def documentation = "https://github.com/charliek/grails-kafka"
def license = "APACHE"
def issueManagement = [ system: "github", url: "https://github.com/charliek/grails-kafka/issues" ]
def scm = [ url: "https://github.com/charliek/grails-kafka" ]
def doWithWebDescriptor = { xml ->
// Implement additions to web.xml (optional), this event occurs before
}
def doWithSpring = {
def kafkaConfig = application.config.kafka
def configHolder = new KafkaConfigHolder(kafkaConfig)
for(def service in application.serviceClasses) {
ServiceWrapper wrappedService = new ServiceWrapper(service, configHolder)
if (wrappedService.shouldProcess()) {
log.info("Setting up ${service} as kafka consumer '${wrappedService.consumerName}' listening to topic '${wrappedService.topicName}'")
"${wrappedService.springBeanName}"(WrappedKafkaListener, service.clazz, wrappedService.getConfig())
}
}
}
def doWithDynamicMethods = { ctx ->
// Registering dynamic methods to classes
}
def doWithApplicationContext = { ctx ->
// Post initialization spring config
Map<String, WrappedKafkaListener> listenerBeans = ctx.getBeansOfType(WrappedKafkaListener)
listenerBeans.each { k, v ->
log.info("Starting kafka listener for bean ${k}")
v.run((MessageProcessor) ctx.getBean(v.processorClass))
}
}
def onChange = { event ->
// Code that is executed when any artefact that this plugin is
// watching is modified and reloaded. The event contains: event.source,
// event.application, event.manager, event.ctx, and event.plugin.
}
def onConfigChange = { event ->
// Code that is executed when the project configuration changes.
// The event is the same as for 'onChange'.
}
def onShutdown = { event ->
// Code that is executed when the application shuts down
Map<String, WrappedKafkaListener> listenerBeans = event.ctx.getBeansOfType(WrappedKafkaListener)
listenerBeans.each { k, v ->
log.info("Shutting down listener for bean ${k}")
v.shutdown()
}
}
}