ResourceResolver try-with-resources in AEM 6.2

Avatar

Avatar

Gdubz-57m2mu

Avatar

Gdubz-57m2mu

Gdubz-57m2mu

27-01-2017

Based on the versions I've found, it should work but I just wanted to confirm if anyone has used/tested this with any luck, without leaving tons of sessions open all over the place.

According to SLING-4805[1], ResourceResolver should properly extend AutoCloseable[2] as of Sling API 2.11.0. And according to the bundle I'm seeing in my system console of my local AEM 6.2 instance, it's using Sling API 2.11.0. So I should be able to refactor all of our try-catch-finally resource resolver code to try-with-resources[3], yeah?

try (ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(null)) { // do something with the resolver } catch (LoginException e) { // error handle }

Probably just being paranoid and second guessing myself, wondering if anyone else has tried this.

[1] https://issues.apache.org/jira/browse/SLING-4805
[2] https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html
[3] https://docs.oracle.com/javase/8/docs/technotes/guides/language/try-with-resources.html

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Gdubz-57m2mu

Avatar

Gdubz-57m2mu

Gdubz-57m2mu

30-01-2017

smacdonald2008 wrote...

Most ppl that is speak with about this manually close sessions. Have you tried this API? 

 

We have historically done something like the following code, albeit with the getting/closing abstracted away to util class methods:

String serviceName = “some-service-name”; ResourceResolver serviceResolver = null; try { Map<String, Object> serviceParams = new HashMap<>(); serviceParams.put(ResourceResolverFactory.SUBSERVICE, serviceName); serviceResolver = resolverFactory.getServiceResourceResolver(serviceParams); // do whatever is needed with the resolver here } catch (LoginException e) { LOG.error("Error getting service resolver for \"{}\": {}", serviceName, e); } finally { Session session = serviceResolver.adaptTo(Session.class); if (session != null) { session.logout(); session = null; } if (serviceResolver != null) { if (serviceResolver.isLive()) { serviceResolver.close(); } serviceResolver = null; } }

Just thought we could simplify our code a bit by not having to deal with the finally block.

To answer your question, yes I just tried it out on Friday and it's error free, but that doesn't necessarily mean that it's closing properly. I suppose I could try to fetch some resources with it after the try-with-resources block and see if it's still open... 

After having memory leak problems with CQ 5.5 and 5.6.1 because of unclosed sessions, I'm just a bit paranoid, I guess.

Answers (5)

Answers (5)

Avatar

Avatar

Feike_Visser1

Employee

Avatar

Feike_Visser1

Employee

Feike_Visser1
Employee

30-01-2017

I like your way/suggestion.I think the main reason why it is not used much is because this requires Java 8.

Avatar

Avatar

Gdubz-57m2mu

Avatar

Gdubz-57m2mu

Gdubz-57m2mu

30-01-2017

Feike Visser wrote...

I like your way/suggestion.I think the main reason why it is not used much is because this requires Java 8.

 

Fair enough. We just recently finished upgrading all of our PROD servers to 1.8 and we're now in the process of refactoring code to take advantage of some of these new features.

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K
smacdonald2008

28-01-2017

Most ppl that is speak with about this manually close sessions. Have you tried this API? 

Avatar

Avatar

Feike_Visser1

Employee

Avatar

Feike_Visser1

Employee

Feike_Visser1
Employee

03-04-2017

Indeed Java7, however I don't if this is available prior 6.2

And AEM6.2 requires Java8. 

Avatar

Avatar

daniel_henriqu1

Avatar

daniel_henriqu1

daniel_henriqu1

03-04-2017

Hi, Feike.

Isn't try-with-resources a Java 7 thing?

So, I believe we just need to import the right Sling API in order to use it, right?

Regards,

Daniel.