RMI 服务器停止?

发布于 2024-10-19 21:30:50 字数 442 浏览 1 评论 0原文

如何保持 RMI 服务器运行?目前,它只是绑定和对象,然后退出。

public class WutServer {
  public static void main(String[] args) throws RemoteException {
    Registry registry = LocateRegistry.createRegistry(1099);
    try {
      registry.bind("WutManager", new WutManager());
      System.out.println("Ready..");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

我只是运行这个类。我没有运行 rmic 或其他任何东西..

我如何强制它保持运行?

How do I keep an RMI server running? It currently, just binds and object, then exits..

public class WutServer {
  public static void main(String[] args) throws RemoteException {
    Registry registry = LocateRegistry.createRegistry(1099);
    try {
      registry.bind("WutManager", new WutManager());
      System.out.println("Ready..");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

I am simply running this class. I didn't run rmic or anything..

How do I force it to stay running?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

琴流音 2024-10-26 21:30:50

试试这个:

Remote stub = UnicastRemoteObject.exportObject(new WutManager(), 0);
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("WutManager", stub);

注意:WutManager 应该实现 java.rmi.Remote。

Try this:

Remote stub = UnicastRemoteObject.exportObject(new WutManager(), 0);
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("WutManager", stub);

Note: WutManager should implement java.rmi.Remote.

心凉怎暖 2024-10-26 21:30:50

您的服务器正在被 DGC 处理,然后被 GC 处理,这会导致它无法导出,如果没有其他事情可做,最终会导致 JVM 退出。要阻止这种情况,如果您通过 LocateRegistry.createRegistry() 创建注册表,请将该方法的返回值保留在静态变量中。否则保留对服务器对象的静态引用。

Your server is being DGC'd and then GC'd, which causes it to be unexported, which eventually causes the JVM to exit if it has nothing else to do. To stop that, if you are creating the Registry via LocateRegistry.createRegistry(), keep the return value of that method in a static variable. Otherwise keep a static reference to your server object.

深者入戏 2024-10-26 21:30:50

这是一个老问题,但这里有一个新答案。

在 OSX 上,使用最新的 Java 9.0.4 我发现该程序退出。如果我使用最新的 Java 1.8.0.162,那么它不会退出并且服务器仍保持运行。

This is an old question, but here is a new answer.

On OSX, using latest Java 9.0.4 I find that the program exits. If I use latest Java 1.8.0.162 then it does not exit and the server remains running.

允世 2024-10-26 21:30:50

需要使WutServer实现客户端访问它的接口,而该接口又应该继承自标记接口Remote。您可能还想让 WutServer 类继承 UnicastRemoteObject;虽然还有其他方法可以构建远程处理支持,但从 UnicastRemoteObject 继承绝对是最简单的方法。

请尝试这样做(尽管您可能应该将远程接口分离到另一个文件中并单独重新分发):

public class WutServer extends UnicastRemoteObject implements WutServer.Wut {
    interface Wut extends Remote {
        String wut() throws RemoteException;
    }
    // Because of the exception...
    public WutServer() throws RemoteException {}
    public String wut() { return "wut"; }
    public static void main(String[] args) throws RemoteException {
        LocateRegistry.createRegistry(1099).rebind("WutManager",new WutServer());
        System.out.println("Ready...");
    }
}

You need to make WutServer implement the interface that clients will access it by, which in turn should inherit from the marker interface Remote. You also probably want to make the WutServer class inherit from UnicastRemoteObject; while there are other ways to build the remoting support, inheriting from UnicastRemoteObject is definitely the easiest way to get something going.

Try this instead (though you should probably separate the remote interface into another file and have it be redistributed separately):

public class WutServer extends UnicastRemoteObject implements WutServer.Wut {
    interface Wut extends Remote {
        String wut() throws RemoteException;
    }
    // Because of the exception...
    public WutServer() throws RemoteException {}
    public String wut() { return "wut"; }
    public static void main(String[] args) throws RemoteException {
        LocateRegistry.createRegistry(1099).rebind("WutManager",new WutServer());
        System.out.println("Ready...");
    }
}
神经大条 2024-10-26 21:30:50

创建一个对象并在主函数末尾调用该对象的等待。那是;

public static void main(String[] args) throws RemoteException {
    Registry registry = LocateRegistry.createRegistry(1099);

    //your object to wait
    Object lockObject=new Object();

    try {            
              registry.bind("WutManager", new WutManager());            
              System.out.println("Ready..");

              //here makes your rmi server non-stop
              synchronized(lockObject){
                    lockObject.wait();
              }
     }catch (Exception e) {            
              e.printStackTrace();          
     } 
}

Create an object and call wait of the object at the end of the main function. That is;

public static void main(String[] args) throws RemoteException {
    Registry registry = LocateRegistry.createRegistry(1099);

    //your object to wait
    Object lockObject=new Object();

    try {            
              registry.bind("WutManager", new WutManager());            
              System.out.println("Ready..");

              //here makes your rmi server non-stop
              synchronized(lockObject){
                    lockObject.wait();
              }
     }catch (Exception e) {            
              e.printStackTrace();          
     } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文