我已经调试了每一行代码以及多个工作示例,但由于某种原因,当我启动 RMI 服务器时,我的 java 应用程序刚刚关闭。
我希望 java 应用程序能够继续运行,因为我已经启动了一个注册表并且我已经反弹了一个实现java.rmi.Remote
.
但是发生的情况是应用程序在创建完所有内容后立即停止。
这是我的 Main.java:
package com.distribridge.servercomponent;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
private static Logger logger = Logger.getLogger(Main.class.getName());
private static int portNumber = 1099;
private Main() {
logger.log(Level.INFO, "Starting component");
Server server;
try {
server = new Server();
} catch (Exception e) {
logger.log(Level.SEVERE, "Could not create server.");
System.exit(1);
return;
}
logger.log(Level.FINE, "Server created.");
Registry registry;
try {
registry = LocateRegistry.createRegistry(portNumber);
} catch (Exception e) {
logger.log(Level.SEVERE, "Could not create registry");
System.exit(1);
return;
}
logger.log(Level.FINE, "Registry created.");
try {
registry.rebind("server", server);
} catch (Exception e) {
logger.log(Level.SEVERE, "Could not bind server.");
System.exit(1);
return;
}
logger.log(Level.FINE, "Server bound.");
logger.log(Level.INFO, "Component started.");
}
public static void main(String[] args) {
System.out.println("Start");
Main main = new Main();
}
}
我的 Server.java 看起来像这样:
public class Server implements IServerForClientLogin, IServerForClient, IServerForTable, Remote {
Server() {
System.out.println("Server Constructor");
}
//Some methods that don't get called yet.
}
服务器扩展的接口位于“共享”模块中。我用那个模块开始了这个项目,并添加了像服务器这样的模块。
这是我的控制台输出:
Start
Jan 03, 2018 1:10:05 PM com.distribridge.servercomponent.Main <init>
INFO: Starting component
Server Constructor
Jan 03, 2018 1:10:05 PM com.distribridge.servercomponent.Main <init>
INFO: Component started.
Process finished with exit code 0
如您所见,当注册表绑定远程对象时,该进程立即关闭。