如何在 Java EE 中以编程方式获取绑定服务器地址和端口? [英] How to get bound server address and port programmatically in Java EE?

查看:15
本文介绍了如何在 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服务器上部署应用程序,一切都很好,但是如果我启动服务器并且应用程序在服务器启动期间加载,MBeans不存在.

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 之前启动/加载应用程序.我看了一眼,发现在我们的应用程序之后加载了以下 Mbean:

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..(一些)

所以,

  • 如何强制 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屋!

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