Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

JCR Event listener not working

test1234567
Level 2
Level 2

Hi There - I am trying to implement the event listener for the specific path and capture the events in the custom logs. For some reason, the events are not triggering. I am using AEM 6.5. Below is the class I am using along with Run mode specific logger for the custom logs and Sling service user mapper service config. Also I have created the system user and granted the complete privileges. I have performed some activity on the specified path but the events are not captured in the logs. Could someone help with this?

 

package com.test.company.aem.core.listeners;

import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
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.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;


//@Component(immediate = true,service= EventListener.class)
@Component(immediate = true)
public class JCREventHandler implements EventListener{

private static final Logger log = LoggerFactory.getLogger(JCREventHandler.class);

private Session session;
private ObservationManager observationManager;

private ResourceResolver resolver;

@Reference
private ResourceResolverFactory resolverFactory;

@Reference
private SlingRepository slingRepository;


@Activate
public void activate(ComponentContext context) throws Exception {
try {
log.info("~~~ Activating the observation ~~~");

/*Map<String, Object> params = new HashMap<>();
params.put(ResourceResolverFactory.SUBSERVICE, "testserviceuser");
resolver = resolverFactory.getServiceResourceResolver(params);
session = resolver.adaptTo(Session.class);
log.info("~~~ Session created ~~~");*/

session = slingRepository.loginService("testserviceuser",null);
observationManager = session.getWorkspace().getObservationManager();
observationManager.addEventListener(
this,
Event.NODE_ADDED | Event.NODE_MOVED | Event.NODE_REMOVED | Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED,
"/content/experience-fragments",
true,
null,
null,
false);
log.info("~~~ Added JCR event listener ~~~");
} catch (RepositoryException e){
log.info(" \n Error while adding Event Listener : {} ",e.getMessage());
}
}

@Deactivate
protected void deactivate(ComponentContext componentContext) {
try {
if (observationManager != null) {
observationManager.removeEventListener(this);
log.info("~~~ Removed JCR event listener ~~~");
}
}
catch (RepositoryException re) {
log.error("Error removing the JCR event listener: ", re);
}
finally {
if (session != null) {
session.logout();
session = null;
}
}
}

public void onEvent(EventIterator eventIterator) {
try {
while (eventIterator.hasNext()){
log.info("\n Path : {} ",eventIterator.nextEvent().getPath());
}
} catch(Exception e){
log.error("\n Error while processing events : {} ",e.getMessage());
}
}

}

 

Logger.PNGMapper.PNG

 

@Arun_Patidar  @BrianKasingli 

1 Accepted Solution
Arun_Patidar
Correct answer by
Community Advisor
Community Advisor

Hi,

There are multiple ways to create an event handler, please check few ways

https://github.com/arunpatidar02/aem63app-repo/blob/master/java/TitlePropertyListener.java

https://github.com/arunpatidar02/aem63app-repo/blob/master/java/SampleJCREvent.java

 

Make sure you have technical user mapping entry in Apache Sling Service User Mapper Service Amendment osgi config

 

 

View solution in original post

2 Replies
asutosh_jena
Community Advisor
Community Advisor

Hi @test1234567 

 

You can try with the resource listener:

 

import org.apache.sling.api.resource.observation.ResourceChange;
import org.apache.sling.api.resource.observation.ResourceChangeListener;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@Component(service = ResourceChangeListener.class,
property = {
ResourceChangeListener.PATHS + "=" + "/content/experience-fragments",
ResourceChangeListener.CHANGES + "=" + "ADDED",
ResourceChangeListener.CHANGES + "=" + "CHANGED",
ResourceChangeListener.CHANGES + "=" + "REMOVED"
}, immediate = true
)
public class SampleListener implements ResourceChangeListener {

private static final Logger log = LoggerFactory.getLogger(SampleEventListener.class);

@Override
public void onChange(List<ResourceChange> list) {
//changes are all related to the nodes of the same page. I just need to get one path and obtain the page path
ResourceChange resourceChange = list.get(0);
String pagePath = resourceChange.getPath();
log.info("Resource Path {}", pagePath);
}
}
Arun_Patidar
Correct answer by
Community Advisor
Community Advisor

Hi,

There are multiple ways to create an event handler, please check few ways

https://github.com/arunpatidar02/aem63app-repo/blob/master/java/TitlePropertyListener.java

https://github.com/arunpatidar02/aem63app-repo/blob/master/java/SampleJCREvent.java

 

Make sure you have technical user mapping entry in Apache Sling Service User Mapper Service Amendment osgi config

 

 

View solution in original post