Auto close rmi server

I'm having problems closing the server component and hope to get help.

My server code is as follows: it has a way to shut down the server

Server

private final String address = "127.0.0.1"; private Registry registry; private int port = 6789; public RmiServer() throws RemoteException { try { registry = LocateRegistry.createRegistry(port); registry.rebind("rmiServer", this); } catch (RemoteException e) { logger.error("Unable to start the server. Exiting the application.", e); System.exit(-1); } } public void shutDownServer() throws RemoteException { int succesful = 0; try { registry.unbind("rmiServer"); UnicastRemoteObject.unexportObject(this, true); Thread.sleep(1000); } catch (NotBoundException e) { logger.error("Error shutting down the server - could not unbind the registry", e); succesful = -1; } catch (InterruptedException e) { logger.info("Unable to sleep when shutting down the server", e); succesful = -1; } catch (AccessException e) { logger.info("Access Exception", e); succesful = -1; } catch (UnmarshalException e) { System.out.println(e.detail.getMessage()); logger.info("UnMarshall Exception", e); succesful = -1; } catch (RemoteException e) { System.out.println(e.detail.getMessage()); logger.info("Remote Exception", e); succesful = -1; } logger.info("server shut down gracefully"); System.exit(succesful); } 

My client connects perfectly, no problem, so to complete the work I created a new application, copied the client code for the connection and then called the close method on the server

Shutdown

open class Shutdown {

 private String serverAddress = "127.0.0.1"; private String serverPort = "6789"; private ReceiveMessageInterface rmiServer; private Registry registry; public Shutdown(){ try { registry = LocateRegistry.getRegistry(serverAddress, (new Integer(serverPort)).intValue()); rmiServer = (ReceiveMessageInterface) (registry.lookup("rmiServer")); logger.info("Client started correctly"); rmiServer.shutDownServer(); System.exit(0); } catch (UnmarshalException e ){ logger.error("Unmarshall exception. Exiting application", e); System.exit(-1); } catch (RemoteException e) { logger.error("Remote object exception occured when connecting to server. Exiting application", e); System.exit(-1); } catch (NotBoundException e) { logger.error("Not Bound Exception occured when connecting to server. Exiting application", e); System.exit(-1); } } 

No matter what I try, I get the following exception:

 ERROR com.rmi.client.RMIClient - Unmarshall exception. Exiting application java.rmi.UnmarshalException: Error unmarshaling return header; nested exception is: java.net.SocketException: Connection reset at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source) at sun.rmi.server.UnicastRef.invoke(Unknown Source) at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source) at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source) at $Proxy0.shutDownServer(Unknown Source) at com.rmi.shutdown.Shutdown.<init>(Shutdown.java:31) at com.rmi.shutdown.Shutdown.main(Shutdown.java:52) Caused by: java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) at java.io.BufferedInputStream.fill(Unknown Source) at java.io.BufferedInputStream.read(Unknown Source) at java.io.DataInputStream.readByte(Unknown Source) ... 7 more 

I believe that this may be due to the fact that the client is not disconnected properly and just β€œdisconnected”, but I'm not sure how to disconnect the server side yet?

Please, can someone advise.

thanks

+4
source share
2 answers

Unexport with force = true does not interrupt ongoing calls. In general, this will allow calls to be made at runtime. Your shutDownServer method shutDownServer almost right, as it unregisters the deleted link and does not export it. However, what he does next does not work. First, he sleeps for one second. This delays the call and makes the client wait for a response. Then, the exit code exits the JVM server without returning from the remote call. This closes the client connection while it is still waiting for a response. That is why the client receives a reset exception connection.

To disable cleaning, unregister on the remote object, disable it with force = true (as you did), and then just return. This will send a response to the client to complete its remote call, and it will exit. Returning to the server after the completion of the last completed call, if there are no other exported objects, and if nothing supports the JVM (for example, non-daemon threads), the JVM will exit. You should let RMI complete server-side processing instead of calling System.exit() .

+5
source

The system does exactly what you told him. You talked about this to the non-export itself, and you set the "force" parameter to true, which interrupts the calls being made, so it did not open itself and interrupted the current call. Just ignore this, or if you insist on a clear response to the shutdown client, try starting a new thread for the unport operation with a slight delay so that turning off the call can return to the client.

0
source

All Articles