Skip to main content
Level 2
Mar 14, 2023 1:02 PM UTC
Solved

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

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.
anirudhanandAccepted solution
Level 2
Mar 16, 2023 6:48 AM UTC

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 // ... } }

 

 

7 replies

Level 2
Mar 14, 2023 4:43 PM UTC

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

Translate
Shivam153
Level 3
Mar 14, 2023 3:07 PM UTC

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

Translate
Level 2
Mar 14, 2023 4:46 PM UTC

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

Translate
Level 2
Mar 14, 2023 5:03 PM UTC

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.

Translate
anirudhanandAuthorAccepted solution
Level 2
Mar 16, 2023 6:48 AM UTC

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 // ... } }

 

 

Translate
Shivam153
Level 3
Mar 16, 2023 7:07 AM UTC

Great!! Thanks for Sharing. @anirudhanand 

Translate