ResourceResolver try-with-resources in AEM 6.2 | Adobe Higher Education
Skip to main content
Gdubz-57m2mu
Level 5
January 27, 2017
해결됨

ResourceResolver try-with-resources in AEM 6.2

  • January 27, 2017
  • 6 답변들
  • 6224 조회

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

이 주제는 답변이 닫혔습니다.
최고의 답변: Gdubz-57m2mu

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.

6 답변

smacdonald2008
Level 10
January 28, 2017

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

Gdubz-57m2mu
Gdubz-57m2mu작성자답변
Level 5
January 30, 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.

Feike_Visser1
Adobe Employee
Adobe Employee
January 30, 2017

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

Gdubz-57m2mu
Gdubz-57m2mu작성자
Level 5
January 30, 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.

daniel_henriqu1
Level 4
April 3, 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.

Feike_Visser1
Adobe Employee
Adobe Employee
April 3, 2017

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

And AEM6.2 requires Java8.