|
| 1 | +package org.apache.camel.spring.boot.actuate.health; |
| 2 | + |
| 3 | +import org.apache.camel.spring.boot.actuate.health.liveness.CamelLivenessStateHealthIndicator; |
| 4 | +import org.apache.camel.spring.boot.actuate.health.readiness.CamelReadinessStateHealthIndicator; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | +import org.springframework.beans.factory.InitializingBean; |
| 8 | +import org.springframework.boot.actuate.health.Health; |
| 9 | +import org.springframework.boot.actuate.health.HealthContributorRegistry; |
| 10 | +import org.springframework.boot.actuate.health.HealthIndicator; |
| 11 | +import org.springframework.boot.actuate.health.NamedContributor; |
| 12 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 13 | +import org.springframework.context.annotation.Configuration; |
| 14 | +import org.springframework.scheduling.TaskScheduler; |
| 15 | +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; |
| 16 | + |
| 17 | +import java.time.Duration; |
| 18 | +import java.time.ZonedDateTime; |
| 19 | + |
| 20 | +/** |
| 21 | + * Configuration class that replace synchronous Camel Health Checks with asynchronous ones. |
| 22 | + * |
| 23 | + * This implementation is based on https://github.com/spring-projects/spring-boot/issues/2652 that most probably |
| 24 | + * will be added in spring boot 3.2.x as a new feature in the future. |
| 25 | + * |
| 26 | + * TODO: To be refactored once async health contributors feature will be added in spring boot. |
| 27 | + * |
| 28 | + */ |
| 29 | +@Configuration |
| 30 | +@ConditionalOnProperty(prefix = "camel.health", name = "async-camel-health-check", havingValue = "true") |
| 31 | +public class AsyncHealthIndicatorAutoConfiguration implements InitializingBean { |
| 32 | + private static final Logger log = LoggerFactory.getLogger(AsyncHealthIndicatorAutoConfiguration.class); |
| 33 | + |
| 34 | + private HealthContributorRegistry healthContributorRegistry; |
| 35 | + private TaskScheduler taskScheduler; |
| 36 | + private CamelHealthCheckConfigurationProperties config; |
| 37 | + |
| 38 | + public AsyncHealthIndicatorAutoConfiguration(HealthContributorRegistry healthContributorRegistry, |
| 39 | + CamelHealthCheckConfigurationProperties config) { |
| 40 | + this.healthContributorRegistry = healthContributorRegistry; |
| 41 | + this.config = config; |
| 42 | + |
| 43 | + ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler(); |
| 44 | + threadPoolTaskScheduler.setPoolSize(config.getHealthCheckPoolSize()); |
| 45 | + threadPoolTaskScheduler.setThreadNamePrefix(config.getHealthCheckThreadNamePrefix()); |
| 46 | + threadPoolTaskScheduler.initialize(); |
| 47 | + taskScheduler = threadPoolTaskScheduler; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void afterPropertiesSet() throws Exception { |
| 52 | + for (NamedContributor<?> namedContributor : healthContributorRegistry) { |
| 53 | + final String name = namedContributor.getName(); |
| 54 | + final Object contributor = namedContributor.getContributor(); |
| 55 | + if (contributor instanceof CamelHealthCheckIndicator |
| 56 | + || contributor instanceof CamelLivenessStateHealthIndicator |
| 57 | + || contributor instanceof CamelReadinessStateHealthIndicator) { |
| 58 | + HealthIndicator camelHealthCheckIndicator = (HealthIndicator) contributor; |
| 59 | + healthContributorRegistry.unregisterContributor(name); |
| 60 | + log.debug( |
| 61 | + "Wrapping " + contributor.getClass().getSimpleName() + " for async health scheduling"); |
| 62 | + WrappedHealthIndicator wrappedHealthIndicator = |
| 63 | + new WrappedHealthIndicator(camelHealthCheckIndicator); |
| 64 | + healthContributorRegistry.registerContributor(name, wrappedHealthIndicator); |
| 65 | + taskScheduler.scheduleWithFixedDelay( |
| 66 | + wrappedHealthIndicator, Duration.ofSeconds(config.getHealthCheckFrequency())); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Health Check Indicator that executes Health Checks within a Task Scheduler |
| 73 | + */ |
| 74 | + private static class WrappedHealthIndicator implements HealthIndicator, Runnable { |
| 75 | + private static final String LAST_CHECKED_KEY = "lastChecked"; |
| 76 | + private static final String LAST_DURATION_KEY = "lastDuration"; |
| 77 | + |
| 78 | + private HealthIndicator wrappedHealthIndicator; |
| 79 | + |
| 80 | + private Health lastHealth; |
| 81 | + |
| 82 | + public WrappedHealthIndicator(HealthIndicator wrappedHealthIndicator) { |
| 83 | + this.wrappedHealthIndicator = wrappedHealthIndicator; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Health health() { |
| 88 | + Health lastHealth = getLastHealth(); |
| 89 | + if (lastHealth == null) { |
| 90 | + setLastHealth(getAndWrapHealth()); |
| 91 | + lastHealth = getLastHealth(); |
| 92 | + } |
| 93 | + |
| 94 | + return lastHealth; |
| 95 | + } |
| 96 | + |
| 97 | + private Health getAndWrapHealth() { |
| 98 | + ZonedDateTime startTime = ZonedDateTime.now(); |
| 99 | + Health baseHealth = getWrappedHealthIndicator().health(); |
| 100 | + ZonedDateTime endTime = ZonedDateTime.now(); |
| 101 | + Duration duration = Duration.between(startTime, endTime); |
| 102 | + return Health.status(baseHealth.getStatus()) |
| 103 | + .withDetails(baseHealth.getDetails()) |
| 104 | + .withDetail(LAST_CHECKED_KEY, startTime) |
| 105 | + .withDetail(LAST_DURATION_KEY, duration) |
| 106 | + .build(); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void run() { |
| 111 | + setLastHealth(getAndWrapHealth()); |
| 112 | + } |
| 113 | + |
| 114 | + public HealthIndicator getWrappedHealthIndicator() { |
| 115 | + return wrappedHealthIndicator; |
| 116 | + } |
| 117 | + |
| 118 | + public Health getLastHealth() { |
| 119 | + return lastHealth; |
| 120 | + } |
| 121 | + |
| 122 | + public void setLastHealth(Health lastHealth) { |
| 123 | + this.lastHealth = lastHealth; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments