引导自动配置池时如何在 spring 引导中使用 Hikari cp 的 Jmx bean

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

开机时Hikari cpJmx bean如何在spring boot中使用autoconfigure pooling?

我尝试按照下面的说明进行操作 link。

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

Hre 是我的 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)");

我没有手动创建数据源,只是在 application.properties 中提供了属性,然后引导完成剩下的工作。

我遇到了同样的问题。

您可以从 HikariDataSource 本身获取 HikariPoolMXBean。

为了做到这一点,我将 HikariDataSource 注入到 class 我需要 HikariPoolMXBean,并调用了这个方法:

HikariPoolMXBean poolProxy = hikariDataSource.getHikariPoolMXBean();