获取 Glassfish2 域的名称

发布于 2024-09-01 11:48:47 字数 583 浏览 11 评论 0原文

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 个应用程序服务器,因此域名非常有用......知道吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一页 2024-09-08 11:48:47

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

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

柠檬色的秋千 2024-09-08 11:48:47

这不是最漂亮的解决方案,但这个 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

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

云雾 2024-09-08 11:48:47

你可以这样做:

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

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

刘备忘录 2024-09-08 11:48:47

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

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.

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"});
羁拥 2024-09-08 11:48:47

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

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

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" ) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文