Class ManagedLeaderLatchCreator
- java.lang.Object
-
- org.kiwiproject.curator.leader.ManagedLeaderLatchCreator
-
public class ManagedLeaderLatchCreator extends Object
Entry point to initializeManagedLeaderLatch
, which wraps an Apache CuratorLeaderLatch
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 DropwizardManaged
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
andLeaderResource
.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ManagedLeaderLatchCreator
addLeaderLatchListener(org.apache.curator.framework.recipes.leader.LeaderLatchListener listener)
Adds the specifiedLeaderLatchListener
.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
.Optional<ManagedLeaderLatchHealthCheck>
getHealthCheck()
Returns the health check (if registered) afterstart()
has been called.ManagedLeaderLatch
getLeaderLatch()
Returns theManagedLeaderLatch
created afterstart()
has been called.List<org.apache.curator.framework.recipes.leader.LeaderLatchListener>
getListeners()
Returns a list containing all registeredLeaderLatchListener
s afterstart()
has been called.boolean
isLeaderLatchStarted()
Has this instance created and started aManagedLeaderLatch
?ManagedLeaderLatchCreator
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 aManagedLeaderLatchHealthCheck
unless explicitly disabled viawithoutHealthCheck()
Creates and registers the JAX-RS REST endpoints unless explicitly disabled viawithoutResources()
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 aManagedLeaderLatch
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.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 aManagedLeaderLatch
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.ManagedLeaderLatchCreator
withoutHealthCheck()
Configures without a health check.ManagedLeaderLatchCreator
withoutResources()
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 theclient
is 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 aManagedLeaderLatch
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 clientenvironment
- the Dropwizard environmentserviceDescriptor
- the service information from the registrylisteners
- optional listeners- Returns:
- a started
ManagedLeaderLatch
- Throws:
IllegalStateException
- if theclient
is 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 aManagedLeaderLatch
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 theManagedLeaderLatch
as 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
ManagedLeaderLatchCreator
with a startedManagedLeaderLatch
- Throws:
IllegalStateException
- if theclient
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 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
ManagedLeaderLatchHealthCheck
unless 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
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
-
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 theManagedLeaderLatch
created 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 registeredLeaderLatchListener
s afterstart()
has been called.- Returns:
- any registered
LeaderLatchListener
s - 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
-
-