[英]Get the name of a Glassfish2 domain
is it possible to get the name of the current domain in Glassfish v2? 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); 当应用服务器将要崩溃时,这将向我们发送电子邮件(PermGen空间异常); but we've got 3 appserver running, so the domain name would be really usefull ... any idea? 但是我们已经运行了3个appserver,所以域名真的很有用……知道吗?
Thanks 谢谢
You can use AMX to get the domain name. 您可以使用AMX来获取域名。 It is a available from DomainRoot .getAppserverDomainName(). 它可以从DomainRoot .getAppserverDomainName()获得。
it's not the most beautifull solution, but this hack works: 它不是最美丽的解决方案,但是此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 );
}
provided that the working directory is .../domains/domain/config 假设工作目录为... / domains / domain / config
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 hostname
等于"host:port"
,如果要连接本地JVM,则返回null
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" ) );
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. 如果只想通过在J2EEDomain mbean上使用getPropertyValue方法来使用JMX(不依赖AMX库),则可以基于Kevin的答案。
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"});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.