This plugin was originally published to provide some dependencies for the multi-tenant plugin. It will probably be revamped and rereleased with a new name.
Check Faq page for changelog
Install
grails install-plugin falcone-util
Eventing
The main feature of this plugin is a light-weight "groovy" pub/sub eventing framework. Any object in the system can be published as an "event", and any number of consumers can register to listen on that event.
This is extremely useful for loose communication between plugins or components, as well as wholesale handling of certain events. For example, I might want to listen for any update to any domain class so I can log audit trail information.
Publishing Events
All controllers, services, and domain classes have a "publishEvent" method for publishing events.
class InvoiceService {
def writeOffInvoice(def invoice) {
invoice.hasBeenWrittenOff = true
notifyAccounting()
publishEvent("invoiceWriteOff", invoice)
}
}You can also manually publish events by using the eventBroker bean:
class SomeSpringBean { EventBroker eventBroker def someSpringMethod() {
//Do something
eventBroker.publish("someSpringMethodCalled", this)
}}Subscribing to Events
There is a builder used for conveniently subscribing to events. Listening to events is very similar to registering spring beans.
Subscribing to Events Through Plugins
You can subscribe to events through plugins by using the doWithEvents closure (just like doWithSpring). Simply add a closure to your
Name (+)GrailsPlugin.groovy file.
def doWithEvents = {ctx -> tenantChanged() { //The name of the event to subscribe to
event-> //The event object
println "Tenant changed from ${event.old} to ${event.new}" //Your event handling code
}
//The "invalidateCredentials" parameter used below is an optional
//parameter to uniquely identify your event consumer for debugging
//purposes. If your consumer throws an exception, for example,
//the event processing system can print out that string to help you
//track down the problem better.
tenantChanged("invalidateCredentials") {event->
SecurityUtils.logOutCurrentUser()
} hibernate.criteriaCreated("onlyReturnActiveRecords") {
criteriaContext -> criteriaContext.criteria.add(Expression.eq("status", "active"))
}
} Subscribing to events in a normal Grails Application
To subscribe to events in a normal grails application, create a file called grails-app/conf/events.groovy. The builder syntax is the exact same as the example above, with the exception that you declare an initial "events" closure.
grails-app/conf/events.groovy:
consumers = {
tenantChanged() {
event->
//Code goes here
}
hibernate.criteriaCreated {
criteria->
}} Subscribe to Events Manually
You can manually subscribe to events using the eventBroker bean:
class SomeBean { EventBroker eventBroker void init() {
eventBroker.subscribe("someEvent") {
event ->
//When the event is thrown, reload this instance.
this.reload();
}
}
} Out-Of-The-Box Events
The following events are published out of the box by the plugin.
Standard Hibernate Events
All hibernate events can be subscribed to globally (for all domain classes) or for one specific domain class. To listen for specific domain class, you will subscribe to
hibernate.[eventName].[domainClass], eg.
hibernate.delete.Invoice would listen to all delete events for the Invoice domain class:
| Event Name | Event Class |
|---|
| hibernate.autoFlush | org.hibernate.event.AutoFlushEvent |
| hibernate.delete | org.hibernate.event.DeleteEvent |
| hibernate.dirtyCheck | org.hibernate.event.DirtyCheckEvent |
| hibernate.evict | org.hibernate.event.EvictEvent |
| hibernate.flush | org.hibernate.event.FlushEvent |
| hibernate.flushEntity | org.hibernate.event.FlushEntityEvent |
| hibernate.load | org.hibernate.event.LoadEvent |
| hibernate.initializeCollection | org.hibernate.event.InitializeCollectionEvent |
| hibernate.lock | org.hibernate.event.LockEvent |
| hibernate.merge | org.hibernate.event.MergeEvent |
| hibernate.persist | org.hibernate.event.PersistEvent |
| hibernate.postDelete | org.hibernate.event.PostDeleteEvent |
| hibernate.postInsert | org.hibernate.event.PostInsertEvent |
| hibernate.postLoad | org.hibernate.event.PostLoadEvent |
| hibernate.postUpdate | org.hibernate.event.PostUpdateEvent |
| hibernate.preDelete | org.hibernate.event.PreDeleteEvent |
| hibernate.preInsert | org.hibernate.event.PreInsertEvent |
| hibernate.preLoad | org.hibernate.event.PreLoadEvent |
| hibernate.preUpdate | org.hibernate.event.PreUpdateEvent |
| hibernate.refresh | org.hibernate.event.RefreshEvent |
| hibernate.replicate | org.hibernate.event.ReplicateEvent |
| hibernate.saveOrUpdate | org.hibernate.event.SaveOrUpdateEvent |
Session, Criteria, and Query Events
The system will fire events when these instances are created:
| Event Name | Event Class |
|---|
| hibernate.sessionCreated | org.hibernate.Session |
| hibernate.criteriaCreated | com.infusion.util.domain.event.hibernate.CriteriaContext |
| hibernate.queryCreated | org.hibernate.Query |