In addition to the asynchronous event processing the plugin gives you:
- A publishEvent method attached to all domain classes, controllers and services.
- A Hibernate session bound to the listener thread for the duration of the notification so that listeners can access lazy-loaded properties, etc.
- The ability to have a "retry policy" for certain types of failed notifications on individual listeners. This is particularly useful for listeners that do things like invoking external web-services that may be periodically unavailable.
To install the plugin just use:
grails install-plugin spring-events
The code and some more detailed documentation is on GitHub. I'll be migrating the docs to the plugin's page on grails.org soon.
2 comments:
Thanks for this. I've been struggling with how to resolve the LazyInitializationException errors I get with Hibernate when my ApplicationEvent listener tries to use lazily loaded objects. I adapted your Grails code for use with regular Spring and it's working great!
Is there a way to make a particular event synchronous?
For most of my events, having them asynchronous is desirable, however for a select few synchronous execution is required.
I suspect that this would require me to create my own publishing service, as well as changes to the plugin itself (so that it did not use the taskExecutor when notifying listeners).
Post a Comment