Commit 180305ef authored by mohammad.salama's avatar mohammad.salama

Exit when Connection is Disconnected or Closed

parent 8baf18bc
......@@ -3,47 +3,7 @@
<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$/REAM ME.txt" beforeDir="false" afterPath="$PROJECT_DIR$/REAM ME.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ZookeeperProject.iml" beforeDir="false" afterPath="$PROJECT_DIR$/ZookeeperProject.iml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.8.3/audience-annotations-0.12.0.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.8.3/commons-io-2.11.0.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.8.3/logback-classic-1.2.10.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.8.3/logback-core-1.2.10.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.8.3/netty-buffer-4.1.94.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.8.3/netty-codec-4.1.94.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.8.3/netty-common-4.1.94.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.8.3/netty-handler-4.1.94.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.8.3/netty-resolver-4.1.94.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.8.3/netty-transport-4.1.94.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.8.3/netty-transport-classes-epoll-4.1.94.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.8.3/netty-transport-native-epoll-4.1.94.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.8.3/netty-transport-native-unix-common-4.1.94.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.8.3/slf4j-api-1.7.30.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.8.3/zookeeper-3.8.3.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.8.3/zookeeper-jute-3.8.3.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/audience-annotations-0.12.0.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/commons-io-2.11.0.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/logback-classic-1.2.10.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/logback-core-1.2.10.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/netty-buffer-4.1.94.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/netty-codec-4.1.94.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/netty-common-4.1.94.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/netty-handler-4.1.94.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/netty-resolver-4.1.94.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/netty-tcnative-boringssl-static-2.0.61.Final-linux-aarch_64.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/netty-tcnative-boringssl-static-2.0.61.Final-linux-x86_64.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/netty-tcnative-boringssl-static-2.0.61.Final-osx-aarch_64.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/netty-tcnative-boringssl-static-2.0.61.Final-osx-x86_64.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/netty-tcnative-boringssl-static-2.0.61.Final-windows-x86_64.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/netty-tcnative-boringssl-static-2.0.61.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/netty-tcnative-classes-2.0.61.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/netty-transport-4.1.94.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/netty-transport-classes-epoll-4.1.94.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/netty-transport-native-epoll-4.1.94.Final-linux-x86_64.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/netty-transport-native-unix-common-4.1.94.Final.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/slf4j-api-1.7.30.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/zookeeper-3.9.1.jar" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/zookeeper-3.9.1/zookeeper-jute-3.9.1.jar" beforeDir="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" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......
......@@ -4,38 +4,48 @@ import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
public class LeaderElection implements Watcher
{
public class LeaderElection implements Watcher {
private static final String address = "192.168.184.10:2181";
private static final int SESSION_TIMEOUT = 3000; //dead client
private static final int SESSION_TIMEOUT = 3000;
private ZooKeeper zooKeeper;
public static void main(String[] args) throws IOException, InterruptedException
{
public static void main(String[] args) throws IOException, InterruptedException {
LeaderElection leaderElection = new LeaderElection();
leaderElection.connectToZookeeper();
leaderElection.run();
}
public void connectToZookeeper() throws IOException
{
public void connectToZookeeper() throws IOException {
this.zooKeeper = new ZooKeeper(address, SESSION_TIMEOUT, this);
}
public void run() throws InterruptedException {
synchronized (zooKeeper) {
zooKeeper.wait();
synchronized (zooKeeper)
{
zooKeeper.wait();
}
}
@Override
public void process(WatchedEvent watchedEvent)
{
public void process(WatchedEvent watchedEvent) {
switch (watchedEvent.getType()) {
case None:
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
System.out.println("Successfully connected to Zookeeper");
if (watchedEvent.getState() == Event.KeeperState.SyncConnected)
{
System.out.println("Successfully connected to ZooKeeper");
}
else if (watchedEvent.getState() == Event.KeeperState.Closed)
{
System.out.println("Connection Closed");
System.exit(0);
}
else if (watchedEvent.getState() == Event.KeeperState.Disconnected)
{
System.out.println("Disconnected");
System.exit(0);
}
break;
}
}
}
}
\ No newline at end of file
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