终止Java RMI服务器应用程序 [英] Terminate Java RMI server application

查看:128
本文介绍了终止Java RMI服务器应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Java RMI设置了客户端/服务器项目。下面我展示部分实现。 I
使用

I have set up a client/server project using Java RMI. Below I show parts of the implementation. I launch the server using

ProcessBuilder processBuilder = new ProcessBuilder("cmd", "/C", "start /B java -jar myServer.jar --start);
this.myProcess = processBuilder.start();


b $ b

我向处理命令行调用的服务器添加了一个main方法
服务器启动并运行完美。客户端能够按预期连接和执行。

I have added a main method to the server which handles the command line call. The server starts and runs perfectly. The client is able to connect and perform as I expect it to.

我尝试杀死服务器时出现问题,这需要从外部完成。
以前启动的进程的对象不再可用了。服务器,主要方法
的服务器类调用一个stop方法(见下面)这个方法现在杀死了RMI但是没有
最少停止JVM运行,进程仍然可用并需要从
任务管理器(在Windows上)杀死。

My problems arise when I try to kill the server. This needs to be done from outside. The object which previously started the process is not available anymore. To actually stop the server, the main method of the server class calls a stop method (see below). This method now kills the RMI but it does not the least stop the JVM from running. The process is still available and needs to be killed from the task manager (on Windows).

我错过了一些在实现中产生这种行为的事实,为什么进程不停止运行?

Do I miss some fact in my implementation that yields this behavior. Why does the process not stop running?

public class MyServer {

    // ...

    public void startServer() throws RemoteException {

        // the RMI registry
        Registry registry;

        try {
            // find the registry
            registry = LocateRegistry.createRegistry(portRMI);
        } catch (Exception e) {
            // if the registry does not yet exist, try to create a new registry
            // entry or use an existing one
            registry = LocateRegistry.getRegistry(MyConstants.HOST_NAME, portRMI);
        }

        // create the object
        servantObject = new MyServant();

        // bind the object to the server
        registry.rebind(MyConstants.SERVER_NAME, servantObject);
    }

    public void stopServer() throws RemoteException {

        try {
            // access the service
            Registry rmiRegistry = LocateRegistry.getRegistry(MyConstants.HOST_NAME, portRMI);
            MyService myService = (MyService) rmiRegistry.lookup(MyConstants.SERVER_NAME);

            rmiRegistry.unbind(MyConstants.SERVER_NAME);

            // get rid of the service object
            UnicastRemoteObject.unexportObject(myService, true);

            // get rid of the rmi registry
            UnicastRemoteObject.unexportObject(rmiRegistry, true);

        } catch (NoSuchObjectException e) {
            // ...
        } catch (NotBoundException e) {
            // ...
        }
    }

    public static void main(String[] args) {
        // handle arguments and call either startServer or stopServer
    }
}

public class MyServant extends UnicastRemoteObject implements MyService {
    // ...
}

public interface MyService extends Remote {
    // ...
}


推荐答案

您的应用程序尝试停止服务器只是取消绑定某些对象,因为它不是正在运行的rmi服务器进程本身,它不会有很大的影响。

Your app that tries to stop the server just unbinds some object, as it is not the running rmi server process itself it will not have a big effect.

如果你的rmi服务器是进程a,你应该写一个应用程序作为进程b运行,发送消息给进程a停止它。

if your rmi sever is process a, you should write an app (using rmi) running as process b to send a message to process a to stop it.

这篇关于终止Java RMI服务器应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆