rmi 서버를 종료했다가 rmi를 실행할 때, 유의할 점이 있다.

실행시, 

처음 실행할 때는 createRegistry를 사용한다.
 LocateRegistry.createRegistry(rmiPort);

rmi 서버를 종료하고 다시 실행할 때는 getRegistry를 사용하면 된다.

코드는 다음과 같다.



 


/**

 * A RMI Server class binds or unbinds <code>NetworkDataBase</code> class.   

 * 

 * @author Kim, Yong Hwan

 * @version 1.0

 * 

 */

public class RMIServer {

/**

* <code>NetworkDataBase</code> object to bind or unbind

*/

private static NetworkDataBase ndb;

/**

* RMI Server port

*/

private static int port;

/**

* Checks if connection is valid

*/

private static boolean isConnected;

/**

* Checks if rmi server is running in first time

*/

private static boolean firstTime = true;

 

    /**

     * Binds the remote RMI database on the specified port.

     * 

     * @param dbLocation 

     *   database file location.

     * @param rmiPort

     *            port on the server.

     * @throws RemoteException

     *            Thrown if the remote database, <code>NetworkDataBase</code> 

     *            class could not be accessed.

     */

    public static void register(String dbLocation, int rmiPort) throws RemoteException {

    Registry registry;

    port = rmiPort;

    ndb = new NetworkDataBase(dbLocation); 

    if (firstTime) {

    registry = LocateRegistry.createRegistry(rmiPort);

    firstTime = false;

    } else {

    registry = LocateRegistry.getRegistry(rmiPort);

    }

        registry.rebind("database", ndb);


        isConnected = true;

    }

    

    /**

     * Unbinds and unexports the remote RMI database 

     * 

     * @throws RemoteException

     *            Thrown if the rmi server is not shutdown.

     */

    public static void exit() throws RemoteException {

    try {

Naming.unbind("rmi://127.0.0.1:" + port + "/database");

} catch (Exception e){

throw new RemoteException(e.getMessage());

}

        ndb = null;

        port = 0;

        isConnected = false;

    }

    

    /**

     * Gets if connection is valid;

     * @return return true, if connection is alive. Otherwise return false.

     */

    public static boolean isConnected() {

    return isConnected;

    }

}

Posted by '김용환'
,