JCR event listener is not working in aem 6.4 | Community
Skip to main content
Level 3
Nov 2, 2018 9:45 AM UTC
Solved

JCR event listener is not working in aem 6.4

Hi all,

Looks like the JCR event listener in 6.4 is broken. Events dont get triggered at all. Is anyone else facing the same issue? Is there any solution?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
arunpatidar
arunpatidarAccepted solution
New Participant
Nov 3, 2018 9:13 AM UTC

Hi Scott,

It does work with system user as well. But you need JCR repository session in order to get jcr events work.

Below code works for me.

aem63app-repo/SampleJCREvent.java at master · arunpatidar02/aem63app-repo · GitHub

@Reference
private SlingRepository repository;
private Session session;
private ObservationManager observationManager;
protected void activate(ComponentContext context) throws Exception {
session = repository.loginService("readService",null);
observationManager = session.getWorkspace().getObservationManager();
observationManager.addEventListener(this, Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED, "/content/AEM64App/fr", true, null,
new String[]{"cq:PageContent","nt:unstructured"} , true);
logger.info("*************added JCR event listener");
}
Arun Patidar

22 replies

arunpatidar
New Participant
May 29, 2019 11:15 AM UTC

Hi,

I've just checked below example in 6.5, working fine for me.

I created a system user arch17sysuser, gave CRUD permissions, created mapping with sub service readService

mapping look like below where com.acc.arch17.arch17.core is bundle symbolic name.

com.acc.arch17.arch17.core:readService=arch17sysuser

aem63app-repo/SampleJCREvent.java at master · arunpatidar02/aem63app-repo · GitHub

Translate
Arun Patidar
Level 4
Jan 11, 2023 6:35 AM UTC

@arunpatidar 

onEvent method never triggered for me. is it still working for you?

bcz I followed the same from here:aem63app-repo/SampleJCREvent.java at master · arunpatidar02/aem63app-repo · GitHub.

I saw the log event that get registered and unregistered.

 

public void onEvent(EventIterator it) {
while (it.hasNext()) {
Event event = it.nextEvent();
try {
logger.info("********INSIDE TRY *****");
Property changedProperty = session.getProperty(event.getPath());
if (changedProperty.getName().equalsIgnoreCase("jcr:title")
&& !changedProperty.getString().endsWith("!")) {
changedProperty.setValue(changedProperty.getString() + "!");
logger.info("*************Property updated: {}", event.getPath());
session.save();
}
}
catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
}

 

Translate
arunpatidar
New Participant
Jan 11, 2023 8:31 AM UTC

HI @karthick1356 
Which AEM version are you using?

Translate
Arun Patidar
ajinkayk6258253
Level 3
May 29, 2019 10:41 AM UTC

not getting any error. bundle is also active, eventlistner not working. it worked with administrativelogin.but not with servicelogin

Translate
Level 2
Mar 25, 2021 4:23 PM UTC

It's 2021 Same problem on Cloud AEM when switched to version 2021.3.5087.20210322T071003Z. On version: AEM v2021.1.4794.20210121T150042Z. it was working fine.

Translate
arunpatidar
New Participant
May 29, 2019 10:37 AM UTC

Hi,

What error are you getting? Is bundle active?

Translate
Arun Patidar
ajinkayk6258253
Level 3
May 29, 2019 7:43 AM UTC

this solution is not  working for 6.5

Translate
smacdonald2008
Level 10
Nov 3, 2018 2:52 PM UTC

Confirmed this output was created:  com.adobe.community.listeners.SimpleResourceListener something has been added : /apps/example/config

Code:

package com.adobe.community.listeners;

import java.util.HashMap;

import java.util.Map;

import javax.jcr.Property;

import javax.jcr.RepositoryException;

import javax.jcr.Session;

import javax.jcr.observation.Event;

import javax.jcr.observation.EventListener;

import javax.jcr.observation.ObservationManager;

import org.apache.sling.api.resource.LoginException;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.api.resource.ResourceResolver;

import org.apache.sling.api.resource.ResourceResolverFactory;

import org.osgi.service.component.annotations.Activate;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Deactivate;

import org.osgi.service.component.annotations.Modified;

import org.osgi.service.component.annotations.Reference;

import org.osgi.service.metatype.annotations.Designate;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.osgi.service.component.ComponentContext;

import javax.jcr.observation.EventIterator ;

@Component(immediate=true,

service= EventListener.class)

public class SimpleResourceListener implements EventListener{

    Logger log = LoggerFactory.getLogger(this.getClass());

     private Session adminSession;

    

   

     @Reference

     org.apache.sling.jcr.api.SlingRepository repository;

    

     @Activate

     public void activate(ComponentContext context) throws Exception {

     log.info("activating ExampleObservation");

     try {

   adminSession = repository.loginService("datawrite",null);

         adminSession.getWorkspace().getObservationManager().addEventListener(

          this, //handler

          Event.PROPERTY_ADDED|Event.NODE_ADDED, //binary combination of event types

          "/apps/example", //path

          true, //is Deep?

          null, //uuids filter

          null, //nodetypes filter

          false);

    

        

     } catch (RepositoryException e){

      log.error("unable to register session",e);

      throw new Exception(e);

     }

    }

    @Deactivate

    public void deactivate(){

     if (adminSession != null){

      adminSession.logout();

     }

    }

    

    public void onEvent(EventIterator eventIterator) {

      try {

        while (eventIterator.hasNext()){

          log.info("something has been added : {}", eventIterator.nextEvent().getPath());

        }

       } catch(RepositoryException e){

       log.error("Error while treating events",e);

      }

     }

    }

6.4 article  -- https://helpx.adobe.com/experience-manager/using/aem64_event_listener.html

Translate
smacdonald2008
Level 10
Nov 3, 2018 2:37 PM UTC

that is great! We will create a 6.4 HELXP article and show this code.

Translate
arunpatidar
arunpatidarAccepted solution
New Participant
Nov 3, 2018 9:13 AM UTC

Hi Scott,

It does work with system user as well. But you need JCR repository session in order to get jcr events work.

Below code works for me.

aem63app-repo/SampleJCREvent.java at master · arunpatidar02/aem63app-repo · GitHub

@Reference
private SlingRepository repository;
private Session session;
private ObservationManager observationManager;
protected void activate(ComponentContext context) throws Exception {
session = repository.loginService("readService",null);
observationManager = session.getWorkspace().getObservationManager();
observationManager.addEventListener(this, Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED, "/content/AEM64App/fr", true, null,
new String[]{"cq:PageContent","nt:unstructured"} , true);
logger.info("*************added JCR event listener");
}
Translate
Arun Patidar
vitis90Author
Level 3
Nov 2, 2018 4:29 PM UTC

hi Scott, even this code didn't work

adminSession = repository.loginAdministrative(null);

observationManager = adminSession.getWorkspace().getObservationManager();

observationManager.addEventListener(this, Event.NODE_ADDED | Event.NODE_MOVED | Event.NODE_REMOVED, "/content/dam/products",true, null, null, false);

Translate
vitis90Author
Level 3
Nov 2, 2018 4:08 PM UTC

I'm getting via subservice and a system user, the usual way.

Translate
vitis90Author
Level 3
Nov 2, 2018 4:08 PM UTC

Hi Scott,

I'm actually getting a session object in this way, using the subservice and system user. It is the recommended way right? Is there any workaround for this? I wouldn't want to get the session by passing null object because it is not the secure way.

Translate