禁用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 = 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.(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

我相信这可能是因为客户端未正确断开连接并且只是“切断”但我不确定如何断开服务器端的连接?

请有人建议.

谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值