Spring Integration has ZooKeeper support as documented in https://docs.spring.io/spring-integration/reference/html/zookeeper.html However this document is so vague.
It suggests adding below bean but does not give details on how to start/stop a poller when the node is granted leadership.
@Bean
public LeaderInitiatorFactoryBean leaderInitiator(CuratorFramework client) {
return new LeaderInitiatorFactoryBean()
.setClient(client)
.setPath("/siTest/")
.setRole("cluster");
}
Do we have any example on how to ensure below poller is run only once in a cluster at any time using zookeeper?
@Component
public class EventsPoller {
public void pullEvents() {
//pull events should be run by only one node in the cluster at any time
}
}
The
LeaderInitiatoremits anOnGrantedEventandOnRevokedEvent, when it becomes leader and its leadership is revoked.See https://docs.spring.io/spring-integration/reference/html/messaging-endpoints-chapter.html#endpoint-roles and the next https://docs.spring.io/spring-integration/reference/html/messaging-endpoints-chapter.html#leadership-event-handling for more info about those events handling and how it affects your components in the particular role.
Although I agree that Zookkeper chapter must have some link to that
SmartLifecycleRoleControllerchapter. Feel free to raise a JIRA on the matter and contribution is welcome!UPDATE
This is what I did in our test:
And I have in logs something like this: