Package com.axelor.app.leader
Class LeaderElectionWorker
java.lang.Object
com.axelor.app.leader.LeaderElectionWorker
The
LeaderElectionWorker class is responsible for managing a leader election mechanism
among multiple instances of a distributed system. This class ensures that only one node acts as
the leader at a time using PostgreSQL Session-Level Advisory Lock.
The class uses a scheduled task to periodically attempt to acquire leadership or verify the continued validity of the leader's lock. It operates on a single background thread to manage the election process.
Only available on PostgreSQL databases
-
Method Summary
Modifier and TypeMethodDescriptionstatic LeaderElectionWorkerRetrieves the singleton instance of the LeaderElectionWorker.booleanisLeader()Indicates whether the current instance is the leader.protected booleanChecks if the election service is currently active.voidstart()Starts the background leader election service.voidstop()Stops the leader election service gracefully.
-
Method Details
-
getInstance
Retrieves the singleton instance of the LeaderElectionWorker.- Returns:
- the single instance of the LeaderElectionWorker.
-
stop
public void stop()Stops the leader election service gracefully.This shuts down the scheduler, interrupts any running thread, and closes the database connection (releasing the lock).
-
start
public void start()Starts the background leader election service.This initializes a single-thread scheduler with a Daemon thread. The election process runs immediately (0 delay) and repeats every 300 seconds.
-
isLeader
public boolean isLeader()Indicates whether the current instance is the leader.- Returns:
trueif the current instance holds leadership,falseotherwise.
-
isRunning
protected boolean isRunning()Checks if the election service is currently active.- Returns:
trueif the scheduler is running.
-