Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Getting session has been close method whenever invoking any event object method

ravi_shankerj14
Level 2
Level 2

Hi There,

 

I am getting an error called "this session has been closed " while accessing getNodes() method in event listener using jcr session object. Before executing the below block of code session is coming properly. But whenever i tried to access anything from jcr it is closing the session.

Could you please help what can be the possible reason for this ?

jcrContentNode = getJCRContentNode(event.getProperty(SlingConstants.PROPERTY_PATH).toString());

/*after event.getProperty(...) execution session is getting null values under sd and session context */

private Node getJCRContentNode(String nodePath) throws PathNotFoundException, RepositoryException {
logger.debug("NODE PATH : {}",nodePath);
Node node = jcrSession.getNode(nodePath);
return node;
}

1 Accepted Solution
BrianKasingli
Correct answer by
Community Advisor
Community Advisor

@ravi_shankerj14,

It looks like you are having trouble accessing the Session object. It's best practice to use "service users" while trying to obtain the Session object as you can define limitations of access within the JCR.

A great example of how to implement Sling event handlers in AEM, while utilising "service users" to obtain a Session object can be found in this blog. https://aem.redquark.org/2018/10/day-14-eventing-in-aem.html

Follow this blog article to learn how to create system users; learning the manual process is best when you are new to "system users" - http://www.aemcq5tutorials.com/tutorials/create-system-user-in-aem/

Utilise ACS Commons - Ensure Authorize feature to automate the creation of "system users" via OSGI configurations (https://adobe-consulting-services.github.io/acs-aem-commons/features/ensure-service-users/index.html)

Note: It is not recommended to obtain the session with SlingRepository.loginAdministrative() and ResourceResolverFactory.getAdministrativeResourceResolver() methods provided by Sling because of bad design and performance implementations (documentation: https://helpx.adobe.com/uk/experience-manager/6-4/sites/administering/using/security-service-users.h...). Rather, aim for "service users".

View solution in original post

4 Replies
Theo_Pendle
Community Advisor
Community Advisor
I think it's important for us to see more code if we're going to diagnose the issue. Can you share the whole class please? Or recreate the issue in a very simple class and show us that if you don't want to share too much.
BrianKasingli
Correct answer by
Community Advisor
Community Advisor

@ravi_shankerj14,

It looks like you are having trouble accessing the Session object. It's best practice to use "service users" while trying to obtain the Session object as you can define limitations of access within the JCR.

A great example of how to implement Sling event handlers in AEM, while utilising "service users" to obtain a Session object can be found in this blog. https://aem.redquark.org/2018/10/day-14-eventing-in-aem.html

Follow this blog article to learn how to create system users; learning the manual process is best when you are new to "system users" - http://www.aemcq5tutorials.com/tutorials/create-system-user-in-aem/

Utilise ACS Commons - Ensure Authorize feature to automate the creation of "system users" via OSGI configurations (https://adobe-consulting-services.github.io/acs-aem-commons/features/ensure-service-users/index.html)

Note: It is not recommended to obtain the session with SlingRepository.loginAdministrative() and ResourceResolverFactory.getAdministrativeResourceResolver() methods provided by Sling because of bad design and performance implementations (documentation: https://helpx.adobe.com/uk/experience-manager/6-4/sites/administering/using/security-service-users.h...). Rather, aim for "service users".

View solution in original post

ravi_shankerj14
Level 2
Level 2

@BrianKasingli

 

Thanks for the update. I forgot to mention that we are getting a session using system user only by creating sub-service.

 

public ResourceResolver getServiceResourceResolver() throws RepositoryException {
HashMap<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "testService");
ResourceResolver resourceResolver = null;
try {
resourceResolver = rrf.getServiceResourceResolver(param);
} catch (Exception e) {
logger.error("Error in getting Engage Sys User Resolver : {}",e);
}
return resourceResolver;
}

public Session getServiceUserSession() {
try{
Session sysSession = getServiceResourceResolver().adaptTo(Session.class);
return sysSession;
}catch(Exception exp){
logger.error("Error in getting Engage User session" + exp);
}
return null;
}

 

 

 

And also we are getting the session but it got nullify whenever we try to access jcr nodes or resource.

BrianKasingli
Community Advisor
Community Advisor
@Ravi, can you please share your Apache sling Service User Mapper Service Amendment configuration?