How to close rmiregistry running on particular port?

纵然是瞬间 提交于 2019-11-27 03:27:45

问题


I am working on Java RMI. I am having little issue with running my rmiregistry on port 2028 as I allready used that one to run my test program. I can run my program using other port but I would like to know, How we can close rmiregistry running on perticular port ?


回答1:


If you want to do this in programming, we do something like:

// create the registry
Registry rmiRegistry = LocateRegistry.createRegistry(port);
...
// connect to it
JMXConnectorServer connector =
    JMXConnectorServerFactory.newJMXConnectorServer(url,
        new HashMap<String, Object>(),
        ManagementFactory.getPlatformMBeanServer());
// do stuff with it ...

// close the connection
if (connector != null) {
    connector.stop();
}
// deregister the registry
if (rmiRegistry != null) {
    UnicastRemoteObject.unexportObject(rmiRegistry, true);
}

Here's the full code for our JMXServer class. We have problems creating 2 of these and completely unregistering them so we make sure to run our unit tests on different ports.

I use this code in my SimpleJmx JMX client/service package.




回答2:


After so much of hassle I suddenly realize that rmiregistry runs in background of shell. So all we have to do close it first bring it to foreground and then close it. And it worked.

BTW to bring it to foreground just type:

% fg

and then to close it type:

Ctrl + c

That's it. Thanks a lot everyone who tried to help me out.




回答3:


If you are running rmiregistry from the shell try to close it with:

Process p = 
Runtime.getRuntime().exec("ps -ef | grep rmiregistry | awk '{ print $2 }' | kill -9");

I'm not so fresh with shell commands, but I hope you get the idea.




回答4:


If the rmi registry is already using the port and you want to rebind a service without using another port. You can try the code below

Registry registry = null;
try {
    registry = LocateRegistry.createRegistry(1099);
} catch (ExportException ex) {
   registry = LocateRegistry.getRegistry(1099);
} catch (RemoteException ex) {
  Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}


来源:https://stackoverflow.com/questions/8386001/how-to-close-rmiregistry-running-on-particular-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!