BlackJonnie
BlackJonnie

Reputation: 91

How to use JMX MBean for HikariCP in Spring boot application?

How to use JMX MBean for HikariCP in Spring boot application? I have a code like this:

@SpringBootApplication
public class App() { ... }

And other class:

@Configuration
public class DatabaseCfg() {
@Bean
@ManagedOperation
public DataSource ds (@Value("${hikari.proprerties}") String config) {
HikariConfig hikariConfig = new HikariConfig(config);
return new HikariDataSource(hikariConfig);
}

In Java Mission Control (or JMX Console) a saw only Datasource managed bean, not JMX MBean for HikariCP (link). Is it possible to add it too?

Upvotes: 9

Views: 16017

Answers (3)

DaveC
DaveC

Reputation: 81

In Spring Boot 2.0+ you can set the register-mbeans property in your application.properties file

spring.datasource.hikari.register-mbeans = true

If you are using an earlier version of Spring Boot you will also have to set the datasource

spring.datasource.type = com.zaxxer.hikari.HikariDataSource 

Upvotes: 8

caoxudong
caoxudong

Reputation: 373

Try this. Exclude your Hiakri DataSource Bean from being registered by Spring.

@Resource
private ObjectProvider<MBeanExporter> mBeanExporter;

@Bean("dataSource")
public DataSource createDataSource() {
    String url = hikariDataSourceConfig.getUrl();
    String username = hikariDataSourceConfig.getUsername();
    String password = hikariDataSourceConfig.getPassword();
    long idleTimeoutInMilliSeconds =
            hikariDataSourceConfig.getIdleTimeOutInMilliseconds();
    long maxLifetimeInMilliseconds =
            hikariDataSourceConfig.getMaxLifetimeInMilliseconds();
    int maximumPoolSize = hikariDataSourceConfig.getMaximumPoolSize();
    int minimumIdle = hikariDataSourceConfig.getMinimumIdle();
    String poolName = "HikariDataSource";
    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setRegisterMbeans(true);
    hikariConfig.setJdbcUrl(url);
    hikariConfig.setUsername(username);
    hikariConfig.setPassword(password);
    hikariConfig.setIdleTimeout(idleTimeoutInMilliSeconds);
    hikariConfig.setMaxLifetime(maxLifetimeInMilliseconds);
    hikariConfig.setMaximumPoolSize(maximumPoolSize);
    hikariConfig.setMinimumIdle(minimumIdle);
    hikariConfig.setPoolName(poolName);
    HikariDataSource dataSource = new HikariDataSource(hikariConfig);
    mBeanExporter
            .ifUnique((exporter) -> exporter.addExcludedBean("dataSource"));
    return dataSource;
}

Upvotes: 0

M. Rizzo
M. Rizzo

Reputation: 2261

I believe on your hikariConfig you need to set a few additional settings. You need to register the MBeans and set a pool name on the configuration.

HikariConfig hiakriConfig = new HikariConfig(config);
hikariConfig.setRegisterMbeans(true);
kikariConfig.setPoolName("my-pool-1");

Yes you obviously could drive these through the properties as well. I'm not sure if you are including these in your properties file as they are not listed. Also please note you are spelling properties wrong (@Value("${ds.proprerties}") should probably should be (@Value("${ds.properties}") but I'm not sure how you actually have named variables and property files. You may want to double check if that is where you want to set all of the properties.

Upvotes: 1

Related Questions