Java RMIサーバーをリモートでシャットダウンする方法

StackOverflow https://stackoverflow.com/questions/241034

  •  04-07-2019
  •  | 
  •  

質問

次のような非常に単純な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.");
        }
}

exitメソッドを呼び出すと、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){}
}

他のヒント

実際に登録を解除してすぐに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();
}

スレッドは、quitメソッドから戻っている間に、将来何かが起こるようにするために必要です。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top