Class LockRegistryLeaderInitiator
java.lang.Object
org.springframework.integration.support.leader.LockRegistryLeaderInitiator
- All Implemented Interfaces:
Aware
,DisposableBean
,ApplicationEventPublisherAware
,Lifecycle
,Phased
,SmartLifecycle
public class LockRegistryLeaderInitiator
extends Object
implements SmartLifecycle, DisposableBean, ApplicationEventPublisherAware
Component that initiates leader election based on holding a lock. If the lock has the
right properties (global with expiry), there will never be more than one leader, but
there may occasionally be no leader for short periods. If the lock has stronger
guarantees, and it interrupts the holder's thread when it expires or is stolen, then
you can adjust the parameters to reduce the leaderless period to be limited only by
latency to the lock provider. The election process ties up a thread perpetually while
we hold and try to acquire the lock, so a native leader initiator (not based on a lock)
is likely to be more efficient. If there is no native leader initiator available, but
there is a lock registry (e.g. on a shared database), this implementation is likely to
be useful.
- Since:
- 4.3.1
- Author:
- Dave Syer, Artem Bilan, Vedran Pavic, Glenn Renfro, Kiel Boatman, Gary Russell, Christian Tzolov
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionstatic final long
static final long
Fields inherited from interface org.springframework.context.SmartLifecycle
DEFAULT_PHASE
-
Constructor Summary
ConstructorDescriptionCreate a new leader initiator with the provided lock registry and a default candidate (which just logs the leadership events).LockRegistryLeaderInitiator
(LockRegistry locks, Candidate candidate) Create a new leader initiator. -
Method Summary
Modifier and TypeMethodDescriptionvoid
destroy()
int
getPhase()
boolean
boolean
boolean
void
setApplicationEventPublisher
(ApplicationEventPublisher applicationEventPublisher) void
setAutoStartup
(boolean autoStartup) void
setBusyWaitMillis
(long busyWaitMillis) void
setExecutorService
(ExecutorService executorService) Deprecated, for removal: This API element is subject to removal in a future version.void
setHeartBeatMillis
(long heartBeatMillis) void
setLeaderEventPublisher
(LeaderEventPublisher leaderEventPublisher) Set theLeaderEventPublisher
.void
setPhase
(int phase) void
setPublishFailedEvents
(boolean publishFailedEvents) Enable or disable the publishing of failed events to the specified applicationEventPublisher.void
setTaskExecutor
(AsyncTaskExecutor taskExecutor) Set aAsyncTaskExecutor
for running leadership daemon.void
start()
Start the registration of thecandidate
for leader election.void
stop()
Stop the registration of thecandidate
for leader election.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.context.SmartLifecycle
stop
-
Field Details
-
DEFAULT_HEART_BEAT_TIME
public static final long DEFAULT_HEART_BEAT_TIME- See Also:
-
DEFAULT_BUSY_WAIT_TIME
public static final long DEFAULT_BUSY_WAIT_TIME- See Also:
-
-
Constructor Details
-
LockRegistryLeaderInitiator
Create a new leader initiator with the provided lock registry and a default candidate (which just logs the leadership events).- Parameters:
locks
- lock registry
-
LockRegistryLeaderInitiator
Create a new leader initiator. The candidate implementation is provided by the user to listen for leadership events and carry out business actions.- Parameters:
locks
- lock registrycandidate
- leadership election candidate
-
-
Method Details
-
setExecutorService
@Deprecated(since="6.2", forRemoval=true) public void setExecutorService(ExecutorService executorService) Deprecated, for removal: This API element is subject to removal in a future version.since 6.2 in favor ofsetTaskExecutor(AsyncTaskExecutor)
Set theExecutorService
, where is not provided then a default of single thread Executor will be used.- Parameters:
executorService
- the executor service- Since:
- 5.0.2
-
setTaskExecutor
Set aAsyncTaskExecutor
for running leadership daemon.- Parameters:
taskExecutor
- theAsyncTaskExecutor
to use.- Since:
- 6.2
-
setHeartBeatMillis
public void setHeartBeatMillis(long heartBeatMillis) -
setBusyWaitMillis
public void setBusyWaitMillis(long busyWaitMillis) -
setLeaderEventPublisher
Set theLeaderEventPublisher
.- Parameters:
leaderEventPublisher
- the event publisher
-
setApplicationEventPublisher
- Specified by:
setApplicationEventPublisher
in interfaceApplicationEventPublisherAware
-
isRunning
public boolean isRunning() -
getPhase
public int getPhase()- Specified by:
getPhase
in interfacePhased
- Specified by:
getPhase
in interfaceSmartLifecycle
-
setPhase
public void setPhase(int phase) - Parameters:
phase
- the phase- See Also:
-
isAutoStartup
public boolean isAutoStartup()- Specified by:
isAutoStartup
in interfaceSmartLifecycle
-
setAutoStartup
public void setAutoStartup(boolean autoStartup) - Parameters:
autoStartup
- true to start automatically- See Also:
-
getContext
- Returns:
- the context.
-
isPublishFailedEvents
public boolean isPublishFailedEvents() -
setPublishFailedEvents
public void setPublishFailedEvents(boolean publishFailedEvents) Enable or disable the publishing of failed events to the specified applicationEventPublisher. Because of the large number of failure events that can be published while attempting to get a mutex during leader election (in the case that another instance is holding the mutex), the default is set to false.- Parameters:
publishFailedEvents
- boolean that if true, failed events will be published. If false, no failures will be published. Default is false.- Since:
- 5.0
-
start
public void start()Start the registration of thecandidate
for leader election. -
destroy
public void destroy()- Specified by:
destroy
in interfaceDisposableBean
-
stop
public void stop()Stop the registration of thecandidate
for leader election. If the candidate is currently leader, its leadership will be revoked.
-
setTaskExecutor(AsyncTaskExecutor)