Java RMI编程服务

时间:2013-01-24 03:39:22

标签: java rmi

我是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();

当程序终止时,服务就消失了。什么是使服务器像在后台运行的服务和“远程方法”仍然可以被调用的正确方法?

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用Apache Commons Daemon来完成此任务。