Folks,

I'd like to propose implementing AutoClosable
<http://docs.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html>
interface
in order to allow Geode users to treat Cache as an AutoClosable resource.

Most of old GemFire examples and Geode docs
<http://gemfire.docs.pivotal.io/latest/userguide/index.html#basic_config/the_cache/managing_a_client_cache.html>[1]
as well include a very common practice as follows:

Cache cache = new CacheFactory().set("name", "nodeName")
        .set("log-level", "fine")
        .set("statistic-sampling-enabled", "true")...
        .create();

// application code

// Close the cache and disconnect
    System.out.println("Closing the cache and disconnecting.");
    cache.close();

The idea would be to avoid such closing calls when user think it's
appropriate by using try-with-resources syntax:

try ( Cache cache = new CacheFactory().set("name", "nodeName")... ) {

// application code

}

There would be *no need* to call cache.close() or use try/finally blocks.
Please find in the link below more examples
<http://docs.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html>
[3]
of try-with-resources.

*Risks*

- Need to validate how uses of CacheFactory().create() vs
CacheFactory().getAnyInstance() could be affected in existing code.
- How clients with subscription enabled could be affected.  Before closing
we should be able to check if subscription is enabled and just execute the
close(true) as expected.

*Implementation *

>From my initial analysis, it seems like we would be only extending
RegionService
<http://gemfire.docs.pivotal.io/latest/javadocs/japi/com/gemstone/gemfire/cache/RegionService.html>[4]
with AutoClosable and provide additional tests.  But this is very shallow
for now and would need to look further for other implications.


Any thoughts ?

[1]
http://gemfire.docs.pivotal.io/latest/userguide/index.html#basic_config/the_cache/managing_a_client_cache.html
[2] http://docs.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html
[3]
http://docs.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html
[4]
http://gemfire.docs.pivotal.io/latest/javadocs/japi/com/gemstone/gemfire/cache/RegionService.html
-- 

William Markito Oliveira
Enterprise Architect
-- For questions about Apache Geode, please write to
*[email protected]
<[email protected]>*

Reply via email to