Skip to content

Commit

Permalink
Merge pull request #19 from beccagaspard/master
Browse files Browse the repository at this point in the history
Allow grails to still start up when rabbit config is not specified - use...
  • Loading branch information
graemerocher committed Sep 3, 2014
2 parents 1ef24c5 + e104151 commit 112b277
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions RabbitmqGrailsPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,32 @@ class RabbitmqGrailsPlugin {

// URL to the plugin's documentation
def documentation = "http://grails-plugins.github.com/grails-rabbitmq/"

def loadAfter = ['services']
def observe = ['*']

def doWithSpring = {
def doWithSpring = {

def rabbitmqConfig = application.config.rabbitmq
def configHolder = new RabbitConfigurationHolder(rabbitmqConfig)

def connectionFactoryConfig = rabbitmqConfig?.connectionfactory

def connectionFactoryUsername = connectionFactoryConfig?.username
def connectionFactoryPassword = connectionFactoryConfig?.password
def connectionFactoryVirtualHost = connectionFactoryConfig?.virtualHost
def connectionFactoryHostname = connectionFactoryConfig?.hostname
def connectionFactoryPort = connectionFactoryConfig?.port
def connectionChannelCacheSize = connectionFactoryConfig?.channelCacheSize ?: 10

def messageConverterBean = rabbitmqConfig.messageConverterBean
def messageConverterBean = rabbitmqConfig?.messageConverterBean

if(!connectionFactoryUsername || !connectionFactoryPassword || !connectionFactoryHostname) {
log.error 'RabbitMQ connection factory settings (rabbitmq.connectionfactory.username, rabbitmq.connectionfactory.password and rabbitmq.connectionfactory.hostname) must be defined in Config.groovy'
} else {

log.debug "Connecting to rabbitmq ${connectionFactoryUsername}@${connectionFactoryHostname} with ${configHolder.getDefaultConcurrentConsumers()} consumers."

def connectionFactoryClassName = connectionFactoryConfig?.className ?:
'org.springframework.amqp.rabbit.connection.CachingConnectionFactory'
def parentClassLoader = getClass().classLoader
Expand All @@ -87,7 +87,7 @@ class RabbitmqGrailsPlugin {
username = connectionFactoryUsername
password = connectionFactoryPassword
channelCacheSize = connectionChannelCacheSize

if (connectionFactoryPort) {
port = connectionFactoryPort
}
Expand Down Expand Up @@ -124,15 +124,15 @@ class RabbitmqGrailsPlugin {

serviceConfigurer.configure(delegate)
}

def queuesConfig = application.config.rabbitmq?.queues
if(queuesConfig) {
def queueBuilder = new RabbitQueueBuilder()
queuesConfig = queuesConfig.clone()
queuesConfig.delegate = queueBuilder
queuesConfig.resolveStrategy = Closure.DELEGATE_FIRST
queuesConfig()

// Deal with declared exchanges first.
queueBuilder.exchanges?.each { exchange ->
if (log.debugEnabled) {
Expand All @@ -144,7 +144,7 @@ class RabbitmqGrailsPlugin {
Boolean.valueOf(exchange.autoDelete),
exchange.arguments)
}

// Next, the queues.
queueBuilder.queues?.each { queue ->
if (log.debugEnabled) {
Expand All @@ -158,13 +158,13 @@ class RabbitmqGrailsPlugin {
queue.arguments,
)
}

// Finally, the bindings between exchanges and queues.
queueBuilder.bindings?.each { binding ->
if (log.debugEnabled) {
log.debug "Registering binding between exchange '${binding.exchange}' & queue '${binding.queue}'"
}

def args = [ ref("grails.rabbit.exchange.${binding.exchange}"), ref ("grails.rabbit.queue.${binding.queue}") ]
if (binding.rule) {
log.debug "Binding with rule '${binding.rule}'"
Expand All @@ -177,7 +177,7 @@ class RabbitmqGrailsPlugin {
"grails.rabbit.binding.${binding.exchange}.${binding.queue}"(Binding, binding.queue, QUEUE, binding.exchange, binding.rule, binding.arguments )
}
}

rabbitRetryHandler(StatefulRetryOperationsInterceptorFactoryBean) {
def retryPolicy = new SimpleRetryPolicy()
def maxRetryAttempts = 1
Expand All @@ -190,23 +190,23 @@ class RabbitmqGrailsPlugin {
}
}
retryPolicy.maxAttempts = maxRetryAttempts

def backOffPolicy = new FixedBackOffPolicy()
backOffPolicy.backOffPeriod = 5000

def retryTemplate = new RetryTemplate()
retryTemplate.retryPolicy = retryPolicy
retryTemplate.backOffPolicy = backOffPolicy

retryOperations = retryTemplate
}
}
}
}

def doWithDynamicMethods = { appCtx ->
addDynamicMessageSendingMethods application.allClasses, appCtx
}

private addDynamicMessageSendingMethods(classes, ctx) {
if(ctx.rabbitMQConnectionFactory) {
classes.each { clz ->
Expand All @@ -217,7 +217,7 @@ class RabbitmqGrailsPlugin {

def doWithApplicationContext = { applicationContext ->
def containerBeans = applicationContext.getBeansOfType(SimpleMessageListenerContainer)
if(applicationContext.rabbitTemplate.messageConverter instanceof org.springframework.amqp.support.converter.AbstractMessageConverter) {
if(applicationContext.rabbitTemplate?.messageConverter instanceof org.springframework.amqp.support.converter.AbstractMessageConverter) {
applicationContext.rabbitTemplate.messageConverter.createMessageIds = true
}
containerBeans.each { beanName, bean ->
Expand All @@ -229,11 +229,11 @@ class RabbitmqGrailsPlugin {
}
}
}

def onChange = { evt ->
if(evt.source instanceof Class) {
addDynamicMessageSendingMethods ([evt.source], evt.ctx)

// If a service has changed, reload the associated beans
if(isServiceEventSource(application, evt.source)) {
def serviceGrailsClass = application.addArtefact(ServiceArtefactHandler.TYPE, evt.source)
Expand All @@ -247,7 +247,7 @@ class RabbitmqGrailsPlugin {
beans.registerBeans(evt.ctx)
startServiceListener(serviceGrailsClass.propertyName, evt.ctx)
}
}
}

// Other listener containers may have been stopped if they were
// affected by the re-registering of the changed class. For example,
Expand Down

0 comments on commit 112b277

Please sign in to comment.