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 473452d5f3c5a50f4ed644b4b3ce9908b6ee1066
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Fri May 29 13:40:12 2020 +0200

    camel-health - Make health simpler
---
 .../camel/health/HealthCheckConfiguration.java     | 104 ++++++---------------
 .../camel/impl/health/AbstractHealthCheck.java     |  10 +-
 .../impl/health/DefaultHealthCheckRegistry.java    |   2 +-
 .../apache/camel/impl/health/HealthCheckTest.java  |   4 +-
 4 files changed, 37 insertions(+), 83 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 ddb7052..cd02395 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,101 +22,51 @@ 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;
-    public static final Integer DEFAULT_VALUE_FAILURE_THRESHOLD = 0;
 
     /**
      * Set if the check associated to this configuration is enabled or not.
      */
-    private Boolean enabled;
+    private boolean enabled = true;
 
     /**
-     * Set the check interval.
+     * Set the check interval in milli seconds.
      */
-    private Duration interval;
+    private long interval;
 
     /**
      * Set the number of failure before reporting the service as un-healthy.
      */
-    private Integer failureThreshold;
+    private int failureThreshold;
 
     // *************************************************
     // Properties
     // *************************************************
 
-    /**
-     * @return true if the check associated to this configuration is enabled,
-     *         false otherwise.
-     */
-    public Boolean isEnabled() {
+    public boolean isEnabled() {
         return enabled;
     }
 
-    /**
-     * Set if the check associated to this configuration is enabled or not.
-     */
-    public void setEnabled(Boolean enabled) {
+    public void setEnabled(boolean enabled) {
         this.enabled = enabled;
     }
 
-    /**
-     * @return the check interval.
-     */
-    public Duration getInterval() {
+    public long getInterval() {
         return interval;
     }
 
-    /**
-     * Set the check interval.
-     */
-    public void setInterval(Duration interval) {
+    public void setInterval(long interval) {
         this.interval = interval;
     }
 
-    /**
-     * Set the check interval in a human readable format.
-     */
-    public void setInterval(String interval) {
-        if (ObjectHelper.isNotEmpty(interval)) {
-            this.interval = 
Duration.ofMillis(TimeUtils.toMilliSeconds(interval));
-        } else {
-            this.interval = null;
-        }
-    }
-
-    /**
-     * @return the number of failure before reporting the service as 
un-healthy.
-     */
-    public Integer getFailureThreshold() {
+    public int getFailureThreshold() {
         return failureThreshold;
     }
 
-    /**
-     * Set the number of failure before reporting the service as un-healthy.
-     */
-    public void setFailureThreshold(Integer failureThreshold) {
+    public void setFailureThreshold(int failureThreshold) {
         this.failureThreshold = failureThreshold;
     }
 
-    // *************************************************
-    //
-    // *************************************************
-    public static Boolean defaultValueEnabled() {
-        return DEFAULT_VALUE_ENABLED;
-    }
-
-    public static Duration defaultValueInterval() {
-        return DEFAULT_VALUE_INTERVAL;
-    }
-
-    public static Integer defaultValueFailureThreshold() {
-        return DEFAULT_VALUE_FAILURE_THRESHOLD;
-    }
-
     public HealthCheckConfiguration copy() {
         try {
             return (HealthCheckConfiguration)super.clone();
@@ -135,7 +85,7 @@ public class HealthCheckConfiguration implements Cloneable {
 
     public static final class Builder implements 
org.apache.camel.Builder<HealthCheckConfiguration> {
         private Boolean enabled;
-        private Duration interval;
+        private Long interval;
         private Integer failureThreshold;
 
         private Builder() {
@@ -157,29 +107,28 @@ public class HealthCheckConfiguration implements 
Cloneable {
             return this;
         }
 
-        public Builder enabled(Boolean enabled) {
+        public Builder enabled(boolean enabled) {
             this.enabled = enabled;
             return this;
         }
 
         public Builder interval(Duration interval) {
-            this.interval = interval;
+            this.interval = interval.toMillis();
             return this;
         }
 
-        public Builder interval(Long interval) {
+        public Builder interval(String interval) {
             return ObjectHelper.isNotEmpty(interval)
-                ? interval(Duration.ofMillis(interval))
-                : this;
+                    ? interval(TimeUtils.toMilliSeconds(interval))
+                    : this;
         }
 
-        public Builder interval(String interval) {
-            return ObjectHelper.isNotEmpty(interval)
-                ? interval(TimeUtils.toMilliSeconds(interval))
-                : this;
+        public Builder interval(long interval) {
+            this.interval = interval;
+            return this;
         }
 
-        public Builder failureThreshold(Integer failureThreshold) {
+        public Builder failureThreshold(int failureThreshold) {
             this.failureThreshold = failureThreshold;
             return this;
         }
@@ -187,10 +136,15 @@ public class HealthCheckConfiguration implements 
Cloneable {
         @Override
         public HealthCheckConfiguration build() {
             HealthCheckConfiguration conf = new HealthCheckConfiguration();
-            conf.setEnabled(ObjectHelper.supplyIfEmpty(enabled, 
HealthCheckConfiguration::defaultValueEnabled));
-            conf.setInterval(ObjectHelper.supplyIfEmpty(interval, 
HealthCheckConfiguration::defaultValueInterval));
-            
conf.setFailureThreshold(ObjectHelper.supplyIfEmpty(failureThreshold, 
HealthCheckConfiguration::defaultValueFailureThreshold));
-
+            if (enabled != null) {
+                conf.setEnabled(enabled);
+            }
+            if (interval != null) {
+                conf.setInterval(interval);
+            }
+            if (failureThreshold != null) {
+                conf.setFailureThreshold(failureThreshold);
+            }
             return conf;
         }
     }
diff --git 
a/core/camel-health/src/main/java/org/apache/camel/impl/health/AbstractHealthCheck.java
 
b/core/camel-health/src/main/java/org/apache/camel/impl/health/AbstractHealthCheck.java
index e4a9f53..679a0cb 100644
--- 
a/core/camel-health/src/main/java/org/apache/camel/impl/health/AbstractHealthCheck.java
+++ 
b/core/camel-health/src/main/java/org/apache/camel/impl/health/AbstractHealthCheck.java
@@ -114,9 +114,9 @@ public abstract class AbstractHealthCheck implements 
HealthCheck {
             final HealthCheckConfiguration conf = getConfiguration();
             final HealthCheckResultBuilder builder = 
HealthCheckResultBuilder.on(this);
             final ZonedDateTime now = ZonedDateTime.now();
-            final boolean enabled = 
ObjectHelper.supplyIfEmpty(conf.isEnabled(), 
HealthCheckConfiguration::defaultValueEnabled);
-            final Duration interval = 
ObjectHelper.supplyIfEmpty(conf.getInterval(), 
HealthCheckConfiguration::defaultValueInterval);
-            final Integer threshold = 
ObjectHelper.supplyIfEmpty(conf.getFailureThreshold(), 
HealthCheckConfiguration::defaultValueFailureThreshold);
+            final boolean enabled = conf.isEnabled();
+            final long interval = conf.getInterval();
+            final int threshold = conf.getFailureThreshold();
 
             // Extract relevant information from meta data.
             int invocationCount = (Integer)meta.getOrDefault(INVOCATION_COUNT, 
0);
@@ -139,10 +139,10 @@ public abstract class AbstractHealthCheck implements 
HealthCheck {
 
             // check if the last invocation is far enough to have this check 
invoked
             // again without violating the interval configuration.
-            if (lastResult != null && lastInvocation != null && 
!interval.isZero()) {
+            if (lastResult != null && lastInvocation != null && interval > 0) {
                 Duration elapsed = Duration.between(lastInvocation, now);
 
-                if (elapsed.compareTo(interval) < 0) {
+                if (elapsed.compareTo(Duration.ofMillis(interval)) < 0) {
                     LOGGER.debug("health-check {}/{} won't be invoked as 
interval ({}) is not yet expired (last-invocation={})",
                         getGroup(),
                         getId(),
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 c09b4db..0285df4 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,7 +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;
+    private boolean enabled = true;
 
     public DefaultHealthCheckRegistry() {
         this(null);
diff --git 
a/core/camel-health/src/test/java/org/apache/camel/impl/health/HealthCheckTest.java
 
b/core/camel-health/src/test/java/org/apache/camel/impl/health/HealthCheckTest.java
index 3131014..495be85 100644
--- 
a/core/camel-health/src/test/java/org/apache/camel/impl/health/HealthCheckTest.java
+++ 
b/core/camel-health/src/test/java/org/apache/camel/impl/health/HealthCheckTest.java
@@ -56,7 +56,7 @@ public class HealthCheckTest {
         MyHealthCheck check = new MyHealthCheck();
         check.setState(HealthCheck.State.UP);
         check.getConfiguration().setEnabled(true);
-        check.getConfiguration().setInterval(Duration.ofMillis(1000));
+        check.getConfiguration().setInterval(1000);
 
         HealthCheck.Result result1 = check.call();
         Assert.assertEquals(HealthCheck.State.UP, result1.getState());
@@ -103,7 +103,7 @@ public class HealthCheckTest {
         MyHealthCheck check = new MyHealthCheck();
         check.setState(HealthCheck.State.DOWN);
         check.getConfiguration().setEnabled(true);
-        check.getConfiguration().setInterval(Duration.ofMillis(500));
+        check.getConfiguration().setInterval(500);
         check.getConfiguration().setFailureThreshold(3);
 
         HealthCheck.Result result;

Reply via email to