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

Init-Disconnect-Close

parent e9fc63f0
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
<project version="4"> <project version="4">
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="c96503bf-3b6e-4ec0-9a0f-c9d3e8c6ffc7" name="Changes" comment=""> <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" /> <change beforePath="$PROJECT_DIR$/src/LeaderElection.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/LeaderElection.java" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
......
...@@ -15,8 +15,18 @@ public class LeaderElection implements Watcher { ...@@ -15,8 +15,18 @@ public class LeaderElection implements Watcher {
LeaderElection leaderElection = new LeaderElection(); LeaderElection leaderElection = new LeaderElection();
leaderElection.connectToZookeeper(); leaderElection.connectToZookeeper();
leaderElection.run(); leaderElection.run();
leaderElection.close();
} }
private void close() {
synchronized (zooKeeper) {
try {
zooKeeper.close();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public void connectToZookeeper() throws IOException { public void connectToZookeeper() throws IOException {
this.zooKeeper = new ZooKeeper(address, SESSION_TIMEOUT, this); this.zooKeeper = new ZooKeeper(address, SESSION_TIMEOUT, this);
} }
...@@ -29,8 +39,10 @@ public class LeaderElection implements Watcher { ...@@ -29,8 +39,10 @@ public class LeaderElection implements Watcher {
} }
@Override @Override
public void process(WatchedEvent watchedEvent) { public void process(WatchedEvent watchedEvent)
switch (watchedEvent.getType()) { {
switch (watchedEvent.getType())
{
case None: case None:
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) if (watchedEvent.getState() == Event.KeeperState.SyncConnected)
{ {
...@@ -42,13 +54,7 @@ public class LeaderElection implements Watcher { ...@@ -42,13 +54,7 @@ public class LeaderElection implements Watcher {
synchronized (zooKeeper) synchronized (zooKeeper)
{ {
zooKeeper.notifyAll(); zooKeeper.notifyAll();
try {
zooKeeper.close();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} }
} }
else if (watchedEvent.getState() == Event.KeeperState.Disconnected) else if (watchedEvent.getState() == Event.KeeperState.Disconnected)
...@@ -56,15 +62,8 @@ public class LeaderElection implements Watcher { ...@@ -56,15 +62,8 @@ public class LeaderElection implements Watcher {
System.out.println("Disconnected"); System.out.println("Disconnected");
synchronized (zooKeeper) synchronized (zooKeeper)
{ {
zooKeeper.notifyAll(); zooKeeper.notifyAll();
try {
zooKeeper.close();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} }
///System.exit(0);
} }
break; 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