gpt4 book ai didi

java - 如何远程关闭 Java RMI 服务器

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:02:56 30 4
gpt4 key购买 nike

我有一个非常简单的 Java RMI 服务器,如下所示:

    import java.rmi.*;
import java.rmi.server.*;

public class CalculatorImpl extends UnicastRemoteObject implements Calculator {

private String mServerName;

public CalculatorImpl(String serverName) throws RemoteException
{
super();
mServerName = serverName;
}

public int calculate(int op1, int op2) throws RemoteException
{
return op1 + op2;
}

public void exit() throws RemoteException
{
try{
Naming.unbind(mServerName);
System.out.println("CalculatorServer exiting.");
}
catch(Exception e){}

System.exit(1);
}

public static void main(String args[]) throws Exception
{
System.out.println("Initializing CalculatorServer.");

String serverObjName = "rmi://localhost/Calculator";

Calculator calc = new CalculatorImpl(serverObjName);

Naming.rebind(serverObjName, calc);

System.out.println("CalculatorServer running.");
}
}

当我调用退出方法时,System.exit(1) 抛出以下异常:

CalculatorServer exiting.
java.rmi.UnmarshalException: Error unmarshaling return header; nested exception is:
java.io.EOFException
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:203)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:126)
at CalculatorImpl_Stub.exit(Unknown Source)
at CalculatorClient.<init>(CalculatorClient.java:17)
at CalculatorClient.main(CalculatorClient.java:29)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:243)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:189)
... 4 more
[2]+ Exit 1 java CalculatorImpl

我在这个方法中做错了什么?

最佳答案

如果有人遇到类似问题,我自己想出了答案。这是我的 exit() 方法:

public void exit() throws RemoteException
{
try{
// Unregister ourself
Naming.unbind(mServerName);

// Unexport; this will also remove us from the RMI runtime
UnicastRemoteObject.unexportObject(this, true);

System.out.println("CalculatorServer exiting.");
}
catch(Exception e){}
}

关于java - 如何远程关闭 Java RMI 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/241034/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com