I generally follow the practice of having the resource resolver passed in if
possible. If the whatever is calling the service is triggered by some sort of
HTTP request it's always best to just have the resource resolver passed in - in
which case the resource resolver is only around for the life the request and
gets disposed of automatically by the system.
If I do have to leverage a resource resolver that you get from the factory I
tend to try and dispose of them in the same method I created it.
Paul McMahon
From: Roy Teeuwen <[email protected]>
To: [email protected]
Sent: Tuesday, June 14, 2016 11:19 AM
Subject: Using the resource resolver
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