/**
  * Stop the registration of the {@link #candidate} for leader election. If the candidate is
  * currently leader, its leadership will be revoked.
  */
 @Override
 public synchronized void stop() {
   if (running) {
     leaderSelector.close();
     running = false;
   }
 }
Example #2
0
  @Override
  public void close() {
    leaderSelector.close();
    client.close();

    webServer.close();
  }
  /** Start the registration of the {@link #candidate} for leader election. */
  @Override
  public synchronized void start() {
    if (!running) {
      if (client.getState() != CuratorFrameworkState.STARTED) {
        // we want to do curator start here because it needs to
        // be started before leader selector and it gets a little
        // complicated to control ordering via beans so that
        // curator is fully started.
        client.start();
      }
      leaderSelector = new LeaderSelector(client, buildLeaderPath(), new LeaderListener());
      leaderSelector.setId(candidate.getId());
      leaderSelector.autoRequeue();
      leaderSelector.start();

      running = true;
    }
  }
Example #4
0
  @Override
  public void start() {
    if (serverState == ServerState.STARTING || serverState == ServerState.STARTED) {
      LOG.warn("Already at work!");
      return;
    }

    serverState = ServerState.STARTING;

    client.start();
    leaderSelector.start();

    serverState = ServerState.STARTED;
  }
  @Test
  public void masterSelect2() {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFramework client =
        CuratorFrameworkFactory.builder()
            .connectString("anders1:2181,anders2:2181,anders3:2181")
            .namespace("anders_test")
            .retryPolicy(retryPolicy)
            .connectionTimeoutMs(5000)
            .sessionTimeoutMs(5000)
            .build();
    client.start();
    LeaderSelector leaderSelector =
        new LeaderSelector(
            client,
            "/leader_select",
            new LeaderSelectorListener() {

              @Override
              public void stateChanged(CuratorFramework client, ConnectionState newState) {
                System.out.println(newState.values());
              }

              @Override
              public void takeLeadership(CuratorFramework client) throws Exception {
                System.out.println("master2 is selected");
              }
            });
    // leaderSelector.autoRequeue();
    leaderSelector.start();
    try {
      Thread.sleep(3000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
 @Override
 public void close() throws IOException {
   leaderSelector.close();
 }
 public void start() throws IOException {
   leaderSelector.start();
 }
 public ExampleClient(CuratorFramework client, String path, String name) {
   this.name = name;
   leaderSelector = new LeaderSelector(client, path, this);
   leaderSelector.autoRequeue();
 }