How to close java RMI connection?

N

nadmasl

I have seen many examples how to close sockets but nothing about how to
tear down RMI client/server connection properly. Is there a way to do
this graceously? In the server code below, I exit by invoking
System.exit(1) but it doesn't close objects.

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class ServerRMI {
public static void main(String[] args) {
boolean shutdown = false;
try {
Server obj = new Server();
registerToRegistry("Server", obj);
System.out.println("Server " + obj + " started");
} catch (RemoteException ex) {
ex.printStackTrace();
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
SimpleIO.prompt("To close RMI server type \"close\": ");
String command = null;

while(!shutdown){
command = SimpleIO.readLine();
if (command.equals("close")){
shutdown = true;
System.out.println("Server is shutting down...");
System.exit(1);
}
}

}

public static void registerToRegistry(String name, Remote obj)
throws RemoteException, MalformedURLException{
if (name == null) throw new IllegalArgumentException(
"registration name can not be null");
try {
Naming.rebind(name, obj);
}
catch (RemoteException ex){
Registry r =
LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
r.rebind(name, obj);
}

}

}
 
E

EJP

I have seen many examples how to close sockets but nothing about how to
tear down RMI client/server connection properly. Is there a way to do
this graceously? In the server code below, I exit by invoking
System.exit(1) but it doesn't close objects.

I do not understand. System.exit() closes everything.

However have a look at UnicastRemoteObject.unexportObject().

The connection between an RMI client and server is implicit and it
closes itself automatically via a short idle timeout.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

JAVA RMI PROBLEM 0
RMI & connection refused 10
RMI: Can not connect Client on a custom port 2
Java RMI - registry questions 3
RMI newbie 1
simple RMI chat problem 1
RMI Objects Help 7
java rmi error 1

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top