Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 views
in Technique[技术] by (71.8m points)

jakarta ee - How to get bound server address and port programmatically in Java EE?

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"));

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.

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)

So,

  • how can I force jBoss to load MBeans before my application?
  • is there another way/mbean where I can get my information?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...