ResourceResolver getting closed | Community
Skip to main content
Level 2
June 2, 2022

ResourceResolver getting closed

  • June 2, 2022
  • 2 replies
  • 1226 views

Inside ThreadPoolManager calling a service in background with Servlets request.getResourceResolver(). And its getting closed

private void setUpTreadPool() { ModifiableThreadPoolConfig config = new ModifiableThreadPoolConfig(); config.setMinPoolSize(0); // if no work needs to be done, we don't need a thread hanging around config.setKeepAliveTime(10000); config.setPriority(ThreadPoolConfig.ThreadPriority.MIN); myThreadPool = threadPoolManager.create(config, threadPoolName); }

 

myThreadPool.execute(() -> {service(request.getResourceResolver();});

and executing with this call .

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

SantoshSai
Community Advisor
Community Advisor
June 2, 2022

Hi @mohibul_hasan_dhrubo 

May I know What you are expecting here?

If you open it, you are responsible for closing it

If you use a reference to the resourceResolver, then it is not your responsibility to close it

  • Therefore, your code should open & close in the same scope.
  • If you would obtain a resourceResolver from a request or workflow session, you did not open the resolver and you do not need to close it.

Best practice: https://cqdump.joerghoh.de/2018/11/14/try-with-resource-or-i-will-never-forget-to-close-a-resource-resolver/

Reference: http://ogeek.cn/qa/?qa=103260/

https://experienceleague.adobe.com/docs/experience-manager-learn/foundation/development/understand-java-api-best-practices.html?lang=en

 

Hope that helps you!

 

Regards,

Santosh

Santosh Sai
Level 2
June 2, 2022

The resourceResolver reference i am getting form the request.getResourceResolver() is getting closed when i use it from inside a ThreadPool without me closing it explicitly. How can i let it stay open as i cant adapt it to Session which i will need to use to get some results from a query

SantoshSai
Community Advisor
Community Advisor
June 2, 2022

@mohibul_hasan_dhrubo Can you please pass complete code snippet here if possible? I can try to solve it. OR you can add it in a try catch block and use it within try
Refer to this: https://experienceleague.adobe.com/docs/experience-manager-cloud-manager/using/how-to-use/custom-code-quality-rules.html?lang=en#resourceresolver-objects-should-always-be-closed

Santosh Sai
joerghoh
Adobe Employee
Adobe Employee
June 3, 2022

what is the service() method doing with the resourceResolver?

Level 2
June 6, 2022
myThreadPool.execute(() -> assetReferenceService.emailReport(request, response, finalToEmailList, finalCcEmailList, finalEmailBody, finalEmailSubject, request.resourceResolver()));

this service fetches the used and unused asset references by using `ReferenceSearch` api and then using the Day.CQ.Mailer service to send mail to users.  I am calling this service in a separate thread. After passing the resourceResolver and using it to create a `Session` the resourceResolver throws `IllegalStateException` saying resourceResolver is already closed. I have circumvent this issue by using the Service User resourceResolver. but cant use it for some restrictions thats why trying to understand why the resourceResolver is getting closed and if there any alternative solution for that.