The Observation module allows you to listen to events in the repository and trigger a reaction. Use observation to automate tasks such as publishing newly added assets or sending an email notification when a comment is added. Custom event listeners can react to almost any event. The Observation module is based on the JCR observation feature.  

Installing

Maven is the easiest way to install the module. Add the following to your bundle:

Error rendering macro 'artifact-maven-dependencies-snippet-macro'

com.sun.jersey.api.client.ClientHandlerException: java.net.NoRouteToHostException: No route to host (Host unreachable)

What is an event listener?

An event listener registers an interest in a particular type of event. The job of the listener is to detect the event and possibly react to it.

Example: You want to send an email notification when a new page is added. Configure an event listener that listens to the NODE_ADDED event. This event occurs when a new node is added. Limit the listener to the mgnl:page node type so you only get notified about pages, not other types of nodes. Define the mail command as the desired reaction.

Magnolia provides two types of event listeners: listeners that react to the event on their own and listeners that delegate the reaction to a command. Both listeners are configured the same way, but command event listeners require additional properties that the command class supports.

Listener configuration

The module includes a sample, self-contained listener, PageListener, that reacts to an event on its own. The reaction is hard-coded in the class and outputs the following message to the console when a page is added.

Output in console
EventListener Test: Text Message
EventListener Test: Text Message2

Example: Basic PageListener configuration in /modules/observation/config/listenerConfigurations.

Properties:

listenerConfigurations

required

Listener configuration node.

<listener name>

required

Arbitrary node name.

listener

required

Listener node.

class

required

Fully qualified listener class name.

nodeType

optional, default is all node types

Restricts observation to events on the specified node type, for example mgnl:page. Use together with a listener class that observes the specified node type.

path  

required

Path to the node that is observed.

repository

required

Workspace of the observed node.

active

optional, default is false .

Enables and disables the listener.

delay

optional

Minimum delay (in milliseconds) before the reaction is triggered.

The delay and maxDelay properties form a range, and both values must be supplied to activate the feature.

description

optional

Listener description.

eventTypes

optional, default is all event types

Type of JCR event monitored. Options:  NODE_ADDED, NODE_MOVEDNODE_REMOVED, PROPERTY_ADDED, PROPERTY_CHANGED, PROPERTY_REMOVED and PERSIST. Separate multiple types with a comma.

includeSubNodes

optional, default is false

Observe also events in subnodes. For example, only changes to the page would be observed by default. Set to true, if you also want to observe changes to components under the page node.

maxDelay

optional

Maximum delay (in milliseconds) before the reaction is triggered.

The delay and maxDelay properties form a range, and both values must be supplied to activate the feature.

A single listener can monitor a single nodeType or all types. To monitor two types to the exclusion of other types, two listener configurations are required. Folders and contacts, for example, require one listener to monitor folders (nodeType=mgnl:folder) and another to monitor contacts (nodeType=mgnl:contact). You can verify the nodeType by exporting the node to be monitored to XML and searching for the value of jcr:primaryType property in the file.

Command listener configuration

A command event listener delegates the reaction to a command. You can use an existing command or write your own.

The module installs two example command listener configurations in /modules/observation/config/listenercConfigurations/:

  • activateAddedPages: Uses ActivationCommand to automatically activate new pages.
  • sendMailOnPageChange: Uses MailCommand to send an email message when a page is created, updated or deleted. (warning) This example assumes the pageChangeNotification mail template has been configured in the Mail module.

Command-specific listener properties:

listenerConfigurations

required

Listener configuration node.

<listener name>

required

Arbitrary node name.

listener

required

Listener node.

command

required

Command node.

class

required

Fully qualified command class name the reaction is delegated to.

params

optional

Parameters passed to the command. For example the MailTemplate used to compose the mail body is required by MailCommand.

class

required

Fully qualified name of the command listener class.

nodeType

optional, default is all node types

Restricts observation to events on the specified node type.

<listener properties>

optional/requried

Same as standard listener properties.

Listeners may rely on the Mail module to send emails. Configure SMTP setting to ensure they work.

Listener classes

The module provides the following listener classes:

info.magnolia.module.observation.sample.PageListener

PageListener

Writes a message to the console when a page is added.
info.magnolia.module.observation.commands.CommandEventListener

CommandEventListener

Executes a command when an event occurs.
info.magnolia.module.observation.commands.RestrictToNodeTypeEventListener

RestrictToNodeTypeEventListener

Executes a command when an event occurs in the specified node type.
info.magnolia.module.observation.commands.RestrictToTemplateEventListener

RestrictToTemplateEventListener

Executes a command only when an event occurs in the specified node that has a specified template type as a resource node.

info.magnolia.module.observation.commands.RegexEventListener

RegexEventListener

Executes a command when an event occurs in the specified node type and matches a regular expression pattern.
info.magnolia.module.observation.commands.SpecifiedNodeTypeOnlyEventListener

SpecifiedNodeTypeOnlyEventListener

Executes a command when an event occurs in a node that is in the "included" list of node types and not on the "excluded" list.

Example: Activating assets automatically

This example command listener configuration observes the NODE_ADDED event in the dam workspace. To test the listener, upload a new asset.

(warning) The dam workspace is defined twice. The property is always required under the listener configuration. In this case, it is also required as a parameter by the ActivationCommand

Example: Automatically activate new assets added in the Magnolia Assets subapp


#trackbackRdf ($trackbackUtils.getContentIdentifier($page) $page.title $trackbackUtils.getPingUrl($page))