0

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?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

1 Answers1

0

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
    }
}
jpkroehling
  • 13,881
  • 1
  • 37
  • 39
  • I use the code in Java standalone application. Can you show me some code example how I can make one connection and reuse it many times? – Peter Penzov Dec 14 '15 at 18:01
  • If your standalone application is running 24x7, you can either make the connection part of a static field, or use a Singleton pattern. If your standalone application starts, run this code, and shuts down, there's no possibility to keep the connection. – jpkroehling Dec 15 '15 at 07:48