Class 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 Detail

      • from

        public static ManagedLeaderLatchCreator from​(org.apache.curator.framework.CuratorFramework client,
                                                     io.dropwizard.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
      • start

        public static ManagedLeaderLatchCreator start​(org.apache.curator.framework.CuratorFramework client,
                                                      io.dropwizard.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
      • 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
      • 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