Friday, July 22, 2011

Grails, routing plugin and GORM

I's been brought to my attention today that it is possible that Camel routes start sooner than some of the other services in Grails (GORM in particular). This leads to issues like missing methods in domain classes. To get around this problem you can do as follows:

In your route definition add the .noAutoStartup() part like this:
from('direct:foo').routeId('myRoute').noAutoStartup()....
In BootStrap.groovy's init closure add the following code:
import org.codehaus.groovy.grails.web.context.*
[...]
def ctx = ServletContextHolder.servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT)
ctx.camelContext.startRoute('myRoute')
This will give you the possibility to start and even stop a route anytime you want.

You can read more on this topic on fusesource.com.

I hope this will help someone :)

7 comments:

magustenebrarum said...

"from('direct:foo').routeId('myRoute').noAutoStart()"
should probably be
"from('direct:foo').routeId('myRoute').noAutoStartup()"

:)

magustenebrarum said...

"from('direct:foo').routeId('myRoute').noAutoStart()"
should probably be
"from('direct:foo').routeId('myRoute').noAutoStartup()"

:)

Matthias Hryniszak said...

Corrected. Thanks!

Alex Anderson said...

is here an argument this should be set by default, or with a config option in the grails-routing-plugin? It could add noAutoStart(), and start all routes at the appropriate time

Matthias Hryniszak said...

@Alex This plugin tries to be as close to Apache Camel as possible so that when someone switches from pure Java to Grails they find that using Camel here and there is just the same. Also at the same time when we don't change to much to how Camel works we can still rely on the original Apache Camel documentation :)

Alex Anderson said...

Hi Matthias, Blogger just ate a long response I put but here it is in short:
* it would be great if this plugin did not have surprising behaviour like that outlined
* if it does have this behaviour, it would be great if it was on the grails.org FAQ page for the plugin
* to access CamelContext in BootStrap.init(), the simpler way is to declare `def camelContext` as a class property

Matthias Hryniszak said...

@Alex You are right. I'll have to work on both the docs and the unexpected behavior. Could you post a ticket so that it will not be forgotten?