Class ManagedLeaderLatchCreator

java.lang.Object
org.kiwiproject.curator.leader.ManagedLeaderLatchCreator

public class ManagedLeaderLatchCreator extends Object
Entry point to initialize ManagedLeaderLatch, which wraps an Apache Curator LeaderLatch and allows for easy determination whether a JVM process is the leader in a group of JVMs connected to a ZooKeeper cluster.

The ManagedLeaderLatch created by this class will be started immediately, but is a Dropwizard Managed so that it will be stopped when the Dropwizard service shuts down.

In addition, by default a ManagedLeaderLatchHealthCheck is registered with Dropwizard. Two REST resources are registered, GotLeaderLatchResource and LeaderResource.

  • Method Details

    • from

      public static ManagedLeaderLatchCreator from(org.apache.curator.framework.CuratorFramework client, io.dropwizard.core.setup.Environment environment, ServiceDescriptor serviceDescriptor, org.apache.curator.framework.recipes.leader.LeaderLatchListener... listeners)
      Static factory method to create a ManagedLeaderLatchCreator.

      Use this method when you want to perform additional configuration before starting the leader latch.

      You will need to call start() on the returned instance in order to start the ManagedLeaderLatch.

      Parameters:
      client - the Curator client
      environment - the Dropwizard environment
      serviceDescriptor - service metadata
      listeners - optional listeners
      Returns:
      a new instance
      Throws:
      IllegalStateException - if the client is not started, or any required arguments are null
    • startLeaderLatch

      public static ManagedLeaderLatch startLeaderLatch(org.apache.curator.framework.CuratorFramework client, io.dropwizard.core.setup.Environment environment, ServiceDescriptor serviceDescriptor, org.apache.curator.framework.recipes.leader.LeaderLatchListener... listeners)
      If the only thing you want is a ManagedLeaderLatch and you want the standard options (a health check and JAX-RS REST resources) and you do not need references to them, use this method to create and start a latch.

      Otherwise, use start(CuratorFramework, Environment, ServiceDescriptor, LeaderLatchListener...).

      Parameters:
      client - the Curator client
      environment - the Dropwizard environment
      serviceDescriptor - the service information from the registry
      listeners - optional listeners
      Returns:
      a started ManagedLeaderLatch
      Throws:
      IllegalStateException - if the client is not started, or any required arguments are null
      See Also:
    • start

      public static ManagedLeaderLatchCreator start(org.apache.curator.framework.CuratorFramework client, io.dropwizard.core.setup.Environment environment, ServiceDescriptor serviceInfo, org.apache.curator.framework.recipes.leader.LeaderLatchListener... listeners)
      If you want a ManagedLeaderLatch and you want the standard options (a health check and JAX-RS REST resources) and you might need references to them, use this method to create and start a latch.

      The returned ManagedLeaderLatchCreator can be used to then obtain the ManagedLeaderLatch as well as the health check and listeners.

      Parameters:
      client - the Curator client
      environment - the Dropwizard environment
      serviceInfo - the service information from the registry
      listeners - optional listeners
      Returns:
      a ManagedLeaderLatchCreator with a started ManagedLeaderLatch
      Throws:
      IllegalStateException - if the client is not started, or any required arguments are null
    • withoutHealthCheck

      public ManagedLeaderLatchCreator withoutHealthCheck()
      Configures without a health check.

      Use only when constructing a new ManagedLeaderLatchCreator.

      Returns:
      this instance, for method chaining
    • withoutResources

      public ManagedLeaderLatchCreator withoutResources()
      Configures without REST resources to check for leadership and if a leader latch is present.

      Use only when constructing a new ManagedLeaderLatchCreator.

      Returns:
      this instance, for method chaining
    • addLeaderLatchListener

      public ManagedLeaderLatchCreator addLeaderLatchListener(org.apache.curator.framework.recipes.leader.LeaderLatchListener listener)
      Adds the specified LeaderLatchListener.

      Use only when constructing a new ManagedLeaderLatchCreator.

      Parameters:
      listener - the listener to add
      Returns:
      this instance, for method chaining
    • start

      public ManagedLeaderLatchCreator start()
      Starts the leader latch, performing the following actions:

      Note that once this method is called, nothing about the ManagedLeaderLatch can be changed, and calls to other instance methods (e.g. addLeaderLatchListener) will have no effect. Similarly, calling this method more than once is considered unexpected behavior, and we will simply return the existing instance without taking any other actions.

      Returns:
      this instance, from which you can then retrieve the (started) ManagedLeaderLatch
      Throws:
      ManagedLeaderLatchException - if an error occurs starting the latch
    • isLeaderLatchStarted

      public boolean isLeaderLatchStarted()
      Has this instance created and started a ManagedLeaderLatch?
      Returns:
      true if this instance has created and started a leader latch
    • getLeaderLatch

      public ManagedLeaderLatch getLeaderLatch()
      Returns the ManagedLeaderLatch created after start() has been called.

      Use isLeaderLatchStarted() to ensure the latch has been started to ensure this method will succeed.

      Returns:
      the leader latch
      Throws:
      IllegalStateException - if called but the latch has not been started yet
    • getHealthCheck

      public Optional<ManagedLeaderLatchHealthCheck> getHealthCheck()
      Returns the health check (if registered) after start() has been called.
      Returns:
      the registered health check
      Throws:
      IllegalStateException - if called but the latch has not been started yet
    • getListeners

      public List<org.apache.curator.framework.recipes.leader.LeaderLatchListener> getListeners()
      Returns a list containing all registered LeaderLatchListeners after start() has been called.
      Returns:
      any registered LeaderLatchListeners
      Throws:
      IllegalStateException - if called but the latch has not been started yet
      Implementation Note:
      The returned list is an unmodifiable list containing the registered listeners