Commit a5b1727e authored by hengwen's avatar hengwen
Browse files

Register to zookeeper on start

parent a8959c93
......@@ -20,6 +20,9 @@
*/
package rwth.rmi.model.server;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.ZooKeeper;
import java.io.File;
import java.io.IOException;
import java.net.DatagramSocket;
......@@ -33,6 +36,7 @@ import java.rmi.server.UnicastRemoteObject;
public class RMIServer {
public final static String MODEL_MANAGER = "ModelManager";
public final static String ZK_NODE = "/rmi-servers/all";
public static void main(String[] args) {
if (args.length < 2){
......@@ -51,18 +55,30 @@ public class RMIServer {
return;
}
ZkClient zk = new ZkClient("localhost:2181", 1000, 5000);
ModelManager manager = new ModelManager(args[1]);
String host = "localhost";
int port = Integer.valueOf(args[0]);
try {
int port = Integer.valueOf(args[0]);
Registry registry = LocateRegistry.createRegistry(port);
System.out.println("RMIRegistry serving on port: " + port);
//bind the ModelManager to a stub, representing the remote object
registry.bind(MODEL_MANAGER, UnicastRemoteObject.exportObject(manager, 0));
register(zk, host, port);
System.out.println("RMIManager listening");
} catch (RemoteException | AlreadyBoundException e) {
e.printStackTrace();
}
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run(){
unregister(zk, host, port);
}
});
}
public static boolean isPortAvailable(String portString) {
......@@ -97,4 +113,25 @@ public class RMIServer {
}
return false;
}
public static void register(ZkClient zk, String host, int port){
String path = ZK_NODE + "/" + host + ":" + port;
if (!zk.exists(ZK_NODE)){
zk.createPersistent(ZK_NODE, true);
}
// delete in case a node is already there
// this usually happens when a simulator was forced shutdown without unregister itself from zookeeper
zk.delete(path);
zk.createEphemeral(path);
// zkPath = path;
System.out.printf("%s is online\n", path);
}
public static void unregister(ZkClient zk, String host, int port){
System.out.println("shutting down rmi-server");
String path = ZK_NODE + "/" + host + ":" + port;
zk.delete(path);
System.out.printf("%s is offline\n", path);
}
}
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