Going Bananas
Going Bananas

Reputation: 2537

Spring Integration Zookeeper - Am I Currently leader

Have a Spring Integration 4.3.8 app deployed on multiple Tomcat instances and the app uses org.springframework.integration:spring-integration-zookeeper:4.3.8.RELEASE to run the multiple instances of the app in a cluster where only one at a time is ever elected as leader.

Question is, what is the simplest way to query from within the app whether I am currently the leader or not? Can I get this info via an available Curator/Zookeeper API? Or do I need to create an event listener of some sort?

Upvotes: 1

Views: 1586

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121550

I hope you use LeaderInitiator.

When it is granted the leader role, an OnGrantedEvent is emitted. That even has Context property. And that one, in turn, has isLeader() state.

you can store this object for future use and check is isLeader() whenever you need.

Since version 5.0 LeaderInitiator provides direct access to the context:

/**
 * The context of the initiator or null if not running.
 * @return the context (or null if not running)
 * @since 5.0
 */
public Context getContext() {
    if (this.leaderSelector == null) {
        return NULL_CONTEXT;
    }
    return this.context;
}

Upvotes: 1

Related Questions