Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Event Handling in AEM 6.4

Avatar

Level 3

Hi,

We have various event handlers which are not working in 6.4 since various attributes(resourceChangedAttributes etc.) are removed from Sling Events.  I was wondering if Adobe I/O Event handling can be a potential solution??  Has anyone tried this ?

Thanks,

Rachna

17 Replies

Avatar

Community Advisor

Hi,

you can use JCR events.

AEM 6.3 Event problem



Arun Patidar

Avatar

Level 10

Agree with Arun - JCR Events are lower level API then Sling Events.

Avatar

Level 3

Thanks both!

JCR Events has an issue when it comes to getting admin session, it's not allowing to get session using standard sys users way, and it requires to use deprecated method repository.loginAdministrative(null) & whitelisting the bundle to get the session;

I was thinking to go for higher level Page topics or Adobe I/O events for aem, any idea which is better compare to JCR events as deprecation is strongly discouraged as far as I know!  I wanted to try Adobe I/O and it supports custom osgi events too.

Thanks,

Rachna

Avatar

Community Advisor

Hi,

You can try below to get Session using below to observe JCR events.

session = repository.loginService("subservice-name", null);

you need to create subservice to use system user and create mapping through web console

Screen Shot 2018-07-19 at 2.45.37 PM.png

e.g.

session = repository.loginService("readService", null);

this will create session with training-user

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

Thanks

Arun



Arun Patidar

Avatar

Level 3

Thanks Arun,

I have tried above option and after deploying it to 6.4,  seems like I am getting null pointer exception when I am trying to get the session using repository.loginService("readService", null); and getting null value for the repository.

I have initialised repository using following line.

@Reference
private SlingRepository repository;

Thanks,

Rachna

Avatar

Community Advisor

Hi Rachna,

did you created system user and provide all access and subservience for your bundle?

for me it works fine in 6.4

Service Users in AEM



Arun Patidar

Avatar

Level 3

Yes but it throws NP Exception because it doesn't get access to repository so when it tries to access .loginService on null object, it throws NPE

Thanks,

Rachna

Avatar

Community Advisor

Hi

Can you check your imports?

Can you try getting subservience session from resourceResolverfactory just for test, it won't work for JCR events.

public Session getSubserviceSession() {
Session session = null;
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put(ResourceResolverFactory.SUBSERVICE, "readService");
try {
resourceResolver = resourceFactory.getServiceResourceResolver(paramMap);
// logger.debug("User id inside {}", resourceResolver.getUserID());
session = resourceResolver.adaptTo(Session.class);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.debug("getSubserviceSession : Unable to Login : " + e);
}
return session;
}

you can find the complete code for above example at below?

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



Arun Patidar

Avatar

Level 3

Hi Arun,

thanks for the message.

Please find imports which I am using.

import org.apache.felix.scr.annotations.Reference;

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

import org.osgi.service.component.ComponentContext;

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

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import javax.jcr.Session;

import javax.jcr.observation.Event;

import javax.jcr.observation.EventIterator;

import javax.jcr.observation.EventListener;

import javax.jcr.observation.ObservationManager;

Yes you are right, it doesn't work for JCR events.

Thanks,

Rachna

Avatar

Community Advisor

Hi Rachna,

If you are  able to get session using resourceResolver that means your service is good.

Now I am wondering why it is not working for when you are getting session using repository.

Could you please try with my code, It works fine. I've just checked in AEM6.4

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

Screen Shot 2018-07-20 at 3.36.51 PM.png

Thanks

Arun



Arun Patidar

Avatar

Level 3

Hi Arun,

Thanks for it.

I have written similar code as you, the only difference I can see is, you are using @Reference for SlingRepository from here

import org.osgi.service.component.annotations.Reference;  while I am using

import org.apache.felix.scr.annotations.Reference;

Can you please also share your pom file so I can try and make similar to your code.

Thanks,

Rachna

Avatar

Community Advisor

Hi Rachna,

You should use osgi annotation instead Felix annotation in AEM 6.3 onwards.

Here is the migration guide Migration of SCR annotations to OSGi R6 annotations in AEM 6.3

In my code below is the dependancy, I used.

Parent Pom

<dependency>

  <groupId>org.osgi</groupId>

  <artifactId>osgi.annotation</artifactId>

  <version>6.0.1</version>

  <scope>provided</scope>

  </dependency>

core POM

<dependency>

  <groupId>org.osgi</groupId>

  <artifactId>osgi.annotation</artifactId>

  </dependency>

aem63app-repo/java/POMs at master · arunpatidar02/aem63app-repo · GitHub



Arun Patidar

Avatar

Level 3

Hi Arun,

Many thanks for the help, I replaced maven scr plugin with OSGI annotations plugin and looks like I am able to get the repository now but I am getting exception when I try to register event handler using observationManager object.

It throws exception on following line.

observationManager.addEventListener(this, Event.PROPERTY_ADDED | Event.PROPERTY_CHANGED | Event.PROPERTY_REMOVED, "/content/SITEROOT/**/jcr:content/**", true, null,

   new String[] {"cq:PageContent"} , true);

Any idea?

Once again thank you for sending the link to upgrade deprecated scr plugin to osgi annotations libraries.

Thanks & Regards,

Rachna

Avatar

Community Advisor

Hi Rachan,

You should use absolute path when registering events instead of regEx

public void addEventListener(EventListener listener, 
int eventTypes,
java.lang.String absPath,
boolean isDeep,
java.lang.String[] uuid,
java.lang.String[] nodeTypeName,
boolean noLocal) throws RepositoryException

However you can use regEx while listening events

Thanks

Arun



Arun Patidar

rachanam15474013​ Hi Rachana, I am facing exact same issue. Could you please update the fix here .

Avatar

Level 3

anjalishyamsundar​  Hi yeah you can try JCR Events but I didn't use it because it's not cluster safe and very low level, requires lot of filtering in the code before we reach to interested event.  I am also not using osgi annotations so that's another blocker for me to go for JCR Events.