Apache Curator IllegalStateException: Already closed or has not been started

Suppose that we were trying to call .close() on an Apache Curator Framework object, and we encountered this exception:

java.lang.IllegalStateException: Already closed or has not been started
  at org.apache.curator.shaded.com.google.common.base.Preconditions.checkState(Preconditions.java:507)
  at org.apache.curator.x.discovery.details.ServiceCacheImpl.close(ServiceCacheImpl.java:124)
  at org.apache.curator.x.discovery.details.ServiceProviderImpl.close(ServiceProviderImpl.java:86)

There will also be an answer to the question of how to know if a Curator Framework is running or started.

This is happed because the Curator Framework has already been stopped or has not started at all. All you need to do is add the check:

if (curatorFramework.getState() != CuratorFrameworkState.STOPPED) {

  curatorFramework.close();

}

If you still have any questions, feel free to ask me in the comments under this article, or write me on promark33@gmail.com.

If I saved your day, you can support me :)

Leave a Reply

Your email address will not be published.