Linux 如何关闭在特定端口上运行的 rmiregistry?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8386001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to close rmiregistry running on particular port?
提问by slonkar
I am working on Java RMI. I am having little issue with running my rmiregistry on port 2028 as I already 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 particular port ?
我正在研究 Java RMI。我在端口 2028 上运行我的 rmiregistry 没有什么问题,因为我已经使用它来运行我的测试程序。我可以使用其他端口运行我的程序,但我想知道,我们如何关闭在特定端口上运行的 rmiregistry ?
采纳答案by Gray
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.
这是我们 JMXServer 类的完整代码。我们在创建其中 2 个并完全取消注册它们时遇到问题,因此我们确保在不同的端口上运行我们的单元测试。
I use this code in my SimpleJmxJMX client/service package.
我在SimpleJmxJMX 客户端/服务包中使用此代码。
回答by Tudor
If you are running rmiregistry from the shell try to close it with:
如果您从 shell 运行 rmiregistry,请尝试使用以下命令关闭它:
Process p =
Runtime.getRuntime().exec("ps -ef | grep rmiregistry | awk '{ print }' | kill -9");
I'm not so fresh with shell commands, but I hope you get the idea.
我对 shell 命令不太熟悉,但我希望你能理解。
回答by slonkar
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.
经过这么多的麻烦,我突然意识到 rmiregistry 在 shell 的后台运行。所以我们要做的就是关闭它,首先将其置于前台,然后再关闭它。它奏效了。
BTW to bring it to foreground just type:
顺便说一句,只需键入:
% fg
and then to close it type:
然后关闭它类型:
Ctrl + c
Ctrl + c
That's it. Thanks a lot everyone who tried to help me out.
就是这样。非常感谢所有试图帮助我的人。
回答by kayfun
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
如果 rmi 注册中心已经在使用该端口并且您想重新绑定一个服务而不使用另一个端口。你可以试试下面的代码
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);
}