获取Glassfish2域的名称

时间:2010-05-17 15:05:10

标签: java glassfish jmx esb jcaps

是否可以在Glassfish v2中获取当前域的名称?

我有一个代码:

    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
        }

当appserver即将崩溃时会向我们发送一封电子邮件(PermGen空间异常);但我们有3个appserver运行,所以域名真的很有用......任何想法?

由于

5 个答案:

答案 0 :(得分:1)

您可以使用AMX获取域名。它可以从DomainRoot。getAppserverDomainName()。

获得

答案 1 :(得分:1)

这不是最美丽的解决方案,但这个黑客有效:

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 );
}

如果工作目录是...... / domains / domain / config

答案 2 :(得分:1)

你可以这样做:

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"));

hostname等于"host:port",如果要连接本地JVM,则为null

答案 3 :(得分:0)

基于Kevin的回答,如果你只是想在J2Emmain mbean上使用getPropertyValue方法来使用JMX(不依赖于AMX库)。

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"});

答案 4 :(得分:0)

这是我们一直在使用的一种非常简单的方式

db.getHISTORICAL_MEMORY_USAGE().setDOMAIN( System.getProperty( "domain.name" ) );
db.getHISTORICAL_MEMORY_USAGE().setSERVER( System.getProperty( "com.sun.aas.hostName" ) );