我是java rmi的新手。我想创建一个 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();
当程序终止时,服务就消失了。什么是使服务器像它在后台运行的服务并且仍然可以调用“远程方法”的正确方法?
谢谢!