How to obtain JBoss Server Group Name programatica

2020-07-31 05:46发布

How can I obtain the jboss eap 6.x / wildfly server group name programmatically from within a java enterprise application deployed to a domain?

Some of the other jboss values such as node name can be obtained via system property values but server-group-name does not appear to be exposed this way.

I would like to be able to show the server group name (i.e. the cluster name) in a system diagnostics function for technical users to verify they are looking at the correct system... (npe, dev, prod etc)

2条回答
Animai°情兽
2楼-- · 2020-07-31 06:37

One other (and maybe better) way to do it is to use the management API https://docs.jboss.org/author/display/WFLY8/Management+API+reference .

查看更多
Bombasti
3楼-- · 2020-07-31 06:37

I've discovered that I need to use JMX to obtain this value as its exposed via an MBean to the JVM (verifyable via JConsole....)

So answering my own question:

try {
    ObjectName serverMBean = new ObjectName("jboss.as:management-root=server");
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    String serverGroupName = (String) ManagementFactory.getPlatformMBeanServer().getAttribute(serverMBean, "serverGroup");
    logger.info("JBoss server group name is" + serverGroupName);
} catch (Exception e) {
    logger.error("Unable to identify JBoss server-group-name", e);
}

If the application might also be deployed to a standalone server one could query the launchType attribute first. Valid values appear to be STANDALONE or DOMAIN.

In STANDALONE mode the serverGroup attribute is not available however one could use the jboss.node.name system property as an alternative system identifier.

查看更多
登录 后发表回答