Search code examples
spring-bootjmxhikaricp

How Jmx bean of Hikari cp can be used in spring boot when boot autoconfigure pooling


How Jmx bean of Hikari cp can be used in spring boot when boot autoconfigure pooling?

i tried following this instruction in below link.

https://github.com/brettwooldridge/HikariCP/wiki/MBean-(JMX)-Monitoring-and-Management.

Hre is my mbean class

import javax.management.JMX;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;


    @ManagedResource(
            objectName="PD:category=MBeans,name=testBean",
            description="Managed Bean")
    @Component("testMbean")
    public class HikariJmx {

        private String message = "Simple Message";

        private int size=0;
        public HikariJmx(){
            System.out.println("......TestMbean........");
        }

        @ManagedOperation
        public void resetMessageViaMBean(){
            this.message = "Message RESET";
        }

    @ManagedAttribute
    public int getSize() throws Exception{
            try {
                MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
                ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (HikariPool-1)");
                HikariPoolMXBean poolProxy = JMX.newMXBeanProxy(mBeanServer, poolName, HikariPoolMXBean.class);
                return poolProxy.getIdleConnections();
            }catch(Exception e){e.printStackTrace();}

            return 0;
        }
    }

Iam able to see the bean in visual vm. But when i take the attribute SIZE i am getting instanceNotFound exception in below line.

ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (HikariPool-1)");

I am not creating datasource manually, just gave the properties in the application.properties and boot do the rest.


Solution

  • I had the same problem.

    You can get the HikariPoolMXBean from the HikariDataSource itself.

    In order to do that, I injected the HikariDataSource to the class I needed the HikariPoolMXBean, and called this method :

    HikariPoolMXBean poolProxy = hikariDataSource.getHikariPoolMXBean();