2013-01-24 90 views
0

我是java rmi新手。我想創建一個rmi程序作爲服務。例如,我有一個遠程接口:Java RMI編程服務

public interface Handler implements Remote { 
    public void insert (String str) throws RemoteException, NotBoundException; 
} 

public class HandlerImpl extends UnicastRemoteObject implements Handler { 
    public HandlerImpl (int port) { 
     super(port); 
    } 

    public void insert (String str) throws RemoteException, NotBoundException { 
     // insert string to a file 
    } 
} 

而且我也有一個類進行註冊:

class Server { 
    public Server() { 
     Registry svcReg = LocateRegistry.createRegistry(999); 
     Handler handler = new HandlerImpl (1000); 
     svcReg.rebind("insert", handler); 
    } 
} 

現在,如果編寫程序用

Server server = new Server(); 

當程序終止,服務不見了。什麼是使服務器像後臺運行服務一樣的正確方法,並且「遠程方法」仍然可以被調用?

謝謝!

回答