Search code examples
javarmidistributed-system

execute a task before shutting down RMI server


I'm using java RMI to implement a distributed bank system where each server represents a branch with it data stored on its own database when closing a server/branch , the accounts stored in its database should be transferred to another server/branch how to execute the code of this task just before the server is closed any help is appreciated and thanks in advance


Solution

  • Maybe shutdownHook will be good for you.

    Example code:

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
        System.out.println("shutdown application");
    
        }
    }));