Reputation: 6707
I have a fundamental question about Java RMI, I have mostly a C++ background, and there is something I don't get with RMI.
To be clear, I am able to make it work fine, but it's something about the program lifetime.
If I have a toy Java program such as:
public class Test1 {
public static void main(String args[]) {
System.out.println( "start !" );
}
}
when I run it from shell, it prints the message and exits.
If I want to use sockets, then I need to have some infinite loop in my server, that will wait for incoming connections, process them, then start over.
ServerSocket socketserver = new ServerSocket( port_id );
System.out.println( "server: waiting" );
while(true) {
Socket socket = socketserver.accept();
System.out.println( "server: incoming connection" );
... do stuff...
socket.close();
}
Now, say I have an RMI server with the following main()
method:
public static void main(String args[]) throws Exception {
System.out.println( "RMI server started !" );
MyObject obj = new MyObject();
Naming.bind( "//localhost/myObject", obj );
System.out.println( "Done!" );
}
Assuming the class MyObject
can be found, and that everything compiles fine.
If I run this from the shell, then although it does print the last message and that is no infinite loop after that line, the program does not exit.
Questions:
The linked question answers the first question, but not the second. Neither does the Oracle tutorial (unless I missed that ?)
Upvotes: 0
Views: 321
Reputation: 310985
Upvotes: 1
Reputation: 5760
You should implement a thread that listens for client connections and creates worker threads to respond to clients. When the main thread terminated the joined threads will also terminate.
Upvotes: 0