Class ManagedLeaderLatchCreator
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 Summary
Modifier and TypeMethodDescriptionaddLeaderLatchListener(org.apache.curator.framework.recipes.leader.LeaderLatchListener listener) Adds the specifiedLeaderLatchListener.static ManagedLeaderLatchCreatorfrom(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 aManagedLeaderLatchCreator.Returns the health check (if registered) afterstart()has been called.Returns theManagedLeaderLatchcreated afterstart()has been called.List<org.apache.curator.framework.recipes.leader.LeaderLatchListener>Returns a list containing all registeredLeaderLatchListeners afterstart()has been called.booleanHas this instance created and started aManagedLeaderLatch?start()Starts the leader latch, performing the following actions: Creates a newManagedLeaderLatch, starts it, and tells the Dropwizard lifecycle to manage (stop) it Creates and registers aManagedLeaderLatchHealthCheckunless explicitly disabled viawithoutHealthCheck()Creates and registers the JAX-RS REST endpoints unless explicitly disabled viawithoutResources()static ManagedLeaderLatchCreatorstart(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 aManagedLeaderLatchand 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.static ManagedLeaderLatchstartLeaderLatch(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 aManagedLeaderLatchand 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.Configures without a health check.Configures without REST resources to check for leadership and if a leader latch is present.
-
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 aManagedLeaderLatchCreator.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 theManagedLeaderLatch.- Parameters:
client- the Curator clientenvironment- the Dropwizard environmentserviceDescriptor- service metadatalisteners- optional listeners- Returns:
- a new instance
- Throws:
IllegalStateException- if theclientis 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 aManagedLeaderLatchand 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 clientenvironment- the Dropwizard environmentserviceDescriptor- the service information from the registrylisteners- optional listeners- Returns:
- a started
ManagedLeaderLatch - Throws:
IllegalStateException- if theclientis 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 aManagedLeaderLatchand 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
ManagedLeaderLatchCreatorcan be used to then obtain theManagedLeaderLatchas well as the health check and listeners.- Parameters:
client- the Curator clientenvironment- the Dropwizard environmentserviceInfo- the service information from the registrylisteners- optional listeners- Returns:
- a
ManagedLeaderLatchCreatorwith a startedManagedLeaderLatch - Throws:
IllegalStateException- if theclientis not started, or any required arguments are null
-
withoutHealthCheck
Configures without a health check.Use only when constructing a new
ManagedLeaderLatchCreator.- Returns:
- this instance, for method chaining
-
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 specifiedLeaderLatchListener.Use only when constructing a new
ManagedLeaderLatchCreator.- Parameters:
listener- the listener to add- Returns:
- this instance, for method chaining
-
start
Starts the leader latch, performing the following actions:-
Creates a new
ManagedLeaderLatch, starts it, and tells the Dropwizard lifecycle to manage (stop) it -
Creates and registers a
ManagedLeaderLatchHealthCheckunless explicitly disabled viawithoutHealthCheck() -
Creates and registers the JAX-RS REST endpoints unless explicitly disabled via
withoutResources()
Note that once this method is called, nothing about the
ManagedLeaderLatchcan 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
-
Creates a new
-
isLeaderLatchStarted
public boolean isLeaderLatchStarted()Has this instance created and started aManagedLeaderLatch?- Returns:
- true if this instance has created and started a leader latch
-
getLeaderLatch
Returns theManagedLeaderLatchcreated afterstart()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
Returns the health check (if registered) afterstart()has been called.- Returns:
- the registered health check
- Throws:
IllegalStateException- if called but the latch has not been started yet
-
getListeners
Returns a list containing all registeredLeaderLatchListeners afterstart()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
-