Skip to main content

Closing Configs

In general, it's good practice to close Config if you're only creating them temporarily so that their listeners get unregistered from the underlying storage APIs.

Example
// Create a transient config.
Config config = Config.create("MyConfig.cfg").build();

// Do stuff with it.

// Then close the config to unregister the listeners.
config.close();

What close() does​

Configclose()

Config.close() unregisters the Config from the underlying config map so that it no longer receives change events from the store. A long-lived config (for example one shared for the lifetime of the application) typically does not need to be closed. Transient configs created repeatedly against a long-lived store should be closed, otherwise the store keeps a listener for each one.

Config does not implement java.io.Closeable or AutoCloseable, so it cannot be used directly in try-with-resources. Use try/finally instead:

Example
Config config = Config.create("MyConfig.cfg").build();
try {
String name = config.getString("General/name");
} finally {
config.close();
}

Closing stores​

Stores are closed separately. ConfigStore implements java.io.Closeable, and closing a store releases its own resources (for example FileStore.close() interrupts the watcher thread if one was started). Closing a Config does not close the store it was built with, since stores are typically shared. ClasspathStore.close() is a no-op (see ClasspathStore).