Expand my Community achievements bar.

SOLVED

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

Avatar

Level 2

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 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 2

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

 

 

View solution in original post

7 Replies

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

Avatar

Level 4

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

Avatar

Level 2

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

Avatar

Level 2

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.

Avatar

Correct answer by
Level 2

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