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
@@ -47,8 +47,6 @@ public class LeaderInitiator implements SmartLifecycle {
|
||||
|
||||
private static final String DEFAULT_NAMESPACE = "/spring-integration/leader/";
|
||||
|
||||
private static final Context NULL_CONTEXT = () -> false;
|
||||
|
||||
private final CuratorContext context = new CuratorContext();
|
||||
|
||||
/**
|
||||
@@ -61,6 +59,20 @@ public class LeaderInitiator implements SmartLifecycle {
|
||||
*/
|
||||
private final Candidate candidate;
|
||||
|
||||
private final Context nullContext = new Context() {
|
||||
|
||||
@Override
|
||||
public boolean isLeader() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRole() {
|
||||
return LeaderInitiator.this.candidate.getRole();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
private final Object lifecycleMonitor = new Object();
|
||||
|
||||
/**
|
||||
@@ -203,13 +215,13 @@ public class LeaderInitiator implements SmartLifecycle {
|
||||
}
|
||||
|
||||
/**
|
||||
* The context of the initiator or null if not running.
|
||||
* @return the context (or null if not running)
|
||||
* The context of the initiator.
|
||||
* @return the context.
|
||||
* @since 5.0
|
||||
*/
|
||||
public Context getContext() {
|
||||
if (this.leaderSelector == null) {
|
||||
return NULL_CONTEXT;
|
||||
return nullContext;
|
||||
}
|
||||
return this.context;
|
||||
}
|
||||
@@ -292,6 +304,11 @@ public class LeaderInitiator implements SmartLifecycle {
|
||||
LeaderInitiator.this.leaderSelector.interruptLeadership();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRole() {
|
||||
return LeaderInitiator.this.candidate.getRole();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CuratorContext{role=" + LeaderInitiator.this.candidate.getRole() +
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.integration.zookeeper.config;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
@@ -91,7 +92,7 @@ public class LeaderInitiatorFactoryBeanTests extends ZookeeperTestSupport {
|
||||
public void testExceptionFromEvent() throws Exception {
|
||||
CountDownLatch onGranted = new CountDownLatch(1);
|
||||
|
||||
LeaderInitiator initiator = new LeaderInitiator(client, new DefaultCandidate());
|
||||
LeaderInitiator initiator = new LeaderInitiator(client, new DefaultCandidate("foo", "bar"));
|
||||
|
||||
initiator.setLeaderEventPublisher(new DefaultLeaderEventPublisher() {
|
||||
|
||||
@@ -107,10 +108,12 @@ public class LeaderInitiatorFactoryBeanTests extends ZookeeperTestSupport {
|
||||
|
||||
});
|
||||
|
||||
assertThat(initiator.getContext().getRole(), equalTo("bar"));
|
||||
initiator.start();
|
||||
|
||||
assertTrue(onGranted.await(10, TimeUnit.SECONDS));
|
||||
assertTrue(initiator.getContext().isLeader());
|
||||
assertThat(initiator.getContext().getRole(), equalTo("bar"));
|
||||
|
||||
initiator.stop();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user