1

是否可以在 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
        }

当应用服务器即将崩溃(PermGen 空间异常)时,它会向我们发送一封电子邮件;但是我们有 3 个应用服务器正在运行,所以域名会非常有用……知道吗?

谢谢

4

5 回答 5

1

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

于 2010-05-17T16:54:57.477 回答
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"或者null如果你想连接本地 JVM

于 2010-05-25T11:54:12.467 回答
1

这不是最漂亮的解决方案,但这个 hack 有效:

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

于 2010-05-18T10:49:39.477 回答
0

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

db.getHISTORICAL_MEMORY_USAGE().setDOMAIN( System.getProperty( "domain.name" ) );
db.getHISTORICAL_MEMORY_USAGE().setSERVER( System.getProperty( "com.sun.aas.hostName" ) );
于 2013-01-23T21:48:26.973 回答
0

基于 Kevin 的回答,如果您只想通过在 J2EEDomain 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"});
于 2012-01-30T20:41:29.633 回答