Add a Custom JCR Event Listener Using Spring Configuration
You can register custom JCR event listeners in Bloomreach Content by overriding the Spring bean with the ID customJcrObservationEventListenerItems.
When to Use
Use this approach to add JCR event listeners to your main HST web application. For addon modules or plugins, use the alternative method described below.
Prerequisites
- A Bloomreach Content project with HST and Spring configuration.
- A custom event listener class that implements
javax.jcr.observation.EventListener.
Implementation: Registering a Listener in the HST Web Application
-
Create a Spring configuration file in your project at
/src/main/resources/META-INF/hst-assembly/overrides/.
For example:myproject-listeners.xml. -
Define the
customJcrObservationEventListenerItemsbean in your XML file as shown below:
<bean id="customJcrObservationEventListenerItems" class="org.springframework.beans.factory.config.ListFactoryBean"> <property name="sourceList"> <list> <bean class="org.hippoecm.hst.core.jcr.EventListenerItemImpl"> <property name="nodeAddedEnabled" value="true"/> <property name="nodeRemovedEnabled" value="true"/> <property name="propertyAddedEnabled" value="true"/> <property name="propertyChangedEnabled" value="true"/> <property name="propertyRemovedEnabled" value="true"/> <property name="absolutePath" value="/some/content/path"/> <property name="deep" value="true"/> <property name="eventListener"> <bean class="org.myproject.CustomEventListener"> </bean> </property> </bean> </list> </property> </bean>
- Replace
org.myproject.CustomEventListenerwith your implementation. - Your custom class must implement
javax.jcr.observation.EventListener. - Do not change the bean ID. The HST framework detects listeners using this specific ID.
Note: This method works only for the main project. It does not apply to addon modules.
Registering a Listener in an Addon Module or Plugin
To add a JCR event listener from an addon module or plugin, use an HST Addon Module. This approach allows you to package, deploy, and load the listener from a separate classpath resource such as a JAR file.
In your Spring configuration, use a MethodInvokingFactoryBean to inject the listener:
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="jcrObservationEventListenerItems" /> <property name="targetMethod" value="add" /> <property name="arguments"> <!-- Targeting configuration invalidation listener --> <bean class="org.hippoecm.hst.core.jcr.EventListenerItemImpl"> <property name="nodeAddedEnabled" value="true" /> <property name="nodeRemovedEnabled" value="true" /> <property name="propertyAddedEnabled" value="true" /> <property name="propertyChangedEnabled" value="true" /> <property name="propertyRemovedEnabled" value="true" /> <property name="absolutePath" value="/some/content/path" /> <property name="deep" value="true" /> <property name="uuids"> <null /> </property> <property name="noLocal" value="false" /> <property name="eventListener"> <bean class="org.myproject.CustomEventListener"> </bean> </property> </bean> </property> </bean>
Info:
Use this method when developing a plugin or a reusable addon module.
Verification
- After deployment, verify that your custom event listener is triggered by JCR events at the specified path.
- Check application logs for expected event handling.