Question

is it possible to get the name of the current domain in Glassfish v2?

I've got a code like:

    MemoryMXBean bean = ManagementFactory.getMemoryMXBean();
    if (bean != null) {
        MemoryUsage usage = bean.getNonHeapMemoryUsage();
        int current = (int) ((double) usage.getUsed() / usage.getMax() * 100);
        ch.log( Level.INFO, "Memory usage : (non heap) " + usage + " -- "+current+"% (limit:"+THRESHOLD+"%)");
        if (current > THRESHOLD) {
            //send email
        }

which would send us an email when the appserver is about to crash (PermGen space exception); but we've got 3 appserver running, so the domain name would be really usefull ... any idea?

Thanks

Was it helpful?

Solution 3

you can do it this way:

MBeanServerConnection serv;
if (hostname != null) {
    //remote connection
    JMXServiceURL u = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + hostname + "/jmxrmi");
    String[] credentials = new String[]{"admin", "adminadmin"};
    Map<String, String[]> environment = new LinkedHashMap<String, String[]>();
    environment.put("jmx.remote.credentials", credentials);

    JMXConnector c = JMXConnectorFactory.connect(u, environment);
    serv = c.getMBeanServerConnection();
} else {
    //local connection
    serv = ManagementFactory.getPlatformMBeanServer();
}

//connect to AMX
DomainRoot dRoot = ProxyFactory.getInstance(serv).getDomainRoot(true) ;

//get the Administrative domain name in the Domain Configuration
Map<String, String> map = dRoot.getDomainConfig().getProperties() ;
System.out.println(map.get("administrative.domain.name"));

with hostname equals to "host:port", or null if you want to connect the the local JVM

OTHER TIPS

You can use AMX to get the domain name. It is a available from DomainRoot.getAppserverDomainName().

it's not the most beautifull solution, but this hack works:

String dirName = (new File( ".." )).getCanonicalPath();
int dirNameIdx = dirName.lastIndexOf( File.separator );
if (dirNameIdx != -1) 
    APPSERVER = dirName.substring( dirNameIdx + 1 );
    ch.log( Level.INFO, "Memory usage: Appserver name: " + APPSERVER );
}

provided that the working directory is .../domains/domain/config

Building on Kevin's answer, if you just want to use JMX (without relying on AMX libraries) just by using the getPropertyValue method on the J2EEDomain mbean.

MBeanServerConnection serv;

//remote connection
JMXServiceURL u = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + hostname + "/jmxrmi");
String[] credentials = new String[]{"admin", "adminadmin"};
Map<String, String[]> environment = new LinkedHashMap<String, String[]>();
environment.put("jmx.remote.credentials", credentials);

JMXConnector c = JMXConnectorFactory.connect(u, environment);
serv = c.getMBeanServerConnection();
String domainName = (String) mbsc.invoke(new ObjectName("com.sun.appserv:j2eeType=J2EEDomain,name=com.sun.appserv,category=runtime"), "getPropertyValue", new String[] {"administrative.domain.name"}, new Object[] {"java.lang.String"});

Here's a really simple way that we've been using

db.getHISTORICAL_MEMORY_USAGE().setDOMAIN( System.getProperty( "domain.name" ) );
db.getHISTORICAL_MEMORY_USAGE().setSERVER( System.getProperty( "com.sun.aas.hostName" ) );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top