views:

42

answers:

3

Hello,

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

A: 

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

vkraemer
there is an example at http://docs.sun.com/app/docs/doc/820-4336/beapq?l=en but it seems that DomainRoot is meant to be used from outside of the appserver (RMI connection) ... and in my JCAPS environment, I would think that I can't use appserver-specific libraries ... Thank you anyway
Kevin
yes, you're certainly right, but how can I know the port number I'm supposed to connect to? it's the chicken and the egg! :)
Kevin
A: 

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

Kevin
A: 

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

Kevin