Expand my Community achievements bar.

SOLVED

Best practice to open and close resource resolver

Avatar

Level 2

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

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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.

View solution in original post

1 Reply

Avatar

Correct answer by
Employee Advisor

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.