Terminate Java RMI server application

LaDude

I have set up a client/server project using Java RMI. Below I show parts of the implementation. I launch the server using

ProcessBuilder processBuilder = new ProcessBuilder("cmd", "/C", "start /B java -jar myServer.jar --start);
this.myProcess = processBuilder.start();

I have added a main method to the server which handles the command line call. The server starts and runs perfectly. The client is able to connect and perform as I expect it to.

My problems arise when I try to kill the server. This needs to be done from outside. The object which previously started the process is not available anymore. To actually stop the server, the main method of the server class calls a stop method (see below). This method now kills the RMI but it does not the least stop the JVM from running. The process is still available and needs to be killed from the task manager (on Windows).

Do I miss some fact in my implementation that yields this behavior. Why does the process not stop running?

public class MyServer {

    // ...

    public void startServer() throws RemoteException {

        // the RMI registry
        Registry registry;

        try {
            // find the registry
            registry = LocateRegistry.createRegistry(portRMI);
        } catch (Exception e) {
            // if the registry does not yet exist, try to create a new registry
            // entry or use an existing one
            registry = LocateRegistry.getRegistry(MyConstants.HOST_NAME, portRMI);
        }

        // create the object
        servantObject = new MyServant();

        // bind the object to the server
        registry.rebind(MyConstants.SERVER_NAME, servantObject);
    }

    public void stopServer() throws RemoteException {

        try {
            // access the service
            Registry rmiRegistry = LocateRegistry.getRegistry(MyConstants.HOST_NAME, portRMI);
            MyService myService = (MyService) rmiRegistry.lookup(MyConstants.SERVER_NAME);

            rmiRegistry.unbind(MyConstants.SERVER_NAME);

            // get rid of the service object
            UnicastRemoteObject.unexportObject(myService, true);

            // get rid of the rmi registry
            UnicastRemoteObject.unexportObject(rmiRegistry, true);

        } catch (NoSuchObjectException e) {
            // ...
        } catch (NotBoundException e) {
            // ...
        }
    }

    public static void main(String[] args) {
        // handle arguments and call either startServer or stopServer
    }
}

public class MyServant extends UnicastRemoteObject implements MyService {
    // ...
}

public interface MyService extends Remote {
    // ...
}
GerritCap

You should add a self writtem RMIService interface available in your RMIServer so that a program that wishes to stop the running server instructs it to stop.

Your app that tries to stop the server just unbinds some object, as it is not the running rmi server process itself it will not have a big effect.

if your rmi sever is process a, you should write an app (using rmi) running as process b to send a message to process a to stop it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

java RMI error in server

From Dev

RMI Java application not working as expected

From Dev

Where is the Java RMI server listening on?

From Dev

Where is the Java RMI server listening on?

From Dev

Writing a secure RMI server-client application

From Dev

Java RMI: running RMI application over the Internet with public IP of router

From Dev

Java RMI application doesn't quit

From Dev

Is there a way to completely disable RMI in a java application?

From Dev

Running Java RMI application on two machines - ConnectException

From Dev

Is there a way to completely disable RMI in a java application?

From Dev

Java RMI:Updating client side objects in the server

From Dev

Java RMI Server does not print output

From Dev

Getting NotBoundException while writing a Java RMI chat application :-/

From Dev

Force SSL/TLS version for Java RMI server/client

From Dev

Returning a Java Vector from Server to Client using RMI

From Dev

In Java RMI, how can a client know that its server is dead?

From Dev

How to send a message from Server to Client using Java RMI?

From Dev

Java RMI registry can't find server object

From Dev

Java RMI client and remote server Connection Refused to localhost

From Dev

Simple RMI Server with SSL

From Dev

Loging in on an RMI server

From Dev

RMI UnmarshalException in server

From Dev

Spring RMI Server Error

From Dev

RMI Server vs. RMI Registry

From Dev

RMI Server vs. RMI Registry

From Dev

How to correctly terminate an application?

From Dev

mac application, terminate on close

From Dev

Java Web Server Application

From Dev

Java RMI - Returning a reference to another remote object on server from a remote method on server, to client

Related Related

HotTag

Archive