INT-4477: Add getRole() to LeaderInitiator Context
JIRA: https://jira.spring.io/browse/INT-4477
When running multiple roles, it is useful to be able to determine the
role for a particular context, e.g. in an event.
* Docs
**cherry-pick to 5.0.x**
(cherry picked from commit 73dbdb8)
This commit is contained in:
committed by
Artem Bilan
parent
7f898a55ce
commit
b413e0fb84
@@ -704,8 +704,24 @@ An example of this is a file inbound channel adapter that is polling a shared di
|
||||
To participate in a leader election and be notified when elected leader, when leadership is revoked or, failure to acquire the resources to become leader, an application creates a component in the application context called a "leader initiator".
|
||||
Normally a leader initiator is a `SmartLifecycle` so it starts up (optionally) automatically when the context starts, and then publishes notifications when leadership changes.
|
||||
Users can also receive failure notifications by setting the `publishFailedEvents` to `true` (starting with _version 5.0_), in cases when they want take a specific action if a failure occurs.
|
||||
By convention the user provides a `Candidate` that receives the callbacks and also can revoke the leadership through a `Context` object provided by the framework.
|
||||
User code can also listen for `org.springframework.integration.leader.event.AbstractLeaderEvent` s, and respond accordingly, for instance using a `SmartLifecycleRoleController`.
|
||||
By convention, the user provides a `Candidate` that receives the callbacks and also can revoke the leadership through a `Context` object provided by the framework.
|
||||
User code can also listen for `org.springframework.integration.leader.event.AbstractLeaderEvent` s (the super class of `OnGrantedEvent` and `OnRevokedEvent`), and respond accordingly, for instance using a `SmartLifecycleRoleController`.
|
||||
The events contain a reference to the `Context` object:
|
||||
|
||||
[source, java]
|
||||
----
|
||||
public interface Context {
|
||||
|
||||
boolean isLeader();
|
||||
|
||||
void yield();
|
||||
|
||||
String getRole();
|
||||
|
||||
}
|
||||
----
|
||||
|
||||
Starting with _version 5.0.6_, the context provides a reference to the candidate's role.
|
||||
|
||||
There is a basic implementation of a leader initiator based on the `LockRegistry` abstraction.
|
||||
To use it you just need to create an instance as a bean, for example:
|
||||
|
||||
Reference in New Issue
Block a user