(Quick Reference)

event(String topic [, Object data, Map params])

Available in Controllers, Domains, Taglibs and Services

This method returns EventReply if the current user has all of the roles listed.

Usage

class SomeController{

   def logout(){
      def reply = event("logout", session.user)
      //doesn't wait for event execution

      render reply.value  //wait and display value

      event(topic:"afterLogout").waitFor()

      //Only triggered when "afterLogout" finished
      def errorHandler = {errs -> }
      //Use a dedicated error handler
      event(topic:"afterAfterLogout", onError:errs)
   }
}

Arguments

NameMandatoryDefaultDescription
topictrue A String which represents channel subscribed by listeners.
datafalse An Object - preferrably Serializable for IO facilities - which represents the subject of your event such as a domain class.
paramsfalse See below for arguments defaults A Map which represents sending behaviors including namespace.
callbackClosurefalse A Closure triggered after an event completion.

Params arguments :

KeyTypeDefaultDescription
forkBooleanfalseForce the event to reuse the caller thread, therefore executing the method synchronously and propagating any errors.
namespace / forString'app'Target a dedicated topic namespace. To avoid overlapping topic names, the events bus supports a scoping concept called namespace. E.g. 'gorm' is used by gorm events and 'browser' is used for Javascript listeners in events-push plugin.
onReplyClosure{EventReply reply} Same behavior than callbackClosure argument, overrides it if both are defined.
onErrorClosure{List<Exception> errors} If exceptions has been raised by listeners, this callback will be triggered. If undefined, exceptions will be propagated on EventReply.getValue(s).
gormSessionBooleantrueOpens a GORM session for the new thread which carries event execution.
timeoutLong Define a maximum time in millisecond for the event execution.
headersMap<String, Serializable> Additional headers for the event message enveloppe.

event(Map argument)

The map notation allows you to reuse the same arguments than params plus topic for topic, data for data and for (shortcut for 'namespace'). If you specify params, it will use it for the params argument otherwise the first level map is used as params.