Interface HealthCheck

  • All Known Implementing Classes:
    HikariHealthCheck

    public interface HealthCheck
    Reports on the health of some aspect of the system.

    Health checks are typically used for reporting and monitoring purposes. The results exposed by health checks can be reported via HTTP by a HealthCheckHandler.

    The actual check is implemented by the check(Registry) method, that returns a promise for a HealthCheck.Result.

    The inspiration for health checks in Ratpack comes from the Dropwizard Metrics library. Ratpack health checks are different in that they support non blocking checks, and all health checks require a unique name.

    See Also:
    HealthCheckHandler
    • Method Detail

      • getName

        String getName()
        The unique name of the health check.

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

        Returns:
        the name of the health check
      • check

        Promise<HealthCheck.Result> check​(Registry registry)
                                   throws Exception
        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).

        Parameters:
        registry - the server registry
        Returns:
        a promise for the result
        Throws:
        Exception - any
      • of

        static HealthCheck of​(String name,
                              Function<? super Registry,​? extends Promise<HealthCheck.Result>> func)
        Convenience factory for health check implementations.
        
        import ...
        
         
        
         ratpacktestexecExecHarness
         ratpackexecPromise
         ratpackexecregistryRegistry
         ratpackcorehealthHealthCheck
          orgjunitjupiterapiAssertions
        
        public class Example {
         public static void main(String... args) throws Exception {
           HealthCheck.Result result = ExecHarness.yieldSingle(e ->
             HealthCheck.of("test", registry ->
               Promise.value(HealthCheck.Result.healthy())
             ).check(Registry.empty())
           ).getValue();
        
           assertTrue(result.isHealthy());
         }
        }
        
        Parameters:
        name - a name of health check
        func - a health check implementation
        Returns:
        a named health check implementation
      • checkAll

        static Promise<HealthCheckResults> checkAll​(Registry registry,
                                                    Iterable<? extends HealthCheck> healthChecks)
        Execute health checks.

        The checks will be executed in parallel.

        Parameters:
        registry - the registry to pass to each health check
        healthChecks - the health checks to execute
        Returns:
        the results
        Since:
        1.5
      • checkAll

        static Promise<HealthCheckResults> checkAll​(Registry registry,
                                                    Throttle throttle,
                                                    Iterable<? extends HealthCheck> healthChecks)
        Execute health checks.

        The checks will be executed in parallel.

        Parameters:
        registry - the registry to pass to each health check
        throttle - the throttle to use to constrain the parallelism of the checks
        healthChecks - the health checks to execute
        Returns:
        the results
        Since:
        1.5