Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Shared session usage in AEM 6.3 (oh no, not again!)

Avatar

Level 2

For our client we are developing a specific health check which analyses a page output traversing the tree of child pages.

I am aware that a shared session usage is a well known anti-pattern (https://cqdump.wordpress.com/2015/03/02/aem-scaling-patterns-avoid-shared-sessions/ and https://github.com/joerghoh/cqdump/blob/master/examples/bundle/src/main/java/de/joerghoh/cq5/example...) in AEM and keeping this in mind I created this code:

Assuming that I have a list of nodes to check:

for (Node node : nodes) {

  Future f = completionService.submit(testResultFunction(node.getPath()));
  futures.add(f);
}

...

private Callable<String> testResultFunction(final String nodePath) {

   return () -> Templates.serviceResourceResolverFunction(Constants.ServiceUsers.READ_SERVICE,
   resourceResolverFactory, resourceResolver -> {

     String result = requestService.doGet(checkedPageUrl, resourceResolver).getContent();
     ...

     return result;
   });
}

Templates.serviceResourceResolverFunction() looks like this:

public static <R> R serviceResourceResolverFunction(String serviceName, @Nonnull ResourceResolverFactory resourceResolverFactory,
   @Nonnull Function<ResourceResolver, R> function) {

  Map<String, Object> map = Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, serviceName);
  ResourceResolver resolver = null;
 

  try {

   resolver = resourceResolverFactory.getServiceResourceResolver(map);
     return function.apply(resolver);
  } catch (LoginException e) {

    LOG.error("Access denied", e);
    return null;
  } finally {

    if (resolver != null && resolver.isLive()) {

     resolver.close();
    }

  }

}

I am receiving an output which forces me to scratch my head over and over again:

01.03.2018 15:51:00.880 *WARN* [ForkJoinPool-2-worker-1] org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate Attempted to perform hasProperty while thread ForkJoinPool-2-worker-2 was concurrently writing to this session. Blocked until the other thread finished using this session. Please review your code to avoid concurrent use of a session. ...and so on...

In debugger i see that I receive  a new resourceResolver per every request made, and I am not writing anything to the session (?!), so I can only wonder what's the problem here.

Any help will be much appreciated!

Edited 09.03.2018 by Evgeny Tugarev

19 Replies