Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Java/AEM: How to trigger an EventListener (javax.jcr.observation.EventListener)?

Avatar

Level 9

I'm trying to debug a existing code where it's supposed to trigger when "Event.PROPERTY_CHANGED+Event.PROPERTY_ADDED" events happened but I don't think it's triggering.

 

Any ideas on how to help debug the code? (I want my breakpoint inside "onEvent" function to stop the execution)

 

Some info/observations:

 

  • My PDF is located in "/content/dam/files/careers/factsheets/Recruitment-Q-and-A-Factsheet.pdf"
  • I inspected the PDF in CRX/DE and this path "/content/dam/files/careers/factsheets/Recruitment-Q-and-A-Factsheet.pdf/jcr:content" has this property "jcr:primaryType=dam:AssetContent".
  • If I edit the metadata (example title), the parent path for the attributes/properties is located in "/content/dam/files/careers/factsheets/Recruitment-Q-and-A-Factsheet.pdf/jcr:content/metadata"
  • I put a breakpoint in this line "session = commonService.getSession();" and execution stops here when my AEM instance is starting (either starting it up for the first time or restarting the instance). I didn't encounter any errors/exceptions inside "activate" function.
  • To test my breakpoint inside the "onEvent" function, I changed the metadata of the PDF. I also tried publishing it. I also manually created 2 new nodes of types "nt:unstructed" and "nt:folder" inside "/content/dam/files/careers" folder (via CRX/DE). None of these actions triggered my breakpoint.
  • This is my code

 

 

 

 

        protected void activate() {
            try {
                session = commonService.getSession(); //this is a custom service created by 3rd party
                
                observationManager = session.getWorkspace().getObservationManager();
                
                final String[] types = { "dam:Asset", "dam:AssetContent", "nt:unstructured", "nt:folder" };
                
                final String path = "/content/dam/files";
                
                final int events1 = Event.PROPERTY_CHANGED | Event.PROPERTY_ADDED | Event.NODE_ADDED;
                
                observationManager.addEventListener(this, events1, path, true, null, types, false);
            } catch(Exception e) {
                log.error("bad stuff happened",e);
            }
        }
        
        
        public void onEvent(EventIterator eventIterator) {
            while (eventIterator.hasNext()){
                Event event = eventIterator.nextEvent();
                
                //insert more code here
            }
        }

 

 

 



1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @jayv25585659 ,

If I understand correctly the problem might be in your 

session = commonService.getSession();

The third-party session might not have enough user permission in your content path.

 

For better and fully proven code you can use a service-user and give that user permission[Read access] on the particular content node. The sample code will be like this,

@Reference 
private SlingRepository repository;

@Activate
protected void activate() {
    Session session = repository.loginService("your-service-user-name", null);
    // Write your logic
}


@Deactivate
protected void deactivate() throws RepositoryException {
    Session session = repository.loginService("your-service-user-name", null);
    try {
        if (observationManager != null)
            observationManager.removeEventListener(this);
    } finally {
        if (session != null) session.logout();
    }
}

 

View solution in original post

4 Replies

Avatar

Community Advisor

Hello @jayv25585659  - 

 

You can additionally try : 

 

  • Adding loggers inside the onEvent method to check if it is being called.
  • Additionally ensuring that the commonService.getSession() method returns a valid session. Check if the custom service (commonService) is properly implemented and injected into the component.

could you please also look for possible logging errors in error log?

Avatar

Level 9
  • Unsure if additional logging would help since I already have a breakpoint in the first line of the function. If the execution is not stopping in the breakpoint, I don't think extra logging will help. BUT I'll try it anyway.
  • I clearly mentioned that I debugged "activate" function and no errors/exceptions there. (if session is null, i would get an NPE)

 

Avatar

Community Advisor

Hello @jayv25585659 

 

Service is Active?

Can you please check, if the Service is Active and (immediate=true) is set on the Listener?

https://medium.com/@toimrank/aem-handler-and-listener-12b6c8b5a3d3

 

Also, the signature of the activate() is different. Don't know if it matters, as Event Listener is old way of doing things, haven't worked on it for a while.

 

User has necessary access?

Please check the access levels of the user. If in doubt, add the user to administrators group and try triggering the events

 


Aanchal Sikka

Avatar

Correct answer by
Community Advisor

Hello @jayv25585659 ,

If I understand correctly the problem might be in your 

session = commonService.getSession();

The third-party session might not have enough user permission in your content path.

 

For better and fully proven code you can use a service-user and give that user permission[Read access] on the particular content node. The sample code will be like this,

@Reference 
private SlingRepository repository;

@Activate
protected void activate() {
    Session session = repository.loginService("your-service-user-name", null);
    // Write your logic
}


@Deactivate
protected void deactivate() throws RepositoryException {
    Session session = repository.loginService("your-service-user-name", null);
    try {
        if (observationManager != null)
            observationManager.removeEventListener(this);
    } finally {
        if (session != null) session.logout();
    }
}