Ottenere il nome di un dominio Glassfish2
Domanda
è possibile ottenere il nome del dominio corrente in v2 Glassfish?
Ho un codice come:
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
}
che ci avrebbe inviare una e-mail quando l'appserver sta per incidente (PermGen spazio eccezione); ma abbiamo 3 appserver in esecuzione, in modo che il nome di dominio sarebbe davvero utile ... qualche idea?
Grazie
Soluzione 3
si può fare in questo modo:
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"));
con hostname
equivale a "host:port"
, o null
se si desidera collegare la JVM locale
Altri suggerimenti
È possibile utilizzare AMX per ottenere il nome di dominio. Si tratta di una disposizione da domainroot .getAppserverDomainName ().
non è la soluzione più bella, ma questo hack opere:
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 );
}
a condizione che la directory di lavoro è ... / domini / dominio / config
Sulla risposta di Kevin, se si desidera utilizzare JMX (senza fare affidamento su librerie AMX) semplicemente utilizzando il metodo GetPropertyValue sulle MBean J2EEDomain.
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"});
Ecco un modo davvero semplice che abbiamo usato
db.getHISTORICAL_MEMORY_USAGE().setDOMAIN( System.getProperty( "domain.name" ) );
db.getHISTORICAL_MEMORY_USAGE().setSERVER( System.getProperty( "com.sun.aas.hostName" ) );