| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package leader; |
| 21 |
|
| 22 |
import java.io.Closeable; |
| 23 |
import java.io.IOException; |
| 24 |
import java.util.concurrent.TimeUnit; |
| 25 |
import java.util.concurrent.atomic.AtomicInteger; |
| 26 |
import org.apache.curator.framework.CuratorFramework; |
| 27 |
import org.apache.curator.framework.recipes.leader.LeaderSelector; |
| 28 |
import org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter; |
| 29 |
|
| 30 |
/** |
| 31 |
* An example leader selector client. Note that {@link LeaderSelectorListenerAdapter} which |
| 32 |
* has the recommended handling for connection state issues |
| 33 |
*/ |
| 34 |
public class ExampleClient extends LeaderSelectorListenerAdapter implements Closeable { |
| 35 |
private final String name; |
| 36 |
private final LeaderSelector leaderSelector; |
| 37 |
private final AtomicInteger leaderCount = new AtomicInteger(); |
| 38 |
|
| 39 |
public ExampleClient(CuratorFramework client, String path, String name) { |
| 40 |
this.name = name; |
| 41 |
|
| 42 |
// create a leader selector using the given path for management |
| 43 |
// all participants in a given leader selection must use the same path |
| 44 |
// ExampleClient here is also a LeaderSelectorListener but this isn't required |
| 45 |
leaderSelector = new LeaderSelector(client, path, this); |
| 46 |
|
| 47 |
// for most cases you will want your instance to requeue when it relinquishes leadership |
| 48 |
leaderSelector.autoRequeue(); |
| 49 |
} |
| 50 |
|
| 51 |
public void start() throws IOException { |
| 52 |
// the selection for this instance doesn't start until the leader selector is started |
| 53 |
// leader selection is done in the background so this call to leaderSelector.start() returns immediately |
| 54 |
leaderSelector.start(); |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void close() throws IOException { |
| 59 |
leaderSelector.close(); |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public void takeLeadership(CuratorFramework client) throws Exception { |
| 64 |
// we are now the leader. This method should not return until we want to relinquish leadership |
| 65 |
|
| 66 |
final int waitSeconds = (int) (5 * Math.random()) + 1; |
| 67 |
|
| 68 |
System.out.println(name + " is now the leader. Waiting " + waitSeconds + " seconds..."); |
| 69 |
System.out.println(name + " has been leader " + leaderCount.getAndIncrement() + " time(s) before."); |
| 70 |
try { |
| 71 |
Thread.sleep(TimeUnit.SECONDS.toMillis(waitSeconds)); |
| 72 |
} catch (InterruptedException e) { |
| 73 |
System.err.println(name + " was interrupted."); |
| 74 |
Thread.currentThread().interrupt(); |
| 75 |
} finally { |
| 76 |
System.out.println(name + " relinquishing leadership.\n"); |
| 77 |
} |
| 78 |
} |
| 79 |
} |