Hello people...
I am doing an RMI project and I have run into a small problem. Hope someone can help me out. I have 3 RMI servers which all need to communicate via some back end channel (to do some time syncing work). I decided to use sockets for this since I have to send small amounts of data. The problem is that when I create the server socket in the constructor of one of my actual servers, it blocks the thread on creation. And the second instance of the actual server is not created at all. I will highlight it in the code below.
Is there SOME WAY that I can create a new thread and hand off just the socket part of the code below to the new one so that it can create and block without affecting the main thread executing?
public FrontEnd(String serverIP, int serverPort, String serverName, int processId, int portNum)
throws RemoteException
{
int FEserverPort = serverPort;
String FEserverIp = serverIP;
this.processID = processId;
this.socketPort = portNum;
try
{
FEReg = LocateRegistry.createRegistry(FEserverPort);
FEReg.rebind(serverName, this);
System.out.println ("Starting Server Instance on port " + FEserverPort);
System.out.println ("Server bound to name " + serverName);
BEReg = LocateRegistry.getRegistry(serverIp, backEndServerPort);
BEServer = (MessageInterface)(BEReg.lookup("tweetRMI"));
}
catch(Exception e)
{
System.out.println ("The Registry is causing an error");
e.printStackTrace();
}
try
{
this.serverSocket = new ServerSocket(socketPort);
this.incomingConn = serverSocket.accept (); ===================> Server socket creation and blocks here!!!
}
catch (Exception e)
{
System.out.println ("The Socket is causing an error");
e.printStackTrace();
}
objEATS = new ElectionAndTimeSync(processID, socketPort, incomingConn);
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
try
{
FrontEnd serverObject1 = new FrontEnd("localhost", 9002, "feRMI", 2, 10002); =============> This calls the constructor and the thread blocks when the socket is created.
FrontEnd serverObject2 = new FrontEnd("localhost", 9011, "fe1RMI", 3, 10003); =============> This is never called since the thread has blocked due to the above call.
}
catch (Exception e)
{
e.printStackTrace();
System.exit(1);
}
}