Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JMX监视应用程序服务器_Java_Jmx_Jmx Ws - Fatal编程技术网

Java 使用JMX监视应用程序服务器

Java 使用JMX监视应用程序服务器,java,jmx,jmx-ws,Java,Jmx,Jmx Ws,我发现了这个使用JMX和Java进行JBoss监控的简单示例 public class JMXExample { public static void main(String[] args) throws Exception { //Get a connection to the JBoss AS MBean server on localhost String host = "localhost"; int port = 9999;

我发现了这个使用JMX和Java进行JBoss监控的简单示例

public class JMXExample {

    public static void main(String[] args) throws Exception {
        //Get a connection to the JBoss AS MBean server on localhost
        String host = "localhost";
        int port = 9999;  // management-native port
        String urlString =
            System.getProperty("jmx.service.url","service:jmx:remoting-jmx://" + host + ":" + port);
        JMXServiceURL serviceURL = new JMXServiceURL(urlString);
        JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceURL, null);
        MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();

        //Invoke on the JBoss AS MBean server
        int count = connection.getMBeanCount();
        System.out.println(count);
        jmxConnector.close();
    }
}
我想每3秒钟调用一次此代码以获取实时性能数据


有没有办法打开一个到服务器的连接并发送频繁的请求?

如果将此代码部署为EJB,则可以将其设置为
@Singleton@Startup
,连接设置为
@PostConstruct
方法,同时根据
@计划定期收集度量值。例如:

@Singleton
@Startup
public class MetricsGathering {
    @PostConstruct
    public void init() {
        // setup the connection
    }

    @Schedule(second="*/5")
    public void collect() {
        // get the data and do something with it
    }
}

我使用Java独立应用程序中的代码。你能给我看一些代码示例吗?我如何建立一个连接并多次重复使用它?如果你的独立应用程序是全天候运行的,你可以使连接成为静态字段的一部分,或者使用单例模式。如果独立应用程序启动、运行此代码并关闭,则无法保持连接。