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

3

8

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?

Looper answered 29/3, 2017 at 14:52 Comment(1)
Don't do anything. You are trying to out-smart Spring Boot, instead work with the framework. Just let Spring Boot configure the datasource (it will do this automatically just add the appropriate dependency and settings in application.properties.Permatron
E
7

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 
Erich answered 20/1, 2020 at 17:19 Comment(0)
B
1

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.

Buckels answered 29/3, 2017 at 15:45 Comment(1)
Thanx! But it's not working. ds.properties is renamed to hikari.properties so as not to mislead. All the properties are read from this file without problems. As for the code hikariConfig.setRegisterMbeans(true); , when you start the application throws this bean already registered, because JMX is enabled in Spring boot by default (@SpringBootApplication annotation).Looper
K
0

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;
}
Kamerad answered 4/11, 2019 at 12:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.