程序关闭并重新启动而另一个程序在Java中运行时,如何恢复RMI通信?

时间:2015-08-28 01:36:42

标签: java rmi

所以我目前有很多代码,很难将它全部分解为SSCCE但是如果有必要我可能会在稍后尝试这样做。

无论如何,这里是要点:我有两个进程通过RMI进行通信。有用。但是,如果主机进程(JobViewer)退出,然后在客户端进程(作业)的生命周期中返回所有通信,我希望能够继续。

目前,每当作业启动时,我都会将绑定名称保存到文件中,并且JobViewer会在启动时打开此文件。它工作得很好,正确的绑定名称有效。但是,每当我尝试恢复与Job的通信时,我得到一个NotBoundException,当JobViewer重新启动时,我知道事实仍在运行。

我的JobViewer实现了一个使用以下方法扩展Remote的接口:

public void registerClient(String bindedName, JobStateSummary jobSummary) throws RemoteException, NotBoundException;
public void giveJobStateSummary(JobStateSummary jobSummary) throws RemoteException;
public void signalEndOfClient(JobStateSummary jobSummary) throws RemoteException;

我的Job还使用以下方法实现了一个扩展Remote的不同界面:

public JobStateSummary getJobStateSummary() throws RemoteException;
public void killRemoteJob() throws RemoteException;
public void stopRemoteJob() throws RemoteException;
public void resumeRemoteJob() throws RemoteException;

我如何实现这一目标?以下是我目前的一些代码,如果它有帮助,就会进入RMI ......

JobViewer方:

private Registry _registry;
// Set up RMI
_registry = LocateRegistry.createRegistry(2002);
_registry.rebind("JOBVIEWER_SERVER", this);

工作方:

private NiceRemoteJobMonitor _server;

Registry registry = LocateRegistry.getRegistry(hostName, port);
registry.rebind(_bindedClientName, this);
Remote remoteServer = registry.lookup(masterName);

_server = (NiceRemoteJobMonitor)remoteServer;
_server.registerClient(_bindedClientName, _jobStateSummary);

1 个答案:

答案 0 :(得分:-1)

我的解决方案是让Job经常ping JobViewer:

  while (true) {

    try {

      _server.ping();
      // If control reaches here we were able to successfully ping the job monitor.

    } catch (Exception e) {

      System.out.println("Job lost contact with the job monitor at " + new Date().toString() + " ...");

      // If control reaches we were unable to ping the job monitor.  Now we will loop until it presumably comes back to life.
      boolean foundServer = false;
      while (!foundServer) {

        try {

          // Attempt to register again.
          Registry registry = LocateRegistry.getRegistry(_hostName, _port);
          registry.rebind(_bindedClientName, NiceSupervisor.this);
          Remote remoteServer = registry.lookup(_masterName);
          _server = (NiceRemoteJobMonitor)remoteServer;
          _server.registerClient(_bindedClientName, _jobStateSummary);

          // Ping the server for good measure.
          _server.ping();

          System.out.println("Job reconnected with the job monitor at " + new Date().toString() + " ...");

          // If control reaches here we were able to reconnect to the job monitor and ping it again.
          foundServer = true;

        } catch (Exception x) {

          System.out.println("Job still cannot contact the job monitor at " + new Date().toString() + " ...");

        }

       // Sleep for 1 minute before we try to locate the registry again.
        try {
          Thread.currentThread().sleep(PING_WAIT_TIME);
        } catch (InterruptedException x) {

        }

     } // End of endless loop until we find the server again.

   }

    // Sleep for 1 minute after we ping the server before we try again.
    try {
      Thread.currentThread().sleep(PING_WAIT_TIME);
    } catch (InterruptedException e) {

    }

  }  // End of endless loop that we never exit.