Hikari线程池状态监控

@Component
@EnableScheduling
@Controller
public class HikariDataTest {
        private HikariPoolMXBean poolProxy;
        @PostConstruct
        public void test() {
            new Thread(()->{
                HikariDataSource hikaridatasource = new HikariDataSource();
                hikaridatasource.setJdbcUrl("jdbc:mysql://192.168.31.15:96?characterEncoding=utf8&useTimezone=true&serverTimezone=GMT%2B8&useSSL=false");
                hikaridatasource.setUsername("root");
                hikaridatasource.setPassword("");***
                hikaridatasource.setDriverClassName("com.mysql.cj.jdbc.Driver");
                hikaridatasource.setRegisterMbeans(true);
                hikaridatasource.setPoolName("HikariConnectionPool");

                MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
                ObjectName poolName = null;
                try {
                    poolName = new ObjectName("com.zaxxer.hikari:type=Pool (" + hikaridatasource.getPoolName() + ")");
                } catch (MalformedObjectNameException e) {
                    e.printStackTrace();
                }
                poolProxy = JMX.newMXBeanProxy(mBeanServer, poolName, HikariPoolMXBean.class);

                Connection conn = null;
                try {
                    conn = hikaridatasource.getConnection();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                Statement sm = null;
                try {
                    sm = conn.createStatement();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                ResultSet rs = null;
                for (int i = 0; i < 999999999; i++) {
                    try {
                        rs = sm.executeQuery("select username from pg_security_db.sys_user  ");
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                try {
                    sm.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                hikaridatasource.close();
            }).start();
        }

        @Scheduled(cron = "0/10 * * * * *")
        public void HikariMonitor() {
            if(poolProxy == null) {
                System.out.println("Hikari not initialized,please wait...");
            }else {
                System.out.println("HikariPoolState = "
                        + "Active=[" + String.valueOf(poolProxy.getActiveConnections() + "] "
                        + "Idle=[" + String.valueOf(poolProxy.getIdleConnections() + "] "
                        + "Wait=["+poolProxy.getThreadsAwaitingConnection()+"] "
                        + "Total=["+poolProxy.getTotalConnections()+"]")));
            }

        }

}

你可能感兴趣的:(mysql,mysql)