This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit 59c4ffb0d412dd2a078a40658cd1fa90f0694347 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Fri May 29 13:13:54 2020 +0200 camel-health - Make health simpler --- .../camel/health/HealthCheckConfiguration.java | 2 + .../apache/camel/health/HealthCheckRegistry.java | 10 ++++ .../impl/health/DefaultHealthCheckRegistry.java | 23 +++++++-- .../camel/main/DefaultConfigurationConfigurer.java | 14 ++++++ .../camel/main/DefaultConfigurationProperties.java | 58 ++++++++++++++++++++++ .../management/mbean/ManagedCamelHealthMBean.java | 5 +- .../camel/management/mbean/ManagedCamelHealth.java | 5 ++ 7 files changed, 112 insertions(+), 5 deletions(-) diff --git a/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckConfiguration.java b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckConfiguration.java index 8289dd6..ddb7052 100644 --- a/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckConfiguration.java +++ b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckConfiguration.java @@ -22,6 +22,8 @@ import org.apache.camel.RuntimeCamelException; import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.TimeUtils; +// TODO: Remove + public class HealthCheckConfiguration implements Cloneable { public static final Boolean DEFAULT_VALUE_ENABLED = Boolean.TRUE; public static final Duration DEFAULT_VALUE_INTERVAL = Duration.ZERO; diff --git a/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckRegistry.java b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckRegistry.java index 74f8dec..025a23a 100644 --- a/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckRegistry.java +++ b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckRegistry.java @@ -37,6 +37,16 @@ public interface HealthCheckRegistry extends CamelContextAware, StaticService { String FACTORY = "health-check-registry"; /** + * Whether Health Check is enabled globally + */ + boolean isEnabled(); + + /** + * Whether Health Check is enabled globally + */ + void setEnabled(boolean enabled); + + /** * Resolves {@link HealthCheck} or {@link HealthCheckRepository} by id. * * Will first lookup in this {@link HealthCheckRegistry} and then {@link org.apache.camel.spi.Registry}, diff --git a/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthCheckRegistry.java b/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthCheckRegistry.java index db82c2f..c09b4db 100644 --- a/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthCheckRegistry.java +++ b/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthCheckRegistry.java @@ -42,6 +42,7 @@ public class DefaultHealthCheckRegistry extends ServiceSupport implements Health private final Set<HealthCheck> checks; private final Set<HealthCheckRepository> repositories; private CamelContext camelContext; + private boolean enabled; public DefaultHealthCheckRegistry() { this(null); @@ -56,6 +57,16 @@ public class DefaultHealthCheckRegistry extends ServiceSupport implements Health } @Override + public boolean isEnabled() { + return enabled; + } + + @Override + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + @Override protected void doInit() throws Exception { super.doInit(); @@ -198,9 +209,13 @@ public class DefaultHealthCheckRegistry extends ServiceSupport implements Health @Override public Stream<HealthCheck> stream() { - return Stream.concat( - checks.stream(), - repositories.stream().flatMap(HealthCheckRepository::stream) - ).distinct(); + if (enabled) { + return Stream.concat( + checks.stream(), + repositories.stream().flatMap(HealthCheckRepository::stream) + ).distinct(); + } else { + return Stream.empty(); + } } } diff --git a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java index 9905cab..814865a 100644 --- a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java +++ b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java @@ -210,6 +210,20 @@ public final class DefaultConfigurationConfigurer { if (config.getRouteControllerRouteStartupLoggingLevel() != null) { camelContext.getRouteController().setRouteStartupLoggingLevel(config.getRouteControllerRouteStartupLoggingLevel()); } + + // health check + HealthCheckRegistry hc = camelContext.getExtension(HealthCheckRegistry.class); + if (hc != null) { + hc.setEnabled(config.isHealthCheckEnabled()); + if (config.isHealthCheckRoutesEnabled()) { + Object routes = hc.resolveById("routes"); + if (routes != null) { + hc.register(routes); + } + } else { + hc.unregister("routes"); + } + } } /** diff --git a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java index f4609e5..f94f49b 100644 --- a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java +++ b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java @@ -82,6 +82,7 @@ public abstract class DefaultConfigurationProperties<T> { private String xmlRoutes = "classpath:camel/*.xml"; private String xmlRests = "classpath:camel-rest/*.xml"; private boolean lightweight; + // route controller private LoggingLevel routeControllerRouteStartupLoggingLevel; private boolean routeControllerSuperviseEnabled; private String routeControllerIncludeRoutes; @@ -93,6 +94,9 @@ public abstract class DefaultConfigurationProperties<T> { private long routeControllerBackOffMaxElapsedTime; private long routeControllerBackOffMaxAttempts; private double routeControllerBackOffMultiplier; + // health check + private boolean healthCheckEnabled = true; + private boolean healthCheckRoutesEnabled; // getter and setters // -------------------------------------------------------------- @@ -1059,6 +1063,36 @@ public abstract class DefaultConfigurationProperties<T> { this.routeControllerBackOffMultiplier = routeControllerBackOffMultiplier; } + public boolean isHealthCheckEnabled() { + return healthCheckEnabled; + } + + /** + * Whether health check is enabled. + * Is default enabled. + */ + public void setHealthCheckEnabled(boolean healthCheckEnabled) { + this.healthCheckEnabled = healthCheckEnabled; + } + + public boolean isHealthCheckRoutesEnabled() { + return healthCheckRoutesEnabled; + } + + /** + * Whether routes should be included in the health check. + * + * By default only the camel context is part of the health check. + * Enabling routes allows to let routes that are controlled by the routes controller + * take part of the health check, so if there is problems starting up routes, then + * they can influence the health check and report its UP or DOWN. + * + * Is default disabled. + */ + public void setHealthCheckRoutesEnabled(boolean healthCheckRoutesEnabled) { + this.healthCheckRoutesEnabled = healthCheckRoutesEnabled; + } + // fluent builders // -------------------------------------------------------------- @@ -1796,4 +1830,28 @@ public abstract class DefaultConfigurationProperties<T> { return (T) this; } + /** + * Whether health check is enabled. + * Is default enabled. + */ + public T healthCheckEnabled(boolean healthCheckEnabled) { + this.healthCheckEnabled = healthCheckEnabled; + return (T) this; + } + + /** + * Whether routes should be included in the health check. + * + * By default only the camel context is part of the health check. + * Enabling routes allows to let routes that are controlled by the routes controller + * take part of the health check, so if there is problems starting up routes, then + * they can influence the health check and report its UP or DOWN. + * + * Is default disabled. + */ + public T healthCheckRoutesEnabled(boolean healthCheckRoutesEnabled) { + this.healthCheckRoutesEnabled = healthCheckRoutesEnabled; + return (T) this; + } + } diff --git a/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedCamelHealthMBean.java b/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedCamelHealthMBean.java index 57944d7..ced4a00 100644 --- a/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedCamelHealthMBean.java +++ b/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedCamelHealthMBean.java @@ -24,7 +24,10 @@ import org.apache.camel.api.management.ManagedAttribute; import org.apache.camel.api.management.ManagedOperation; public interface ManagedCamelHealthMBean { - + + @ManagedAttribute(description = "Whether Health Check is enabled globally") + boolean isEnabled(); + @ManagedAttribute(description = "Application Health") boolean isHealthy(); diff --git a/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedCamelHealth.java b/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedCamelHealth.java index 3a010cf..922bcbe 100644 --- a/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedCamelHealth.java +++ b/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedCamelHealth.java @@ -53,6 +53,11 @@ public class ManagedCamelHealth implements ManagedCamelHealthMBean { } @Override + public boolean isEnabled() { + return healthCheckRegistry.isEnabled(); + } + + @Override public boolean isHealthy() { for (HealthCheck.Result result : HealthCheckHelper.invoke(context)) { if (result.getState() == HealthCheck.State.DOWN) {