@Override
    public void configure(final LifecycleEnvironment lifecycle, final ServletEnvironment servlets,
                          final JerseyEnvironment jersey, final HealthEnvironment health, final ObjectMapper mapper,
                          final String name) {
        if (!isEnabled()) {
            LOGGER.info("Health check configuration is disabled.");
            return;
        }

        final MetricRegistry metrics = lifecycle.getMetricRegistry();
        final HealthCheckRegistry envHealthChecks = health.healthChecks();

        final String fullName = DEFAULT_BASE_NAME + "-" + name;
        final List<HealthCheckConfiguration> healthCheckConfigs = getHealthCheckConfigurations();

        // setup schedules for configured health checks
        final ScheduledExecutorService scheduledHealthCheckExecutor = createScheduledExecutorForHealthChecks(
                healthCheckConfigs.size(), metrics, lifecycle, fullName);
        final HealthCheckScheduler scheduler = new HealthCheckScheduler(scheduledHealthCheckExecutor);
        // configure health manager to receive registered health state listeners from HealthEnvironment (via reference)
        final HealthCheckManager healthCheckManager = new HealthCheckManager(healthCheckConfigs, scheduler, metrics,
                shutdownWaitPeriod, initialOverallState, health.healthStateListeners());
        healthCheckManager.initializeAppHealth();

        // setup response provider and responder to respond to health check requests
        final HealthResponseProvider responseProvider = healthResponseProviderFactory.build(healthCheckManager,
                healthCheckManager, mapper);
        healthResponderFactory.configure(fullName, healthCheckUrlPaths, responseProvider, health, jersey, servlets,
                mapper);

        // register listener for HealthCheckRegistry and setup validator to ensure correct config
        envHealthChecks.addListener(healthCheckManager);
        lifecycle.manage(new HealthCheckConfigValidator(healthCheckConfigs, envHealthChecks));

        // register shutdown handler with Jetty
        final Duration shutdownDelay = getShutdownWaitPeriod();
        if (isDelayedShutdownHandlerEnabled() && shutdownDelay.toMilliseconds() > 0) {
            final DelayedShutdownHandler shutdownHandler = new DelayedShutdownHandler(healthCheckManager);
            shutdownHandler.register();
            LOGGER.debug("Set up delayed shutdown with delay: {}", shutdownDelay);
        }

        // Set the health state aggregator on the HealthEnvironment
        health.setHealthStateAggregator(healthCheckManager);

        LOGGER.debug("Configured ongoing health check monitoring for healthChecks: {}", getHealthCheckConfigurations());
    }
@Test
    void configure() throws Exception {
        final DefaultHealthFactory healthFactory = configFactory.build(new ResourceConfigurationSourceProvider(), "/yml/health.yml");

        LifecycleEnvironment lifecycleEnvironment = new LifecycleEnvironment(new MetricRegistry());

        ServletRegistration.Dynamic mockServletRegistration = mock(ServletRegistration.Dynamic.class);
        ServletEnvironment servletEnvironment = mock(ServletEnvironment.class);
        ArgumentCaptor<Servlet> servletCaptor = ArgumentCaptor.forClass(Servlet.class);
        when(servletEnvironment.addServlet(eq("health-check-test-servlet"), any(Servlet.class)))
            .thenReturn(mockServletRegistration);

        HealthEnvironment healthEnvironment = new HealthEnvironment(mock(HealthCheckRegistry.class));

        healthFactory.configure(
            lifecycleEnvironment,
            servletEnvironment,
            mock(JerseyEnvironment.class),
            healthEnvironment,
            new ObjectMapper(),
            "test");

        assertThat(lifecycleEnvironment.getManagedObjects())
            .hasSize(2)
            .allSatisfy(obj -> assertThat(obj).isInstanceOf(JettyManaged.class))
            .map(managed -> ((JettyManaged)managed).getManaged())
            .satisfies(obj -> assertThat(obj).element(0).isInstanceOfSatisfying(ExecutorServiceManager.class, executorServiceManager ->
                assertThat(executorServiceManager.getPoolName()).isEqualTo("health-check-test-scheduled-executor")))
            .satisfies(obj -> assertThat(obj).element(1).isInstanceOf(HealthCheckConfigValidator.class));

        assertThat(healthFactory.getHealthResponderFactory())
            .isInstanceOf(ServletHealthResponderFactory.class);

        verify(servletEnvironment).addServlet(eq("health-check-test-servlet"), servletCaptor.capture());
        assertThat(servletCaptor.getValue()).isInstanceOf(ServletHealthResponder.class);

        verify(mockServletRegistration).addMapping("/health-check");

        assertThat(healthEnvironment.healthStateAggregator()).isNotNull();
    }