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-spring-boot.git

commit f5390618faca3259e69de4e42bedb2d52f567203
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Mon Jun 8 12:58:08 2020 +0200

    came-health - Spring Boot. WIP
---
 .../src/main/docs/spring-boot.adoc                 |  16 ++-
 .../camel/spring/boot/CamelAutoConfiguration.java  |   2 +
 .../health/CamelHealthCheckAutoConfiguration.java  |  83 +++++++++++++-
 .../CamelHealthCheckConfigurationProperties.java   | 127 +++++++++++++++++----
 docs/modules/ROOT/pages/spring-boot.adoc           |  10 +-
 5 files changed, 204 insertions(+), 34 deletions(-)

diff --git a/core/camel-spring-boot/src/main/docs/spring-boot.adoc 
b/core/camel-spring-boot/src/main/docs/spring-boot.adoc
index bac61a1..ba2e25d 100644
--- a/core/camel-spring-boot/src/main/docs/spring-boot.adoc
+++ b/core/camel-spring-boot/src/main/docs/spring-boot.adoc
@@ -92,7 +92,7 @@ When using spring-boot with Spring Boot make sure to use the 
following Maven dep
 ----
 
 
-The component supports 129 options, which are listed below.
+The component supports 133 options, which are listed below.
 
 
 
@@ -136,11 +136,15 @@ The component supports 129 options, which are listed 
below.
 | *camel.component.properties.properties-parser* | To use a custom 
PropertiesParser. The option is a 
org.apache.camel.component.properties.PropertiesParser type. |  | String
 | *camel.component.properties.system-properties-mode* | Sets the JVM system 
property mode (0 = never, 1 = fallback, 2 = override). The default mode 
(override) is to use system properties if present, and override any existing 
properties. OS environment variable mode is checked before JVM system property 
mode | 2 | Integer
 | *camel.dataformat.enabled* | Global option to enable/disable dataformat 
auto-configuration, default is true. | true | Boolean
-| *camel.health.context-enabled* | Option to enable/disable context 
health-check. | true | Boolean
-| *camel.health.enabled* | Global option to enable/disable Camel health check. 
| true | Boolean
-| *camel.health.parameters* | Extended configuration for routes, registry or 
custom health checks |  | Map
-| *camel.health.registry-enabled* | Option to enable/disable registry 
health-check. | true | Boolean
-| *camel.health.routes-enabled* | Option to enable/disable routes 
health-check. | true | Boolean
+| *camel.health.config* | Additional health check properties for fine grained 
configuration of health checks. |  | Map
+| *camel.health.config.enabled* | Set if the check associated to this 
configuration is enabled or not. Is default enabled. |  | Boolean
+| *camel.health.config.failure-threshold* | Set the number of failure before 
reporting the service as un-healthy. |  | Integer
+| *camel.health.config.interval* | Set the check interval in milli seconds. |  
| Long
+| *camel.health.config.parent* | The id of the health check such as routes or 
registry (can use * as wildcard) |  | String
+| *camel.health.context-enabled* | Whether context health check is enabled Is 
default enabled |  | Boolean
+| *camel.health.enabled* | Whether health check is enabled globally |  | 
Boolean
+| *camel.health.registry-enabled* | Whether registry health check is enabled 
Is default enabled |  | Boolean
+| *camel.health.routes-enabled* | Whether routes health check is enabled Is 
default enabled |  | Boolean
 | *camel.language.enabled* | Global option to enable/disable language 
auto-configuration, default is true. | true | Boolean
 | *camel.springboot.allow-use-original-message* | Sets whether to allow access 
to the original message from Camel's error handler, or from 
org.apache.camel.spi.UnitOfWork.getOriginalInMessage(). Turning this off can 
optimize performance, as defensive copy of the original message is not needed. 
Default is false. | false | Boolean
 | *camel.springboot.auto-startup* | Sets whether the object should 
automatically start when Camel starts. Important: Currently only routes can be 
disabled, as CamelContext's are always started. Note: When setting auto startup 
false on CamelContext then that takes precedence and no routes is started. You 
would need to start CamelContext explicit using the 
org.apache.camel.CamelContext.start() method, to start the context, and then 
you would need to start the routes manually using Camelcon [...]
diff --git 
a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
 
b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
index 8e95e41..07804a8 100644
--- 
a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
+++ 
b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
@@ -132,6 +132,8 @@ public class CamelAutoConfiguration {
         // configure the common/default options
         DefaultConfigurationConfigurer.configure(camelContext, config);
         // lookup and configure SPI beans
+        DefaultConfigurationConfigurer.afterConfigure(camelContext);
+        // and call after all properties are set
         DefaultConfigurationConfigurer.afterPropertiesSet(camelContext);
 
         return camelContext;
diff --git 
a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthCheckAutoConfiguration.java
 
b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthCheckAutoConfiguration.java
index 7f3b69c..66a8ff6 100644
--- 
a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthCheckAutoConfiguration.java
+++ 
b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthCheckAutoConfiguration.java
@@ -17,7 +17,13 @@
 package org.apache.camel.spring.boot.actuate.health;
 
 import org.apache.camel.CamelContext;
+import org.apache.camel.health.HealthCheck;
+import org.apache.camel.health.HealthCheckRegistry;
+import org.apache.camel.health.HealthCheckRepository;
 import org.apache.camel.spring.boot.CamelAutoConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.config.ConfigurableBeanFactory;
 import org.springframework.boot.actuate.health.HealthIndicator;
 import org.springframework.boot.autoconfigure.AutoConfigureAfter;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@@ -26,6 +32,7 @@ import 
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
 import 
org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Scope;
 
 @Configuration(proxyBeanMethods = false)
 @ConditionalOnClass({HealthIndicator.class})
@@ -34,13 +41,85 @@ import org.springframework.context.annotation.Configuration;
 @AutoConfigureAfter(CamelAutoConfiguration.class)
 public class CamelHealthCheckAutoConfiguration {
 
+    private static final Logger LOG = 
LoggerFactory.getLogger(CamelHealthCheckAutoConfiguration.class);
+
+    @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
     @ConditionalOnClass({CamelContext.class})
     @ConditionalOnMissingBean(CamelHealthCheckIndicator.class)
     protected class CamelHealthCheckIndicatorInitializer {
 
         @Bean
-        public HealthIndicator camelHealthCheckIndicator(CamelContext 
camelContext, CamelHealthCheckConfigurationProperties configuration) {
-            // TODO: use configuration to configure health-check
+        public HealthIndicator camelHealthCheckIndicator(CamelContext 
camelContext, CamelHealthCheckConfigurationProperties config) {
+            if (config != null && config.getEnabled() != null && 
!config.getEnabled()) {
+                // health check is disabled
+                return null;
+            }
+            if (config == null) {
+                config = new CamelHealthCheckConfigurationProperties();
+            }
+
+            HealthCheckRegistry hcr = 
camelContext.getExtension(HealthCheckRegistry.class);
+            if (hcr == null) {
+                LOG.warn("Cannot find HealthCheckRegistry from classpath. Add 
camel-health to classpath.");
+                return null;
+            }
+
+            // configure camel health check
+            // context is enabled by default
+            if (!config.getConfig().containsKey("context") || 
config.getContextEnabled() != null) {
+                HealthCheck hc = (HealthCheck) hcr.resolveById("context");
+                if (hc != null) {
+                    if (config.getContextEnabled() != null) {
+                        
hc.getConfiguration().setEnabled(config.getContextEnabled());
+                    }
+                    hcr.register(hc);
+                }
+            }
+            // routes is enabled by default
+            if (hcr.isEnabled() && (!config.getConfig().containsKey("routes") 
|| config.getRoutesEnabled() != null)) {
+                HealthCheckRepository hc = 
hcr.getRepository("routes").orElse((HealthCheckRepository) 
hcr.resolveById("routes"));
+                if (hc != null) {
+                    if (config.getRoutesEnabled() != null) {
+                        hc.setEnabled(config.getRoutesEnabled());
+                    }
+                    hcr.register(hc);
+                }
+            }
+            // registry is enabled by default
+            final CamelHealthCheckConfigurationProperties lambdaConfig = 
config;
+            if (hcr.isEnabled() && 
(!config.getConfig().containsKey("registry") || config.getRegistryEnabled() != 
null)) {
+                hcr.getRepository("registry").ifPresent(h -> {
+                    if (lambdaConfig.getRegistryEnabled() != null) {
+                        h.setEnabled(lambdaConfig.getRegistryEnabled());
+                    }
+                });
+            }
+
+            // configure health checks configurations
+            for (String id : config.getConfig().keySet()) {
+                
CamelHealthCheckConfigurationProperties.HealthCheckConfigurationProperties hcc 
= config.getConfig().get(id);
+                String parent = hcc.getParent();
+                // lookup health check by id
+                Object hc = hcr.getCheck(parent).orElse(null);
+                if (hc == null) {
+                    hc = hcr.resolveById(parent);
+                    if (hc == null) {
+                        LOG.warn("Cannot resolve HealthCheck with id: " + 
parent + " from classpath.");
+                        continue;
+                    }
+                    hcr.register(hc);
+                    if (hc instanceof HealthCheck) {
+                        ((HealthCheck) 
hc).getConfiguration().setParent(hcc.getParent());
+                        ((HealthCheck) 
hc).getConfiguration().setEnabled(hcc.getEnabled() != null ? hcc.getEnabled() : 
true);
+                        ((HealthCheck) 
hc).getConfiguration().setFailureThreshold(hcc.getFailureThreshold());
+                        ((HealthCheck) 
hc).getConfiguration().setInterval(hcc.getInterval());
+                    } else if (hc instanceof HealthCheckRepository) {
+                        ((HealthCheckRepository) 
hc).setEnabled(hcc.getEnabled() != null ? hcc.getEnabled() : true);
+                        ((HealthCheckRepository) hc).addConfiguration(id, 
hcc.toHealthCheckConfiguration());
+                    }
+                }
+            }
+
             return new CamelHealthCheckIndicator(camelContext);
         }
 
diff --git 
a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthCheckConfigurationProperties.java
 
b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthCheckConfigurationProperties.java
index 0e0bd98..7fd7637 100644
--- 
a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthCheckConfigurationProperties.java
+++ 
b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/actuate/health/CamelHealthCheckConfigurationProperties.java
@@ -16,75 +16,158 @@
  */
 package org.apache.camel.spring.boot.actuate.health;
 
+import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.camel.health.HealthCheckConfiguration;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 
 @ConfigurationProperties(prefix = "camel.health")
 public class CamelHealthCheckConfigurationProperties {
 
     /**
-     * Global option to enable/disable Camel health check.
+     * Whether health check is enabled globally
      */
-    private boolean enabled = true;
+    private Boolean enabled;
 
     /**
-     * Option to enable/disable context health-check.
+     * Whether context health check is enabled
+     *
+     * Is default enabled
      */
-    private boolean contextEnabled = true;
+    private Boolean contextEnabled;
 
     /**
-     * Option to enable/disable routes health-check.
+     * Whether routes health check is enabled
+     *
+     * Is default enabled
      */
-    private boolean routesEnabled = true;
+    private Boolean routesEnabled;
 
     /**
-     * Option to enable/disable registry health-check.
+     * Whether registry health check is enabled
+     *
+     * Is default enabled
      */
-    private boolean registryEnabled = true;
+    private Boolean registryEnabled;
 
     /**
-     * Extended configuration for routes, registry or custom health checks
+     * Additional health check properties for fine grained configuration of 
health checks.
      */
-    private Map<String, String> parameters;
+    private Map<String, HealthCheckConfigurationProperties> config = new 
HashMap<>();
 
-    public boolean isEnabled() {
+    public Boolean getEnabled() {
         return enabled;
     }
 
-    public void setEnabled(boolean enabled) {
+    public void setEnabled(Boolean enabled) {
         this.enabled = enabled;
     }
 
-    public boolean isContextEnabled() {
+    public Boolean getContextEnabled() {
         return contextEnabled;
     }
 
-    public void setContextEnabled(boolean contextEnabled) {
+    public void setContextEnabled(Boolean contextEnabled) {
         this.contextEnabled = contextEnabled;
     }
 
-    public boolean isRoutesEnabled() {
+    public Boolean getRoutesEnabled() {
         return routesEnabled;
     }
 
-    public void setRoutesEnabled(boolean routesEnabled) {
+    public void setRoutesEnabled(Boolean routesEnabled) {
         this.routesEnabled = routesEnabled;
     }
 
-    public boolean isRegistryEnabled() {
+    public Boolean getRegistryEnabled() {
         return registryEnabled;
     }
 
-    public void setRegistryEnabled(boolean registryEnabled) {
+    public void setRegistryEnabled(Boolean registryEnabled) {
         this.registryEnabled = registryEnabled;
     }
 
-    public Map<String, String> getParameters() {
-        return parameters;
+    public Map<String, HealthCheckConfigurationProperties> getConfig() {
+        return config;
     }
 
-    public void setParameters(Map<String, String> parameters) {
-        this.parameters = parameters;
+    public void setConfig(Map<String, HealthCheckConfigurationProperties> 
config) {
+        this.config = config;
+    }
+
+    @ConfigurationProperties(prefix = "camel.health.config")
+    public static class HealthCheckConfigurationProperties {
+
+        /**
+         * The id of the health check such as routes or registry (can use * as 
wildcard)
+         */
+        private String parent;
+
+        /**
+         * Set if the check associated to this configuration is enabled or not.
+         *
+         * Is default enabled.
+         */
+        private Boolean enabled;
+
+        /**
+         * Set the check interval in milli seconds.
+         */
+        private Long interval;
+
+        /**
+         * Set the number of failure before reporting the service as 
un-healthy.
+         */
+        private Integer failureThreshold;
+
+        public String getParent() {
+            return parent;
+        }
+
+        public void setParent(String parent) {
+            this.parent = parent;
+        }
+
+        public Boolean getEnabled() {
+            return enabled;
+        }
+
+        public void setEnabled(Boolean enabled) {
+            this.enabled = enabled;
+        }
+
+        public Long getInterval() {
+            return interval;
+        }
+
+        public void setInterval(Long interval) {
+            this.interval = interval;
+        }
+
+        public Integer getFailureThreshold() {
+            return failureThreshold;
+        }
+
+        public void setFailureThreshold(Integer failureThreshold) {
+            this.failureThreshold = failureThreshold;
+        }
+
+        public HealthCheckConfiguration toHealthCheckConfiguration() {
+            HealthCheckConfiguration answer = new HealthCheckConfiguration();
+            answer.setParent(parent);
+            if (enabled != null) {
+                answer.setEnabled(enabled);
+            }
+            if (interval != null) {
+                answer.setInterval(interval);
+            }
+            if (failureThreshold != null) {
+                answer.setFailureThreshold(failureThreshold);
+            }
+            return answer;
+        }
     }
 }
+
+
diff --git a/docs/modules/ROOT/pages/spring-boot.adoc 
b/docs/modules/ROOT/pages/spring-boot.adoc
index 4532a2a..ea34efe 100644
--- a/docs/modules/ROOT/pages/spring-boot.adoc
+++ b/docs/modules/ROOT/pages/spring-boot.adoc
@@ -92,7 +92,7 @@ When using spring-boot with Spring Boot make sure to use the 
following Maven dep
 ----
 
 
-The component supports 127 options, which are listed below.
+The component supports 129 options, which are listed below.
 
 
 
@@ -136,8 +136,11 @@ The component supports 127 options, which are listed below.
 | *camel.component.properties.properties-parser* | To use a custom 
PropertiesParser. The option is a 
org.apache.camel.component.properties.PropertiesParser type. |  | String
 | *camel.component.properties.system-properties-mode* | Sets the JVM system 
property mode (0 = never, 1 = fallback, 2 = override). The default mode 
(override) is to use system properties if present, and override any existing 
properties. OS environment variable mode is checked before JVM system property 
mode | 2 | Integer
 | *camel.dataformat.enabled* | Global option to enable/disable dataformat 
auto-configuration, default is true. | true | Boolean
-| *camel.health.check.routes.enabled* | Global option to enable/disable Camel 
extended health check for routes, default is false. | false | Boolean
-| *camel.health.enabled* | Global option to enable/disable camel health bean, 
default is true. | true | Boolean
+| *camel.health.config* |  |  | Map
+| *camel.health.context-enabled* |  |  | Boolean
+| *camel.health.enabled* |  |  | Boolean
+| *camel.health.registry-enabled* |  |  | Boolean
+| *camel.health.routes-enabled* |  |  | Boolean
 | *camel.language.enabled* | Global option to enable/disable language 
auto-configuration, default is true. | true | Boolean
 | *camel.springboot.allow-use-original-message* | Sets whether to allow access 
to the original message from Camel's error handler, or from 
org.apache.camel.spi.UnitOfWork.getOriginalInMessage(). Turning this off can 
optimize performance, as defensive copy of the original message is not needed. 
Default is false. | false | Boolean
 | *camel.springboot.auto-startup* | Sets whether the object should 
automatically start when Camel starts. Important: Currently only routes can be 
disabled, as CamelContext's are always started. Note: When setting auto startup 
false on CamelContext then that takes precedence and no routes is started. You 
would need to start CamelContext explicit using the 
org.apache.camel.CamelContext.start() method, to start the context, and then 
you would need to start the routes manually using Camelcon [...]
@@ -225,7 +228,6 @@ The component supports 127 options, which are listed below.
 | *camel.ssl.session-timeout* | The optional SSLSessionContext timeout time 
for javax.net.ssl.SSLSession in seconds. |  | String
 | *camel.ssl.trust-managers* | The optional trust manager configuration for 
creating the TrustManager used in constructing an SSLContext. |  | 
TrustManagersParameters
 | *management.info.camel.enabled* | Whether to enable Camel info. | true | 
Boolean
-| *management.info.camel.verbose* | Global option to enable/disable  health 
bean camel version info, default is true. | true | Boolean
 |===
 // spring-boot-auto-configure options: END
 

Reply via email to