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 SummaryNested Classes
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final longstatic final longFields inherited from interface org.springframework.context.SmartLifecycleDEFAULT_PHASE
- 
Constructor SummaryConstructorsConstructorDescriptionCreate 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 SummaryModifier and TypeMethodDescriptionvoiddestroy()intgetPhase()booleanbooleanbooleanvoidsetApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) voidsetAutoStartup(boolean autoStartup) voidsetBusyWaitMillis(long busyWaitMillis) voidsetHeartBeatMillis(long heartBeatMillis) voidsetLeaderEventPublisher(LeaderEventPublisher leaderEventPublisher) Set theLeaderEventPublisher.voidsetPhase(int phase) voidsetPublishFailedEvents(boolean publishFailedEvents) Enable or disable the publishing of failed events to the specified applicationEventPublisher.voidsetTaskExecutor(AsyncTaskExecutor taskExecutor) Set aAsyncTaskExecutorfor running leadership daemon.voidstart()Start the registration of thecandidatefor leader election.voidstop()Stop the registration of thecandidatefor leader election.Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.context.SmartLifecyclestop
- 
Field Details- 
DEFAULT_HEART_BEAT_TIMEpublic static final long DEFAULT_HEART_BEAT_TIME- See Also:
 
- 
DEFAULT_BUSY_WAIT_TIMEpublic static final long DEFAULT_BUSY_WAIT_TIME- See Also:
 
 
- 
- 
Constructor Details- 
LockRegistryLeaderInitiatorCreate a new leader initiator with the provided lock registry and a default candidate (which just logs the leadership events).- Parameters:
- locks- lock registry
 
- 
LockRegistryLeaderInitiatorCreate 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 registry
- candidate- leadership election candidate
 
 
- 
- 
Method Details- 
setTaskExecutorSet aAsyncTaskExecutorfor running leadership daemon.- Parameters:
- taskExecutor- the- AsyncTaskExecutorto use.
- Since:
- 6.2
 
- 
setHeartBeatMillispublic void setHeartBeatMillis(long heartBeatMillis) 
- 
setBusyWaitMillispublic void setBusyWaitMillis(long busyWaitMillis) 
- 
setLeaderEventPublisherSet theLeaderEventPublisher.- Parameters:
- leaderEventPublisher- the event publisher
 
- 
setApplicationEventPublisher- Specified by:
- setApplicationEventPublisherin interface- ApplicationEventPublisherAware
 
- 
isRunning
- 
getPhasepublic int getPhase()- Specified by:
- getPhasein interface- Phased
- Specified by:
- getPhasein interface- SmartLifecycle
 
- 
setPhasepublic void setPhase(int phase) - Parameters:
- phase- the phase
- See Also:
 
- 
isAutoStartuppublic boolean isAutoStartup()- Specified by:
- isAutoStartupin interface- SmartLifecycle
 
- 
setAutoStartuppublic void setAutoStartup(boolean autoStartup) - Parameters:
- autoStartup- true to start automatically
- See Also:
 
- 
getContext- Returns:
- the context.
 
- 
isPublishFailedEventspublic boolean isPublishFailedEvents()
- 
setPublishFailedEventspublic 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
- 
destroypublic void destroy()- Specified by:
- destroyin interface- DisposableBean
 
- 
stop
 
-