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
  • 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

      public LockRegistryLeaderInitiator(LockRegistry locks)
      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

      public LockRegistryLeaderInitiator(LockRegistry locks, Candidate candidate)
      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 registry
      candidate - leadership election candidate
  • Method Details

    • setTaskExecutor

      public void setTaskExecutor(AsyncTaskExecutor taskExecutor)
      Set a AsyncTaskExecutor for running leadership daemon.
      Parameters:
      taskExecutor - the AsyncTaskExecutor to use.
      Since:
      6.2
    • setHeartBeatMillis

      public void setHeartBeatMillis(long heartBeatMillis)
    • setBusyWaitMillis

      public void setBusyWaitMillis(long busyWaitMillis)
    • setLeaderEventPublisher

      public void setLeaderEventPublisher(LeaderEventPublisher leaderEventPublisher)
      Parameters:
      leaderEventPublisher - the event publisher
    • setApplicationEventPublisher

      public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)
      Specified by:
      setApplicationEventPublisher in interface ApplicationEventPublisherAware
    • isRunning

      public boolean isRunning()
      Specified by:
      isRunning in interface Lifecycle
      Returns:
      true if leadership election for this candidate is running.
    • getPhase

      public int getPhase()
      Specified by:
      getPhase in interface Phased
      Specified by:
      getPhase in interface SmartLifecycle
    • setPhase

      public void setPhase(int phase)
      Parameters:
      phase - the phase
      See Also:
    • isAutoStartup

      public boolean isAutoStartup()
      Specified by:
      isAutoStartup in interface SmartLifecycle
    • setAutoStartup

      public void setAutoStartup(boolean autoStartup)
      Parameters:
      autoStartup - true to start automatically
      See Also:
    • getContext

      public Context 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 the candidate for leader election.
      Specified by:
      start in interface Lifecycle
    • destroy

      public void destroy()
      Specified by:
      destroy in interface DisposableBean
    • stop

      public void stop()
      Stop the registration of the candidate for leader election. If the candidate is currently leader, its leadership will be revoked.
      Specified by:
      stop in interface Lifecycle