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

JCR Event listener not working

Avatar

Avatar
Validate 1
Level 1
test1234567
Level 1

Likes

0 likes

Total Posts

8 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
View profile

Avatar
Validate 1
Level 1
test1234567
Level 1

Likes

0 likes

Total Posts

8 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
View profile
test1234567
Level 1

07-06-2021

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 

View Entire Topic

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,345 likes

Total Posts

3,220 posts

Correct Reply

914 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,345 likes

Total Posts

3,220 posts

Correct Reply

914 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

08-06-2021

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