Search in sources :

Example 1 with HikariPoolMXBean

use of com.zaxxer.hikari.HikariPoolMXBean in project HikariCP by brettwooldridge.

the class TestMBean method testMBeanReporting.

@Test
public void testMBeanReporting() throws SQLException, InterruptedException, MalformedObjectNameException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(3);
    config.setMaximumPoolSize(5);
    config.setRegisterMbeans(true);
    config.setConnectionTimeout(2800);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "100");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        getUnsealedConfig(ds).setIdleTimeout(3000);
        TimeUnit.SECONDS.sleep(1);
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (testMBeanReporting)");
        HikariPoolMXBean hikariPoolMXBean = JMX.newMXBeanProxy(mBeanServer, poolName, HikariPoolMXBean.class);
        assertEquals(0, hikariPoolMXBean.getActiveConnections());
        assertEquals(3, hikariPoolMXBean.getIdleConnections());
        try (Connection ignored = ds.getConnection()) {
            assertEquals(1, hikariPoolMXBean.getActiveConnections());
            TimeUnit.SECONDS.sleep(1);
            assertEquals(3, hikariPoolMXBean.getIdleConnections());
            assertEquals(4, hikariPoolMXBean.getTotalConnections());
        }
        TimeUnit.SECONDS.sleep(2);
        assertEquals(0, hikariPoolMXBean.getActiveConnections());
        assertEquals(3, hikariPoolMXBean.getIdleConnections());
        assertEquals(3, hikariPoolMXBean.getTotalConnections());
    } finally {
        System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs");
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariPoolMXBean(com.zaxxer.hikari.HikariPoolMXBean) Connection(java.sql.Connection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Aggregations

HikariConfig (com.zaxxer.hikari.HikariConfig)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 HikariPoolMXBean (com.zaxxer.hikari.HikariPoolMXBean)1 TestElf.newHikariConfig (com.zaxxer.hikari.pool.TestElf.newHikariConfig)1 Connection (java.sql.Connection)1 MBeanServer (javax.management.MBeanServer)1 ObjectName (javax.management.ObjectName)1 Test (org.junit.Test)1