Falcone Util

  • Tags: /
  • Latest: 1.0
  • Last Updated: 25 August 2010
  • Grails version: 1.3.4 > *
  • Authors: martytime
1 vote
Dependency:
compile ":falcone-util:1.0"

 Documentation

Summary

Description

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 NameEvent Class
hibernate.autoFlushorg.hibernate.event.AutoFlushEvent
hibernate.deleteorg.hibernate.event.DeleteEvent
hibernate.dirtyCheckorg.hibernate.event.DirtyCheckEvent
hibernate.evictorg.hibernate.event.EvictEvent
hibernate.flushorg.hibernate.event.FlushEvent
hibernate.flushEntityorg.hibernate.event.FlushEntityEvent
hibernate.loadorg.hibernate.event.LoadEvent
hibernate.initializeCollectionorg.hibernate.event.InitializeCollectionEvent
hibernate.lockorg.hibernate.event.LockEvent
hibernate.mergeorg.hibernate.event.MergeEvent
hibernate.persistorg.hibernate.event.PersistEvent
hibernate.postDeleteorg.hibernate.event.PostDeleteEvent
hibernate.postInsertorg.hibernate.event.PostInsertEvent
hibernate.postLoadorg.hibernate.event.PostLoadEvent
hibernate.postUpdateorg.hibernate.event.PostUpdateEvent
hibernate.preDeleteorg.hibernate.event.PreDeleteEvent
hibernate.preInsertorg.hibernate.event.PreInsertEvent
hibernate.preLoadorg.hibernate.event.PreLoadEvent
hibernate.preUpdateorg.hibernate.event.PreUpdateEvent
hibernate.refreshorg.hibernate.event.RefreshEvent
hibernate.replicateorg.hibernate.event.ReplicateEvent
hibernate.saveOrUpdateorg.hibernate.event.SaveOrUpdateEvent

Session, Criteria, and Query Events

The system will fire events when these instances are created:
Event NameEvent Class
hibernate.sessionCreatedorg.hibernate.Session
hibernate.criteriaCreatedcom.infusion.util.domain.event.hibernate.CriteriaContext
hibernate.queryCreatedorg.hibernate.Query