Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 507
BlackJonnie Avatar asked Mar 29 '17 14:03

BlackJonnie


People also ask

How do I enable JMX in spring boot?

JMX is automatically enabled by default in a Spring Boot application. As a result, all of the Actuator endpoints are exposed as MBeans. And it sets us up nicely to expose any other bean in the Spring application context as an MBean.

What is the use of HikariCP in spring boot?

HikariCP is a reliable, high-performance JDBC connection pool. It is much faster, lightweight and have better performance as compare to other connection pool API. Because of all these compelling reasons, HikariCP is now the default pool implementation in Spring Boot 2.

What is JMX spring boot?

Java Management Extensions (JMX) provide a standard mechanism to monitor and manage applications. By default, Spring Boot exposes management endpoints as JMX MBeans under the org. springframework. boot domain.

What is HikariCP DataSource?

Overview. Hikari is a JDBC DataSource implementation that provides a connection pooling mechanism. Compared to other implementations, it promises to be lightweight and better performing.

Is it possible to add a JMX MBean for hikaricp?

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? Show activity on this post. Show activity on this post. 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.

What are JMX MBeans in Spring Boot?

By default, Spring Boot exposes management endpoints as JMX MBeans under the org.springframework.boot domain. Let's see how to access them via JConsole.

How to mark a bean as an MBean in Spring Boot?

To mark a bean as an MBean in Spring boot, all that is needed is to annotate the class with the @ManagedResource annotation. This will indicate to the JmxAutoConfiguration class that this is a managed bean. In order to expose specific methods to JMX interfaces, the methods should be annotated with the @ManagedOperation annotation.

Can I use Hikari connection pool with Spring Boot 2?

As a matter of fact, if you try adding com.zaxxer:HikariCP to your project, Eclipse will report that you are overriding the default implementation available in Spring Boot 2 starters: To configure Hikari Connection Pool you can use the application.properties file.


2 Answers

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 
like image 160
DaveC Avatar answered Oct 09 '22 12:10

DaveC


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.

like image 40
M. Rizzo Avatar answered Oct 09 '22 14:10

M. Rizzo