Java 完全关闭rmi服务器
我在关闭服务器组件时遇到了一些问题,希望得到一些帮助 我的服务器代码如下所示,它有一个关闭服务器的方法 服务器Java 完全关闭rmi服务器,java,rmi,Java,Rmi,我在关闭服务器组件时遇到了一些问题,希望得到一些帮助 我的服务器代码如下所示,它有一个关闭服务器的方法 服务器 private final String address = "127.0.0.1"; private Registry registry; private int port = 6789; public RmiServer() throws RemoteException { try { registry =
private final String address = "127.0.0.1";
private Registry registry;
private int port = 6789;
public RmiServer() throws RemoteException {
try {
registry = LocateRegistry.createRegistry(port);
registry.rebind("rmiServer", this);
} catch (RemoteException e) {
logger.error("Unable to start the server. Exiting the application.", e);
System.exit(-1);
}
}
public void shutDownServer() throws RemoteException {
int succesful = 0;
try {
registry.unbind("rmiServer");
UnicastRemoteObject.unexportObject(this, true);
Thread.sleep(1000);
} catch (NotBoundException e) {
logger.error("Error shutting down the server - could not unbind the registry", e);
succesful = -1;
} catch (InterruptedException e) {
logger.info("Unable to sleep when shutting down the server", e);
succesful = -1;
}
catch (AccessException e) {
logger.info("Access Exception", e);
succesful = -1;
}
catch (UnmarshalException e) {
System.out.println(e.detail.getMessage());
logger.info("UnMarshall Exception", e);
succesful = -1;
}
catch (RemoteException e) {
System.out.println(e.detail.getMessage());
logger.info("Remote Exception", e);
succesful = -1;
}
logger.info("server shut down gracefully");
System.exit(succesful);
}
我的客户端连接良好,没有问题,所以要关闭,我创建了一个新的应用程序,复制了客户端代码以连接,然后在服务器上调用close方法
关闭
公共类关闭{
private String serverAddress = "127.0.0.1";
private String serverPort = "6789";
private ReceiveMessageInterface rmiServer;
private Registry registry;
public Shutdown(){
try {
registry = LocateRegistry.getRegistry(serverAddress, (new Integer(serverPort)).intValue());
rmiServer = (ReceiveMessageInterface) (registry.lookup("rmiServer"));
logger.info("Client started correctly");
rmiServer.shutDownServer();
System.exit(0);
}
catch (UnmarshalException e ){
logger.error("Unmarshall exception. Exiting application", e);
System.exit(-1);
}
catch (RemoteException e) {
logger.error("Remote object exception occured when connecting to server. Exiting application", e);
System.exit(-1);
} catch (NotBoundException e) {
logger.error("Not Bound Exception occured when connecting to server. Exiting application", e);
System.exit(-1);
}
}
无论我尝试什么,我总是得到以下例外
ERROR com.rmi.client.RMIClient - Unmarshall exception. Exiting application
java.rmi.UnmarshalException: Error unmarshaling return header; nested exception is:
java.net.SocketException: Connection reset
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at $Proxy0.shutDownServer(Unknown Source)
at com.rmi.shutdown.Shutdown.<init>(Shutdown.java:31)
at com.rmi.shutdown.Shutdown.main(Shutdown.java:52)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.DataInputStream.readByte(Unknown Source)
... 7 more
错误com.rmi.client.RMIClient-解组异常。正在退出应用程序
java.rmi.UnmarshaleException:对返回头进行解组时出错;嵌套异常为:
java.net.SocketException:连接重置
位于sun.rmi.transport.StreamRemoteCall.executeCall(未知源)
位于sun.rmi.server.UnicastRef.invoke(未知源)
位于java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(未知源)
位于java.rmi.server.RemoteObjectInvocationHandler.invoke(未知源)
位于$Proxy0.shutDownServer(未知源)
在com.rmi.shutdown.shutdown.(shutdown.java:31)
位于com.rmi.shutdown.shutdown.main(shutdown.java:52)
原因:java.net.SocketException:连接重置
位于java.net.SocketInputStream.read(未知源)
位于java.io.BufferedInputStream.fill(未知源)
位于java.io.BufferedInputStream.read(未知源)
位于java.io.DataInputStream.readByte(未知源)
…还有7个
我相信这可能是因为客户端没有正确断开连接,只是被“切断”,但我不确定如何断开服务器端的连接
请给我一些建议
感谢系统正按照您告诉它的方式进行操作。您让它自行取消报告,并将“force”参数设置为true,该参数将中止正在进行的调用,因此它自行取消报告并中止正在进行的调用。只需忽略它,或者如果您坚持要对关闭的客户端做出干净的响应,请让服务器为其启动一个新线程unexport操作,具有短延迟,因此关机调用可以返回到客户端。unexport with force=true不会中止正在进行的调用。通常,它会让正在进行的调用运行到完成。您的
shutDownServer
方法几乎是正确的,因为它取消注册远程引用并将其unexport。它的作用是什么不过,next不起作用。首先,它会休眠一秒钟。这会使调用继续进行,并使客户端等待答复。然后关闭代码退出服务器JVM,而不会从远程调用返回。这会在客户端仍在等待答复时关闭客户端的连接。这就是客户端获得连接重置异常的原因
要完全关闭,请取消注册远程对象,并使用force=true将其取消导出(如您所做的那样)然后返回。这将向客户端发送回复,让其远程调用完成,然后客户端将退出。在最后一次进行中的调用完成后,如果没有导出其他对象,并且没有其他东西保持JVM(如非守护进程线程),则返回服务器JVM将退出。您需要让RMI完成其服务器端处理,而不是调用
System.exit()
@ejb。我还想关闭客户端,就像数据库连接一样。关闭任何帮助吗?请解释以下内容:“然后简单地返回”?返回什么?@iJava通常从方法返回,而不是抛出异常或调用System.exit()
。在这种情况下,删除所有exit()调用将使控件从方法的末尾运行,这将起作用。return
语句也会起作用。方法调用的正确顺序是:UnicastRemoteObject.unexportObject(this,true);
thenregistry.unbind(“rmiServer”);
当初始化期间绑定失败时,它会起作用。