如何在Java EE中以编程方式获取绑定的服务器地址和端口? [英] How to get bound server address and port programmatically in Java EE?
问题描述
在启动时,我们需要获取正在运行的应用程序的服务器地址和http端口。到目前为止,我们这样做:
At startup we need to get the server address and the http port of the running application. Until now we made it like this:
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName socketBindingMBean = new ObjectName("jboss.as:socket-binding-group=standard-sockets,socket-binding=http");
String host = (String) mBeanServer.getAttribute(socketBindingMBean, "boundAddress"),
Integer port = (Integer) mBeanServer.getAttribute(socketBindingMBean, "boundPort"));
一切都很好但是从jBoss 7.1.1.Final迁移到7.1.3.Final我们得到了在服务器启动时未定义MBean的问题。这意味着如果我在已经运行的 jboss服务器上部署应用程序,一切都很好,但是如果我启动服务器并且在服务器启动期间加载了应用程序MBean不在那里。
Everything was fine but after migration from jBoss 7.1.1.Final to 7.1.3.Final we got the problem that the MBean isn't defined at server startup. That means everything is fine if I deploy the application on an already running jboss server, but if I start the server and the application is loaded up during server start MBeans are not there.
我不知道为什么,但我感觉jBoss确保在大多数MBean之前启动/加载应用程序。我看了一眼,发现我们申请后加载了Mbeans:
I don't know why but I have the feeling that jBoss makes sure, that out application is started/loaded before most of the MBeans. I had a small look and found out that following Mbeans are loaded after our application:
- jboss.as:interface = ..
- jboss.as:socket-binding-group = ..
- jboss.as:subsystem = ..
- jboss.as:core-service=management ..(some)
- jboss.as:interface=..
- jboss.as:socket-binding-group=..
- jboss.as:subsystem=..
- jboss.as:core-service=management.. (some)
所以,
- 如何在我的应用程序之前强制jBoss加载MBean?
- 还有另一种方式/ mbean我可以获取我的信息吗?
推荐答案
我在JBOSS Wildfly 8.1中遇到了同样的问题。我用下面的代码解决了这个问题,这个代码对我有用,可以获得服务器地址和http端口:
I got the same issue in JBOSS Wildfly 8.1 . I solved the problem with the code below that worked for me to get server address and http port:
//http port
ManagementFactory.getPlatformMBeanServer().getAttribute(new ObjectName("jboss.as:socket-binding-group=standard-sockets,socket-binding=http"), "port");
//http adress
ManagementFactory.getPlatformMBeanServer().getAttribute(new ObjectName("jboss.as:interface=public"), "inet-address");
这篇关于如何在Java EE中以编程方式获取绑定的服务器地址和端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!