Commit ad3fde9b authored by mohammad.salama's avatar mohammad.salama

Init-Disconnect-Close

parent e9fc63f0
......@@ -2,7 +2,6 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="c96503bf-3b6e-4ec0-9a0f-c9d3e8c6ffc7" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/LeaderElection.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/LeaderElection.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
......
......@@ -15,8 +15,18 @@ public class LeaderElection implements Watcher {
LeaderElection leaderElection = new LeaderElection();
leaderElection.connectToZookeeper();
leaderElection.run();
leaderElection.close();
}
private void close() {
synchronized (zooKeeper) {
try {
zooKeeper.close();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public void connectToZookeeper() throws IOException {
this.zooKeeper = new ZooKeeper(address, SESSION_TIMEOUT, this);
}
......@@ -29,8 +39,10 @@ public class LeaderElection implements Watcher {
}
@Override
public void process(WatchedEvent watchedEvent) {
switch (watchedEvent.getType()) {
public void process(WatchedEvent watchedEvent)
{
switch (watchedEvent.getType())
{
case None:
if (watchedEvent.getState() == Event.KeeperState.SyncConnected)
{
......@@ -42,13 +54,7 @@ public class LeaderElection implements Watcher {
synchronized (zooKeeper)
{
zooKeeper.notifyAll();
try {
zooKeeper.close();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
else if (watchedEvent.getState() == Event.KeeperState.Disconnected)
......@@ -56,15 +62,8 @@ public class LeaderElection implements Watcher {
System.out.println("Disconnected");
synchronized (zooKeeper)
{
zooKeeper.notifyAll();
try {
zooKeeper.close();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
zooKeeper.notifyAll();
}
///System.exit(0);
}
break;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment