RMI 服务器停止?
如何保持 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个:
注意:WutManager 应该实现 java.rmi.Remote。
Try this:
Note: WutManager should implement java.rmi.Remote.
您的服务器正在被 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.这是一个老问题,但这里有一个新答案。
在 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.
您需要使
WutServer
实现客户端访问它的接口,而该接口又应该继承自标记接口Remote
。您可能还想让WutServer
类继承UnicastRemoteObject
;虽然还有其他方法可以构建远程处理支持,但从 UnicastRemoteObject 继承绝对是最简单的方法。请尝试这样做(尽管您可能应该将远程接口分离到另一个文件中并单独重新分发):
You need to make
WutServer
implement the interface that clients will access it by, which in turn should inherit from the marker interfaceRemote
. You also probably want to make theWutServer
class inherit fromUnicastRemoteObject
; while there are other ways to build the remoting support, inheriting fromUnicastRemoteObject
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):
创建一个对象并在主函数末尾调用该对象的等待。那是;
Create an object and call wait of the object at the end of the main function. That is;