Peter Penzov
Peter Penzov

Reputation: 1670

Monitor application server with JMX

I found this simple example of JBoss monitoring with JMX and Java

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();
    }
}

I want to call this code every 3 seconds to get live performance data.

Is there a way to open one connection to the server and send frequent requests?

Upvotes: 0

Views: 165

Answers (1)

jpkroehling
jpkroehling

Reputation: 14061

If you deploy this code as an EJB, you can make it a @Singleton @Startup, with the connection being set at a @PostConstruct method, while the metrics are gathered periodically, according to a @Schedule. For instance:

@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
    }
}

Upvotes: 0

Related Questions