Groovy Notifications

This tutorial explains how to create a Groovy Event Listener that responds to document create and update events. For this purpose the XWiki Observation module provides APIs which listen to internal XWiki events. The complete list of XWiki Bridge Events is available on GitHub.

A possible use case is the need to create a Groovy Listener that sends an email notification to the farm administrator whenever a page is created or updated in the "Code" space of the main wiki. Also, you might want to keep a log of the new/updated documents in a wiki document, for instance "Admin.Logger". The recommended approach is to create a Scheduler job because this way you can schedule when the event listener sends email notifications. Moreover, there is no risk for the code to be executed after a server restart. 

The first step will then be to go to "Scheduler.WebHome" and create a new job, named "Document Updated Event Listener". The Groovy code is:

import org.xwiki.observation.*
import org.xwiki.observation.event.*
import org.xwiki.bridge.event.*
import org.xwiki.observation.event.filter.*
import com.xpn.xwiki.web.*
import com.xpn.xwiki.*

class LoggingEventListener implements EventListener


   String getName()
      // The unique name of this event listener
      return "logging"

   List<Event> getEvents()
      // The list of events this listener listens to
      def regEx = new RegexEventFilter("xwiki:Code\\..*")
      return Arrays.asList(new DocumentCreatedEvent(regEx), new DocumentUpdatedEvent(regEx))

  // Called by the Observation Manager when an event matches the list of events returned
  // by getEvents()
 void onEvent(Event event, Object source, Object data)
    def context = (XWikiContext) data
    def sender = context.getWiki().getUser(source.getCreator(), context).getEmail()
    def to = ""
    def subject = "Document changes on xwiki:Code"
    def body = "Hello, A page has been created or updated in the space 'Code'. You can view this page at: ${source.fullName}"
    def result = context.getWiki().getPluginApi("mailsender", context).sendTextMessage("${sender}", "${to}", subject, body)
   if (source.fullName != "Admin.Logger") {
            def document = context.getWiki().getDocument("Admin.Logger", context)
            document.setContent("${document.getContent()}\n${source.fullName} has been created or updated.")
            context.getWiki().saveDocument(document, "Logging event", true, context)
// Register against the Observation Manager
def observation = Utils.getComponent(ObservationManager.class)
def listener = new LoggingEventListener()

The events the listener listens to are:

and in order to be able to use them, you must make sure you have imported the dedicated package: import org.xwiki.bridge.event.*

After triggering the Scheduler job, create a page in the "xwiki:Code/" space then go to "Admin.Logger" where you should see the new page listed.


Also, provided you have correctly set up a SMTP server, you should receive an email notification regarding the new/updated document.


In case you are interested in more Groovy Event Listener examples, you could check the tutorial.


Related Pages

Trying to decide between Cloud and On Premise? See the comparison on the XWiki Help Center.

Search this space