Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable dynamic methods #37

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions RoutingGrailsPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,19 @@ class RoutingGrailsPlugin {
}

def doWithDynamicMethods = { ctx ->
def template = ctx.getBean('producerTemplate')
def config = application.config.grails.routing
if (!config?.disableDynamicMethods) {
def template = ctx.getBean('producerTemplate')

addDynamicMethods(application.controllerClasses, template)
addDynamicMethods(application.serviceClasses, template)
addDynamicMethods(application.controllerClasses, template)
addDynamicMethods(application.serviceClasses, template)

if (isQuartzPluginInstalled(application)) {
addDynamicMethods(application.taskClasses, template)
if (isQuartzPluginInstalled(application)) {
addDynamicMethods(application.taskClasses, template)
}
}

// otherwise we autostart camelContext here
def config = application.config.grails.routing

if (config.grails.routing.autoStartup ?: true) {
def camelContextId = config.camelContextId ?: 'camelContext'
application.mainContext.getBean(camelContextId).start()
Expand All @@ -105,12 +106,15 @@ class RoutingGrailsPlugin {
]

def onChange = { event ->
def artifactName = "${event.source.name}"
def config = application.config.grails.routing
if (!config?.disableDynamicMethods) {
def artifactName = "${event.source.name}"

if (artifactName.endsWith('Controller') || artifactName.endsWith('Service')) {
def artifactType = (artifactName.endsWith('Controller')) ? 'controller' : 'service'
def grailsClass = application."${artifactType}Classes".find { it.fullName == artifactName }
addDynamicMethods([grailsClass], event.ctx.getBean('producerTemplate'))
if (artifactName.endsWith('Controller') || artifactName.endsWith('Service')) {
def artifactType = (artifactName.endsWith('Controller')) ? 'controller' : 'service'
def grailsClass = application."${artifactType}Classes".find { it.fullName == artifactName }
addDynamicMethods([grailsClass], event.ctx.getBean('producerTemplate'))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.grails.plugins.routing

import javax.activation.DataHandler
import org.apache.camel.Exchange
import org.apache.camel.Message
import org.apache.camel.Processor

class CamelMessageService {
static transactional = false

def producerTemplate

def sendMessage(endpoint,message) {
producerTemplate.sendBody(endpoint,message)
}

def sendMessageAndHeaders(endpoint, message, headers) {
producerTemplate.sendBodyAndHeaders(endpoint,message,headers)
}

def sendMessageAndHeadersAndAttachments(endpoint, message, headers, Map<String, DataHandler> attachments) {
producerTemplate.send( endpoint, { Exchange exchange ->
Message msg = exchange.in;
msg.setBody(message)
msg.setHeaders(headers)
attachments.each {
msg.addAttachment(it.key, it.value)
}
} as Processor)
}

def requestMessage(endpoint,message) {
producerTemplate.requestBody(endpoint,message)
}

def requestMessageAndHeaders(endpoint, message, headers) {
producerTemplate.requestBodyAndHeaders(endpoint, message, headers)
}
}