Events, rules, jobs

Introduction

Since using a Java Content repository Jahia makes use of the standard JCR ObservationManager. This manager observes events on the content repository and passes them to several event listeners implementing the abstract DefaultEventListener class.

In the JCRStoreService Jahia configures several default event listeners, which are necessary to make Jahia work. These event listeners are then registered to the JCRObservationManager.

One such event listener is the JBoss Rules Listener, which allows integrators to use rules to process events. These rules can be deployed in modules (template-sets), but Jahia also adds some default rules into the core product.

Furthermore the JCRStoreService holds a configurable interceptor-chain of PropertyInterceptor implementations, which are called on getting, setting or removing of properties in nodes through Jahia's JCR node/property wrappers.

Rules

Each module in Digital Experience Manager can define its own rules, extend the rule domain specific language and add its own set of global objects.

How to add rules in a module?

Just define a rules.drl file in the resources/META-INF folder of the module. An example from the tasks module:

package org.jahia.modules.tasks.rules
//list any import classes here.
import org.jahia.services.content.rules.*
import org.slf4j.Logger
expander rules.dsl
//declare any global variables here
global User user
global Service service
global Logger logger
global Tasks tasks
rule "A welcome task for the new user"
when
A new node is created
- the node has the type jnt:user
then
Log "Creating welcome task for new user: " + node.getName()
Create task "Welcome to Jahia!" with description "We are glad to have you in our platform." for user node.getName()
end
rule "A notification about new group member"
when
A new node is created
- the node has the type jnt:member
The node has a parent
then
Log "Notifying members of the group '" + parent.getParent().getName() + "' about new member '" + node.getName()
Create task "New member in the group" with description "A new member was added to the group." for members of group parent.getParent().getName()
end
rule "A task has been created"
when
A new node is created
- the node has the type jnt:task
The node has a property assignee
then
Set the property state of the node with the value "active"
Assign permissions "rw-" on the node to the user property.getNode().getName()
end

This module adds some new rule consequences that are not part of the default DSL:

Create task "Welcome to Jahia!" with description "We are glad to have you in our platform." for user node.getName()

And it also defines a global object of type Tasks named tasks:

global Tasks tasks

How to extend the domain specific language for my rules ?

It is also very simple. You only have to add a rules.dsl file in the WEB-INF/resources folder of your module. An example from the tasks module:

As for the DSL all modules will share the same set of global objects so be sure that the key, you use, is unique across all modules.

Changes from Jahia 6.5 / 6.6

As a new version of drools is now used, some changes may be required in the rules files you developed for Jahia 6.5 or 6.6.

The rules format did not change, but you may have to look after the following changes :

The KnowledgeHelper class has moved from org.drools.spi to org.drools.core.spi

The # sign for comments is deprecated, comments should now use java syntax : // or /* */

System events

In order to listen events sent via spring, you need to implement to ApplicationListener<ApplicationEvent> interface, replacing ApplicationEvent by the event class you want to listen to. See ApplicationListener

Module deployment events

TemplatePackageRedeployedEvent event is sent when a module is deployed on the server. ModuleDeployedOnSiteEvent is sent when a module is deployed to a site.

Login / logout events

Login and logout event can be activated, if your application need to listen to these events. You'll have to add fireLoginEvent and/or fireLogoutEvent properties in your jahia.properties file. An event will then be sent everytime a user logs in or out

JCR Events

You can extend the org.jahia.services.content.DefaultEventListener class to create a listener on JCR events. The event listener needs to be declared in the module spring file.