Hi All,
I just want to know about best practice to open and close resource resolvers.
As shown in the below example Example-1, is it fine to declare the resource resolver at class level and initilize it in activate method and use it in the methods of the class and close it in the deactivate method.
If this is not the best practice, is it that resource resolver has to be initilized in the mehtods whenever required and close it after using it in the same method, as shown in the example-2
-------------------------------------------------------------------------------
Example-1
-------------------------------------------------------------------------------
@Component
@Service
@Property(name = "event.topics", value = ReplicationAction.EVENT_TOPIC)
public class Test implements Runnable, EventHandler {
private ResourceResolver resourceResolver;
private Session session;
protected void activate(ComponentContext ctx) {
final Map<String, Object> authInfo = Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, (Object) "testService");
resourceResolver = resolverFactory.getServiceResourceResolver(authInfo);
session = resourceResolver.adaptTo(Session.class);
}
protected void deactivate(ComponentContext componentContext) throws RepositoryException {
if(resourceResolver != null){
resourceResolver.close();
}
if (session != null) {
session.logout();
session = null;
}
}
//Does read operation on repository resourceResolver
public void readOperation(){
}
//Does write operation on repository by adoption resourceResolver to session
public void writeOperation(){
}
}
--------------------------------------------------------------------------------
Example-2
-------------------------------------------------------------------------------
@Component
@Service
@Property(name = "event.topics", value = ReplicationAction.EVENT_TOPIC)
public class Test implements Runnable, EventHandler {
//Does read operation on repository resourceResolver
public void readOperation(){
private ResourceResolver resourceResolver;
private Session session;
final Map<String, Object> authInfo = Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, (Object) "testService");
resourceResolver = resolverFactory.getServiceResourceResolver(authInfo);
session = resourceResolver.adaptTo(Session.class);
if(resourceResolver != null){
resourceResolver.close();
}
if (session != null) {
session.logout();
session = null;
}
}
//Does write operation on repository by adoption resourceResolver to session
public void writeOperation(){
private ResourceResolver resourceResolver;
private Session session;
final Map<String, Object> authInfo = Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, (Object) "testService");
resourceResolver = resolverFactory.getServiceResourceResolver(authInfo);
session = resourceResolver.adaptTo(Session.class);
if(resourceResolver != null){
resourceResolver.close();
}
if (session != null) {
session.logout();
session = null;
}
}
}
-------------------------------------------------------------------
Please give the suggestions on this.
--
Regards,
Pavan Bukka
Solved! Go to Solution.
The long-running session/resource resolver of example 1 is causing problems and should be avoided. Use the approach 2. And to shorten the boilerplate, you use a try-with-resource statement like this:
try (ResourceResolver resourceResolver = resolverFactory.getServiceResourceResolver(authInfo)) {
}
A resource resolver implements AutoClosable to make it work.
The long-running session/resource resolver of example 1 is causing problems and should be avoided. Use the approach 2. And to shorten the boilerplate, you use a try-with-resource statement like this:
try (ResourceResolver resourceResolver = resolverFactory.getServiceResourceResolver(authInfo)) {
}
A resource resolver implements AutoClosable to make it work.
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies