import java.util.Date;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import org.jboss.mx.util.MBeanServerLocator;
public class CC {
/**
* @param args
*/
public static void main(String[] args) {
try {
ObjectName mbeanId = new ObjectName("jboss.system:type=Server");
MBeanServer server = MBeanServerLocator.locateJBoss();
Date startDate = (Date)server.getAttribute(new ObjectName("jboss.system:type=Server"), "StartDate");
Date jbossStartupTime = new Date(System.currentTimeMillis() - startDate.getTime());
} catch (MalformedObjectNameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AttributeNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstanceNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MBeanException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ReflectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
测试环境jboss 5.1下通过
参照https://developer.jboss.org/thread/79294?tstart=0
在JBoss7+中,最好不要用 org.jboss.security.util.MBeanServerLocator。
可以用下面的方法来获取MBeanServer:
javax.management.MBeanServerConnection mbeanServerConnection = java.lang.management.ManagementFactory
.getPlatformMBeanServer();