-
Notifications
You must be signed in to change notification settings - Fork 1
/
JmsGrailsPlugin.groovy
317 lines (270 loc) · 12.3 KB
/
JmsGrailsPlugin.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
/*
* Copyright 2010 Grails Plugin Collective
*
* 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.plugin.jms.bean.JmsBeanDefinitionsBuilder
import grails.plugin.jms.listener.ListenerConfigFactory
import grails.plugin.jms.listener.ServiceInspector
import grails.util.Environment;
import grails.util.GrailsUtil
import org.apache.commons.logging.LogFactory
import org.codehaus.groovy.grails.commons.ServiceArtefactHandler
class JmsGrailsPlugin {
static LOG = LogFactory.getLog('grails.plugin.jms.JmsGrailsPlugin')
def version = "1.3-SNAPSHOT"
def author = "Grails Plugin Collective"
def authorEmail = "grails.plugin.collective@gmail.com"
def title = "JMS integration for Grails"
def grailsVersion = "1.2.0 > *"
def loadAfter = ['services', 'controllers','dataSource','hibernate', 'hibernate4']
def observe = ['services', 'controllers']
def pluginExcludes = [
"src/docs/**",
"**/grails/plugin/jms/test/**"
]
def listenerConfigs = [:]
def serviceInspector = new ServiceInspector()
def listenerConfigFactory = new ListenerConfigFactory()
def jmsConfigHash
def jmsConfig
def getDefaultConfig() {
new ConfigSlurper(Environment.current.name).parse(DefaultJmsBeans)
}
def getListenerConfigs(serviceClass, application) {
LOG.debug("inspecting '${serviceClass.name}' for JMS listeners")
serviceInspector.getListenerConfigs(serviceClass, listenerConfigFactory, application)
}
def registerListenerConfig(listenerConfig, beanBuilder) {
def queueOrTopic = (listenerConfig.topic) ? "TOPIC" : "QUEUE"
LOG.info "registering listener for '${listenerConfig.listenerMethodName}' of service '${listenerConfig.serviceBeanPrefix}' to ${queueOrTopic} '${listenerConfig.destinationName}'"
listenerConfig.register(beanBuilder)
}
def doWithSpring = {
jmsConfig = defaultConfig.merge(application.config.jms)
// We have to take a hash now because a config object
// will dynamically create nested maps as needed
jmsConfigHash = jmsConfig.hashCode()
LOG.debug("merged config: $jmsConfig")
if (jmsConfig.disabled) {
isDisabled = true
LOG.warn("not registering listeners because JMS is disabled")
return
}
new JmsBeanDefinitionsBuilder(jmsConfig).build(delegate)
application.serviceClasses?.each { service ->
def serviceClass = service.getClazz()
def serviceClassListenerConfigs = getListenerConfigs(serviceClass, application)
if (serviceClassListenerConfigs) {
serviceClassListenerConfigs.each {
registerListenerConfig(it, delegate)
}
listenerConfigs[serviceClass.name] = serviceClassListenerConfigs
}
}
}
def doWithApplicationContext = { applicationContext ->
//Fetch and set the asyncReceiverExecutor
try {
def asyncReceiverExecutor = applicationContext.getBean('jmsAsyncReceiverExecutor')
if (asyncReceiverExecutor) {
LOG.info "A jmsAsyncReceiverExecutor was detected in the Application Context and therefore will be set in the JmsService."
applicationContext.getBean('jmsService').asyncReceiverExecutor = asyncReceiverExecutor
}
}
catch (e) {
LOG.debug "No jmsAsyncReceiverExecutor was detected in the Application Context."
}
}
//Send*
def sendJMSMessage2 = { jmsService, destination, message ->
jmsService.send(destination, message)
}
def sendJMSMessage3 = { jmsService, destination, message, postProcessor ->
jmsService.send(destination, message, postProcessor)
}
def sendJMSMessage4 = { jmsService, destination, message, jmsTemplateBeanName, postProcessor ->
jmsService.send(destination, message, jmsTemplateBeanName, postProcessor)
}
def sendQueueJMSMessage2 = { jmsService, destination, message ->
jmsService.send(queue: destination, message)
}
def sendQueueJMSMessage3 = { jmsService, destination, message, postProcessor ->
jmsService.send(queue: destination, message, postProcessor)
}
def sendQueueJMSMessage4 = { jmsService, destination, message, jmsTemplateBeanName, postProcessor ->
jmsService.send(queue: destination, message, jmsTemplateBeanName, postProcessor)
}
def sendTopicJMSMessage2 = { jmsService, destination, message ->
jmsService.send(topic: destination, message)
}
def sendTopicJMSMessage3 = { jmsService, destination, message, postProcessor ->
jmsService.send(topic: destination, message, postProcessor)
}
def sendTopicJMSMessage4 = { jmsService, destination, message, jmsTemplateBeanName, postProcessor ->
jmsService.send(topic: destination, message, jmsTemplateBeanName, postProcessor)
}
def addSendMethodsToClass(jmsService, clazz) {
[sendJMSMessage: "sendJMSMessage",
sendQueueJMSMessage: "sendQueueJMSMessage",
sendTopicJMSMessage: "sendTopicJMSMessage",
sendPubSubJMSMessage: "sendTopicJMSMessage"
].each { m, i ->
2.upto(4) { n ->
clazz.metaClass."$m" << this."$i$n".curry(jmsService)
}
}
}
//ReceiveSelected*
def receiveSelectedJMSMessage1 = { jmsService, destination, selector ->
jmsService.receiveSelected(destination, selector)
}
def receiveSelectedJMSMessage2 = { jmsService, destination, selector, long timeout ->
jmsService.receiveSelected(destination, selector, timeout)
}
def receiveSelectedJMSMessage3 = { jmsService, destination, selector, jmsTemplateBeanName ->
jmsService.receiveSelected(destination, selector, jmsTemplateBeanName)
}
def receiveSelectedJMSMessage4 = { jmsService, destination, selector, long timeout, jmsTemplateBeanName ->
jmsService.receiveSelected(destination, selector, timeout, jmsTemplateBeanName)
}
def receiveSelectedAsyncJMSMessage1 = { jmsService, destination, selector ->
jmsService.receiveSelectedAsync(destination, selector)
}
def receiveSelectedAsyncJMSMessage2 = { jmsService, destination, selector, long timeout ->
jmsService.receiveSelectedAsync(destination, selector, timeout)
}
def receiveSelectedAsyncJMSMessage3 = { jmsService, destination, selector, jmsTemplateBeanName ->
jmsService.receiveSelectedAsync(destination, selector, jmsTemplateBeanName)
}
def receiveSelectedAsyncJMSMessage4 = { jmsService, destination, selector, timeout, jmsTemplateBeanName ->
jmsService.receiveSelectedAsync(destination, selector, timeout, jmsTemplateBeanName)
}
def addReceiveSelectedToClass(jmsService, clazz) {
[receiveSelectedJMSMessage: "receiveSelectedJMSMessage",
receiveSelectedAsyncJMSMessage: "receiveSelectedAsyncJMSMessage"
].each { m, i ->
1.upto(4) { n ->
clazz.metaClass."$m" << this."$i$n".curry(jmsService)
}
}
}
def addServiceMethodsToClass(jmsService, clazz) {
addSendMethodsToClass(jmsService, clazz)
addReceiveSelectedToClass(jmsService, clazz)
}
def doWithDynamicMethods = { ctx ->
addServiceMethods(application)
}
def onChange = { event ->
if (!event.source || !event.ctx) {
return
}
def jmsService = event.ctx.getBean('jmsService')
if (application.isControllerClass(event.source)) {
addServiceMethodsToClass(jmsService, event.source)
return
}
if (!application.isServiceClass(event.source)) {
return
}
if (event.source.name.endsWith(".JmsService")) {
return
}
if (jmsConfig.disabled) {
LOG.warn("not inspecting $event.source for listener changes because JMS is disabled in config")
}
else {
boolean isNew = event.application.getServiceClass(event.source?.name) == null
def serviceClass = application.addArtefact(ServiceArtefactHandler.TYPE, event.source).clazz
if (!isNew) {
listenerConfigs.remove(serviceClass.name).each { unregisterListener(it, event.ctx) }
}
def serviceListenerConfigs = getListenerConfigs(serviceClass, application)
if (serviceListenerConfigs) {
listenerConfigs[serviceClass.name] = serviceListenerConfigs
def newBeans = beans {
serviceListenerConfigs.each { listenerConfig ->
registerListenerConfig(listenerConfig, delegate)
}
}
newBeans.beanDefinitions.each { n, d ->
event.ctx.registerBeanDefinition(n, d)
}
}
}
addServiceMethodsToClass(jmsService, event.source)
}
def onConfigChange = { event ->
def newJmsConfig = defaultConfig.merge(event.source.jms)
def newJmsConfigHash = newJmsConfig.hashCode()
if (newJmsConfigHash == jmsConfigHash) {
return
}
def previousJmsConfig = jmsConfig
jmsConfig = newJmsConfig
jmsConfigHash = newJmsConfigHash
LOG.warn("tearing down all JMS listeners/templates due to config change")
// Remove the listeners
listenerConfigs.keySet().toList().each {
listenerConfigs.remove(it).each { unregisterListener(it, event.ctx) }
}
// Remove the templates and abstract definitions from config
new JmsBeanDefinitionsBuilder(previousJmsConfig).removeFrom(event.ctx)
if (jmsConfig.disabled) {
LOG.warn("NOT re-registering listeners/templates because JMS is disabled after config change")
}
else {
LOG.warn("re-registering listeners/templates after config change")
// Find all of the listeners
application.serviceClasses.each { serviceClassClass ->
def serviceClass = serviceClassClass.clazz
def serviceListenerConfigs = getListenerConfigs(serviceClass, application)
if (serviceListenerConfigs) {
listenerConfigs[serviceClass.name] = serviceListenerConfigs
}
}
def newBeans = beans {
def builder = delegate
new JmsBeanDefinitionsBuilder(jmsConfig).build(builder)
listenerConfigs.each { name, serviceListenerConfigs ->
serviceListenerConfigs.each { listenerConfig ->
registerListenerConfig(listenerConfig, builder)
}
}
}
newBeans.beanDefinitions.each { n, d ->
event.ctx.registerBeanDefinition(n, d)
}
}
// We need to trigger a reload of the jmsService so it gets any new beans
def jmsServiceClass = application.classLoader.reloadClass(application.mainContext.jmsService.getClass().name)
application.mainContext.pluginManager.informOfClassChange(jmsServiceClass)
// This also means we need to add new versions of the send methods
addServiceMethods(application)
}
def addServiceMethods(application) {
def jmsService = application.mainContext.jmsService
[application.controllerClasses, application.serviceClasses].each {
it.each {
if (it.clazz.name != "JmsService") {
addServiceMethodsToClass(jmsService, it.clazz)
}
}
}
}
def unregisterListener(listenerConfig, appCtx) {
LOG.info("removing JMS listener beans for ${listenerConfig.serviceBeanName}.${listenerConfig.listenerMethodName}")
listenerConfig.removeBeansFromContext(appCtx)
}
}