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.
Download the Observation module from our Nexus repository
The Observation module is not installed by default. If you don't find a version node in Configuration >
/modules/observation then the module is not installed.
You can find listeners installed by other rmodules under the
config node even if the Observation module is not installed.
Create a backup of your system before you install a module. Uninstalling a module is not as simple as removing the .jar file. Modules add and change configurations and may change the content. Try new modules in a test environment first. A module consists of a JAR file and may include dependent JAR files. Modules are responsible for updating their own content and configurations across versions. Be sure to keep only one version of each module and its dependencies.
To install a module:
- Stop the application server.
- Copy the module JAR files into the
WEB-INF/libdirectory. The location of this directory depends on the application server.
- Restart the server.
- Go to the AdminCentral URL.
- Start the Web update process.
- Click Start up Magnolia.
Repeat the steps for each author and public instance.
To uninstall a module, remove the module JAR file from the
/WEB-INF/lib folder and restart Magnolia CMS.
However, this is rarely enough. Modules add and modify configuration during installation. The use of a module also changes content. Removing all of these changes is difficult without knowing the installation tasks in detail. To test a module, use the embedded Derby database and take a backup of your
repositories folder. Install the module and try it. When you are done testing, remove the module JAR and restore the
repositories folder from the backup. This way you can go back to square one. We also recommend that you segregate the development and production environments. Take regular backups for disaster recovery so you can revert to a prior state in a routine fashion.
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. Finally, 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 have some extra properties.
Here is a basic
pageAddedAlert listener. It listens to the NODE_ADDED event and reacts by writing a message to the console when new content is added. This is an example of a self-contained listener that reacts to the event on its own. The reaction is hard-coded in the
|Writes message to console when a page is added|
Here's what the output written to the console looks like:
listener: Identifies the configuration as an event listener.
active: Enables and disables the listener. Default is
delay: Optional. Delay in milliseconds before the reaction is triggered.
description: Optional. Listener description.
eventTypes: Optional. The type of JCR event monitored. Possible types are
PERSIST. Separate multiple types with a comma. All types are monitored by default.
includeSubNodes: Optional. Observes also events in subnodes. Default is
false. For example, by default only changes to the page itself would be observed. If you also want to observe changes to components under the page node, set
maxDelay: Optional. Maximum delay milliseconds before the reaction is triggered.
path: Mandatory. Path to the node that is observed.
repository: Mandatory. Workspace in which the observed node resides.
A single listener can monitor a single
Magnolia provides the following listener classes:
|Writes a message to the console when a page is added.|
|Executes a command when an event occurs.|
|Executes a command when an event occurs in the specified node type.|
|Executes a command when an event occurs in the specified node type and matches a regular expression pattern.|
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.
Command event listeners
sendMailOnPageChanges listener. It observes changes to pages and delegates the reaction to the MailCommand which sends a notification.
Properties specific to command listeners:
class: Fully-qualified name of the command class the reaction is delegated to.
params: Optional. Parameters passed to the command. For example the
mailTemplateused to compose the mail body, required by the MailCommand.
<other>: Any other configuration supported by the command. For example
nodeTypein the case of
includes: Optional. Included list of node types that the SpecifiedNodeTypeOnlyEventListener observes.
<node type name 1>: Node type to observe.
<node type name 2>: Node type to observe.
<node type name 3>: Node type to observe.
excludes: Optional. Excluded list of node types that the SpecifiedNodeTypeOnlyEventListener ignores.
<node type name 1>: Node type to ignore.
<node type name 2>: Node type to ignore.
<node type name 3>: Node type to ignore.
Magnolia provides the following example listeners:
sendMailOnPageChanges: Sends an email message when a page is created, updated or deleted. MGNLOBS-27
sendMailOnPageComments: Installed by the Mail module to send an email when a user comments on a page.
activateAddedPages: Automatically activates new pages
versionResourcesOnChange: Automatically versions STK resources.
versionTemplatesOnChange: Installed by the In-place Templating module to automatically version STK templates.
generateCategories: Installed by Categorization module to automatically generate categories.
Activating assets automatically
The example below automatically activates any new asset added in the Assets app. The listener observes the
NODE_ADDED event in the
dam workspace. To test the listener, upload a new asset.
|Automatically activate new assets|