Class HikariHealthCheck

  • All Implemented Interfaces:
    HealthCheck

    public abstract class HikariHealthCheck
    extends Object
    implements HealthCheck
    Reports on the health of HikariCP JDBC connection pool.
    
     import com.zaxxer.hikari.pool.HikariPool;
     import ratpack.guice.Guice;
     import ratpack.core.health.HealthCheckHandler;
     import ratpack.hikari.HikariHealthCheck;
     import ratpack.hikari.HikariModule;
     import ratpack.test.embed.EmbeddedApp;
    
     import javax.inject.Inject;
    
     import static org.junit.jupiter.api.Assertions.assertEquals;
    
     public class Example {
    
       public static void main(String... args) throws Exception {
         EmbeddedApp.of(s -> s
           .registry(Guice.registry(b -> b
             .module(HikariModule.class, hikariConfig -> {
               hikariConfig.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource");
               hikariConfig.addDataSourceProperty("URL", "jdbc:h2:mem:dev"); // Use H2 in memory database
             })
    
             .bind(MyHealthCheck.class)
           ))
           .handlers(chain -> chain
             .get("health-checks", new HealthCheckHandler())
           )
         ).test(httpClient -> assertEquals("my-health-check : HEALTHY", httpClient.getText("health-checks")));
       }
    
       static class MyHealthCheck extends HikariHealthCheck {
    
         private HikariPool hikariPool;
    
         @Inject
         MyHealthCheck(HikariPool hikariPool) {
           this.hikariPool = hikariPool;
         }
    
         @Override
         public String getName() {
           return "my-health-check";
         }
    
         @Override
         public HikariPool getHikariPool() {
           return hikariPool;
         }
       }
     }
     
    • Constructor Detail

      • HikariHealthCheck

        public HikariHealthCheck()
    • Method Detail

      • getName

        public String getName()
        Description copied from interface: HealthCheck
        The unique name of the health check.

        Each health check within an application must have a unique name.

        Specified by:
        getName in interface HealthCheck
        Returns:
        the name of the health check
      • getTimeout

        public Duration getTimeout()
      • getHikariPool

        public abstract com.zaxxer.hikari.pool.HikariPool getHikariPool()
      • check

        public Promise<HealthCheck.Result> check​(Registry registry)
        Description copied from interface: HealthCheck
        Checks the health of the component, providing a promise for the result.

        This method returns a promise to allow check implementations to be asynchronous. If the implementation does not need to be asynchronous, the result can be returned via Promise.value(Object).

        The registry argument is the server registry, from which other supporting objects can be obtained.

        If this method throws an exception, it is logically equivalent to returned an unhealthy result with the thrown exception.

        If the method returns a failed promise, it will be converted to a result using HealthCheck.Result.unhealthy(Throwable).

        Specified by:
        check in interface HealthCheck
        Parameters:
        registry - the server registry
        Returns:
        a promise for the result