문제
매우 간단한 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
이 방법에서 내가 무엇을 잘못하고 있습니까?
해결책
누군가가 비슷한 문제를 겪고 있다면, 나는 대답을 직접 알아 냈습니다. 다음은 내 출구 () 메소드입니다.
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){}
}
다른 팁
실제로 등록하지 않고 즉시 System.exit은 깨끗하게 종료되지 않습니다. 클라이언트에게 메시지가 완료되었음을 알리기 전에 기본적으로 연결을 중단합니다. 작동하는 것은 시스템을 종료하는 작은 스레드를 시작하는 것입니다.
public void quit() throws RemoteException {
System.out.println("quit");
Registry registry = LocateRegistry.getRegistry();
try {
registry.unbind(_SERVICENAME);
UnicastRemoteObject.unexportObject(this, false);
} catch (NotBoundException e) {
throw new RemoteException("Could not unregister service, quiting anyway", e);
}
new Thread() {
@Override
public void run() {
System.out.print("Shutting down...");
try {
sleep(2000);
} catch (InterruptedException e) {
// I don't care
}
System.out.println("done");
System.exit(0);
}
}.start();
}
스레드는 여전히 종료 방법에서 돌아 오는 동안 미래에 무언가가 일어날 수 있어야합니다.
제휴하지 않습니다 StackOverflow