Our JBoss server.xml file has this line:

 <Engine name="jboss.web" defaultHost="localhost" jvmRoute="app_server_01">

Can there be any method of getting the jvmroute value (within this situation app_server_01) at runtime using Java?

Background

We have got session affinity (sticky periods) set up between our application servers and Apache servers. JBoss appends the jvmroute (application_server_01) towards the JSESSIONID. We've multiple applications set up on one host, but running on different application servers. You want to append the right jvmroute towards the JSESSIONID utilizing a servlet filter.

JMX version

You are able to query the Engine's MBean to obtain these details:

  MBeanServer server = org.jboss.mx.util.MBeanServerLocator.locateJBoss();
  String jvmRoute = (String)server.getAttribute(new ObjectName("jboss.web:type=Engine"), "jvmRoute");

XML Parsing version

An alternative choice is by using the "jboss.server.home.dir" system property to obtain the full path from the server.xml file, after which just open it up utilizing an XML parser.

For instance, in JBoss 4.2.2, the server.xml file reaches:

System.getProperty("jboss.server.home.dir") + File.separator +
  "deploy" + File.separator +
  "jboss-web.deployer" + File.separator +
  "server.xml"

You are able to load this file into whatever XML parser you want.

In JBoss 5.1., your directory structure has transformed, so it might be:

// Incompatible with JBoss 4.x:
System.getProperty("jboss.server.home.dir") + File.separator +
  "deploy" + File.separator +
  "jbossweb.sar" + File.separator +
  "server.xml"