How to add custom jcr event listener through Spring configuration

Custom JCR event listeners can be injected by overriding in Spring assembly XML the bean with id customJcrObservationEventListenerItems.

For example, in your HST webapp project at /src/main/resources/META-INF/hst-assembly/overrides, you can create an XML file myproject-listeners.xml that contains something like:

 

<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> 

 

Your  CustomEventListener must implement  javax.jcr.observation.EventListener. The bean's id is picked up by HST so must not be changed. 

Note that the above Spring configuration only works when adding it to your projects hst-assembly/overrides package. The above cannot be used to add an event listener for an addon module.

In case that you have a module  that needs a JCR event listener, you need to have an HST Addon Module which is a child spring component beans manager which can be packaged, deployed and loaded from a separate classpath resource such as a JAR file. In your Spring configuration you can then use a MethodInvokingFactoryBean to inject your listener as follows:

<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>
Above described method to register  JCR event listener should  be used when developing a  plugin or when developing a (reusable) addon module.
Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?