Class ManagedLeaderLatchCreator
- java.lang.Object
-
- org.kiwiproject.curator.leader.ManagedLeaderLatchCreator
-
public class ManagedLeaderLatchCreator extends Object
Entry point to initializeManagedLeaderLatch, which wraps an Apache CuratorLeaderLatchand allows for easy determination whether a JVM process is the leader in a group of JVMs connected to a ZooKeeper cluster.The
ManagedLeaderLatchcreated by this class will be started immediately, but is a DropwizardManagedso that it will be stopped when the Dropwizard service shuts down.In addition, by default a
ManagedLeaderLatchHealthCheckis registered with Dropwizard. Two REST resources are registered,GotLeaderLatchResourceandLeaderResource.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ManagedLeaderLatchCreatoraddLeaderLatchListener(org.apache.curator.framework.recipes.leader.LeaderLatchListener listener)Adds the specifiedLeaderLatchListener.static ManagedLeaderLatchCreatorfrom(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 aManagedLeaderLatchCreator.Optional<ManagedLeaderLatchHealthCheck>getHealthCheck()Returns the health check (if registered) afterstart()has been called.ManagedLeaderLatchgetLeaderLatch()Returns theManagedLeaderLatchcreated afterstart()has been called.List<org.apache.curator.framework.recipes.leader.LeaderLatchListener>getListeners()Returns a list containing all registeredLeaderLatchListeners afterstart()has been called.booleanisLeaderLatchStarted()Has this instance created and started aManagedLeaderLatch?ManagedLeaderLatchCreatorstart()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.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.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.ManagedLeaderLatchCreatorwithoutHealthCheck()Configures without a health check.ManagedLeaderLatchCreatorwithoutResources()Configures without REST resources to check for leadership and if a leader latch is present.
-
-
-
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 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.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(CuratorFramework, Environment, ServiceDescriptor, LeaderLatchListener...)
-
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 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
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 specifiedLeaderLatchListener.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:-
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
public ManagedLeaderLatch 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
public Optional<ManagedLeaderLatchHealthCheck> 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
public List<org.apache.curator.framework.recipes.leader.LeaderLatchListener> 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
-
-