Clean way to stop RMI server

前端 未结 2 529
粉色の甜心
粉色の甜心 2021-01-15 15:36

A RMI server which works fine without the stopServer functionality.

public class HelloServer extends UnicastRemoteObject implements HelloInterface
{
    priv         


        
相关标签:
2条回答
  • 2021-01-15 16:20

    You need to store the result of LocateRegistry.createRegistry(), and unexport that. At present you're trying to unexport a stub.

    0 讨论(0)
  • 2021-01-15 16:20

    I implemented a shutdown-service in my rmi-server. If I want to shut it down, I call it with a password. Simple Example:

    public interface ShutdownInterface extends Remote {
      public void shutdownService(String password) throws RemoteException;
    }
    

    The serverside implementation can look something like:

    public class ShutdownService extends UnicastRemoteObject implements ShutdownInterface {
    
    private static final long serialVersionUID = 1L;
    
    private boolean doShutdown = false;
    
    public ShutdownService() throws RemoteException {
        super();
    }
    
    @Override
    public void shutdownService(String password) throws RemoteException {
        if ("abcde12345".equals(password)) {
            System.out.println("shutdown requested.");
            this.doShutdown = true;
        } else {
            System.out.println("wrong pwd for shutdown");
        }
    
    }
    
    public boolean isDoShutdown() {
        return this.doShutdown;
    }
    

    }

    Now the server itself keeps a reference to this:

    public class BackendServer {
    public final static int RMI_PORT = 1974;
    private Registry registry = null;
    private ShutdownService shutdownService = null;
    
    public BackendServer() throws RemoteException {
        registry = LocateRegistry.createRegistry(RMI_PORT);
        this.shutdownService = new ShutdownService();
    }
    
    public void initialize() throws AccessException, RemoteException, AlreadyBoundException {
        shutdownService = new ShutdownService();
        registry.bind("ShutdownService", shutdownService);
        registry.bind("MyDataService", new MyDataService());
    }
    
    public void stop() throws NoSuchObjectException {
        System.out.println("stopping rmi server.");
        UnicastRemoteObject.unexportObject(registry, true);
        System.exit(0);
    }
    
    public boolean shouldStop() {
        return this.shutdownService.isDoShutdown();
    }
    
    public static void main(String args[]) {
        try {
            BackendServer bs = new BackendServer();
            bs.initialize();
            System.out.println("Server ready.");
    
            while (!bs.shouldStop()) {
                Thread.sleep(1000);
            }
            bs.stop();
        } catch (Exception e) {
            System.err.println("Server exception: " + e.toString());
            e.printStackTrace();
        }
    }
    

    }

    Of course, this can be realized in a more beautiful way, but this should give you an idea of how to easily implement a shutdown yourself. You can call it from the main client or from a small commandline-tool you code for your server.

    0 讨论(0)
提交回复
热议问题