ResourceResolverFactory.getServiceResourceResolver(param) returns "null" in Eventlistener Activate method | Community
Skip to main content
March 14, 2023
Solved

ResourceResolverFactory.getServiceResourceResolver(param) returns "null" in Eventlistener Activate method

  • March 14, 2023
  • 4 replies
  • 3697 views

I am facing an issue in AEM 6.5.12 Event Listener. Where I am trying to get the resourceresolver using resourceresolverfactory service user using a Util class. This resolver is used to get the session for the observationmanager. The service user is created via repo init with all the permissions and amended via service user mapping. But during the Eventlistener OSGi component activation the resourceresolverfactory.getserviceresourceresolver(param) returns null and the Eventlistener fails Activation. But after manually stoping and activating the Eventlistener component it is able to get the resourceresolver via the service user and the entire functionality works perfect. Could you please suggest how can I get the service user before the bundle/component activation ?

 

Request your Inputs, Thanks a lot 

@veenavikraman @arunpatidar @briankasingli 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by anirudhanand

Thanks @saravanan_dharmaraj and @shivam153  for your help.

 

I was able to resolve this issue by using ServiceUserMapped Sling API in my EventListener class. 

 

As per doc:
For example setting the reference target to "(subServiceName=mySubService)" ensures that your component only starts when the subService is available. 

 

Used it in the below way:

@Component(immediate = true, service = EventListener.class) public class DemoEventListener implements EventListener { // Declared it as a reference in my class @Reference(target = "(subServiceName=my-service-user)") private ServiceUserMapped serviceUserMapped; @Activate @Modified public void activate() throws RepositoryException { Map<String, Object> param = new HashMap<>(); authInfo.put(ResourceResolverFactory.SUBSERVICE,"my-service-user"); resourceResolverFactory.getServiceResourceResolver(param); //... // having observationmanager related implementation // ... } public void onEvent(final EventIterator events) { //... // having logic to be performed based on event // ... } }

 

 

4 replies

Saravanan_Dharmaraj
Community Advisor
Community Advisor
March 14, 2023
March 14, 2023

Thank you but I tried this out as well. Doesn't work out the resourceresolver is null as login exception is thrown.

Shivam153
March 14, 2023

Hi @anirudhanand,

Can you try to add this code resourceResolverfactory.getserviceresourceresolver(param) inside your util class only and whenever you need resolver inside your listener, you can call that method directly and get the resolver.

 

Thanks

Shivam

March 14, 2023

Thanks Sivam, but my current implementation is based this way only where my Util class gets the service resource resolver, have also tried avoiding the Util class and have the getserviceresolver in the activate method of eventlistener same issue persisted

March 14, 2023

On further debugging observed that the service user gets created only after the osgi component activation. Is it possible to get the service user created before bundle activation ? 

I tried delayed activation of the eventlistner osgi component, doesn't help.

anirudhanandAuthorAccepted solution
March 16, 2023

Thanks @saravanan_dharmaraj and @shivam153  for your help.

 

I was able to resolve this issue by using ServiceUserMapped Sling API in my EventListener class. 

 

As per doc:
For example setting the reference target to "(subServiceName=mySubService)" ensures that your component only starts when the subService is available. 

 

Used it in the below way:

@Component(immediate = true, service = EventListener.class) public class DemoEventListener implements EventListener { // Declared it as a reference in my class @Reference(target = "(subServiceName=my-service-user)") private ServiceUserMapped serviceUserMapped; @Activate @Modified public void activate() throws RepositoryException { Map<String, Object> param = new HashMap<>(); authInfo.put(ResourceResolverFactory.SUBSERVICE,"my-service-user"); resourceResolverFactory.getServiceResourceResolver(param); //... // having observationmanager related implementation // ... } public void onEvent(final EventIterator events) { //... // having logic to be performed based on event // ... } }

 

 

Shivam153
March 16, 2023

Great!! Thanks for Sharing. @anirudhanand