I think in general its considered bad practice to have a long running
resource resolver. It might have stale content and you would constantly
need to call refresh() on it whenever you use it. Keep also in mind that
it is not thread safe, so if your doSomething is called concurrently
this might fail.
So far I have not seen a problem with simply creating and closing a
resource resolver each time you need it. But the probably the best
solution is to pass the resolver in.
And :) you should close the resource resolver in deactivate regardless
whether it is live or not.
Regards
Carsten
Roy Teeuwen wrote
> Hello all,
>
> I am wondering on the usage of the resource resolver and on how long it
> should stay open. Lets say I have the following implementation:
>
> @Component
> @Service(SomeService.class)
> public class SomeServiceImpl implements SomeService {
>
> @Reference
> private ResourceResolverFactory resourceResolverFactory;
>
> private ResourceResolver resourceResolver;
>
> @Activate
> protected synchronized void activate(Map<String, Object> properties)
> throws Exception {
> try {
> resourceResolver =
> resourceResolverFactory.getAdministrativeResourceResolver(null);
> } catch (LoginException e) {
> LOG.error("Failed to get admin resourceresolver", e);
> throw e;
> }
> }
>
> @Deactivate
> protected synchronized void deactivate(Map<String, Object> properties)
> throws Exception {
> if (resourceResolver != null && resourceResolver.isLive()) {
> resourceResolver.close();
> }
> }
>
> public void doSomething(String path) {
> Resource resource = resourceResolver.getResource(path);
> //do something
> }
> }
>
>
> Is there anything wrong on using this? Knowing that this means the
> resourceresolver will stay open for possible months/years…
> Or should one try and make the resourceresolver opening as short as possible?
> Knowing that the method will be called around 1000-2000 times an hour.
>
> Another solution is of course passing the resourceResolver to do the
> doSomething, but then the question still remains, how long should you keep a
> resourceresolver open
>
> Greetings,
> Roy
>
--
Carsten Ziegeler
Adobe Research Switzerland
[email protected]