You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/05/29 11:55:38 UTC
[camel] branch master updated (c2c1f96 -> cf31601)
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git.
from c2c1f96 Sync Properties
new 59c4ffb camel-health - Make health simpler
new 473452d camel-health - Make health simpler
new cf31601 camel-health: also register custom hc repository. And fix camel-spring and add better test.
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../spring/health/HealthCheckRegistryTest.java | 55 ++++++-----
.../spring/health/HealthCheckRegistryTest.xml | 33 ++-----
.../camel/health/HealthCheckConfiguration.java | 102 ++++++---------------
.../apache/camel/health/HealthCheckRegistry.java | 10 ++
.../core/xml/AbstractCamelContextFactoryBean.java | 14 +++
.../camel/impl/health/AbstractHealthCheck.java | 10 +-
.../impl/health/DefaultHealthCheckRegistry.java | 23 ++++-
.../apache/camel/impl/health/HealthCheckTest.java | 4 +-
.../MainConfigurationPropertiesConfigurer.java | 10 ++
.../camel-main-configuration-metadata.json | 2 +
.../camel/main/DefaultConfigurationConfigurer.java | 24 +++++
.../camel/main/DefaultConfigurationProperties.java | 58 ++++++++++++
.../management/mbean/ManagedCamelHealthMBean.java | 5 +-
.../camel/management/mbean/ManagedCamelHealth.java | 9 +-
14 files changed, 224 insertions(+), 135 deletions(-)
[camel] 03/03: camel-health: also register custom hc repository.
And fix camel-spring and add better test.
Posted by da...@apache.org.
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 cf316012b4c1be43b278f059adffd5b7c4c02f1e
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri May 29 13:52:13 2020 +0200
camel-health: also register custom hc repository. And fix camel-spring and add better test.
---
.../spring/health/HealthCheckRegistryTest.java | 55 +++++++++++++---------
.../spring/health/HealthCheckRegistryTest.xml | 33 ++++---------
.../core/xml/AbstractCamelContextFactoryBean.java | 14 ++++++
.../MainConfigurationPropertiesConfigurer.java | 10 ++++
.../camel-main-configuration-metadata.json | 2 +
.../camel/main/DefaultConfigurationConfigurer.java | 10 ++++
.../camel/management/mbean/ManagedCamelHealth.java | 4 +-
7 files changed, 78 insertions(+), 50 deletions(-)
diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/health/HealthCheckRegistryTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/health/HealthCheckRegistryTest.java
index ad966a9..448bf1f 100644
--- a/components/camel-spring/src/test/java/org/apache/camel/spring/health/HealthCheckRegistryTest.java
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/health/HealthCheckRegistryTest.java
@@ -16,15 +16,13 @@
*/
package org.apache.camel.spring.health;
-import java.util.Collection;
-import java.util.Optional;
+import java.util.List;
+import java.util.stream.Collectors;
import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.health.HealthCheck;
import org.apache.camel.health.HealthCheckRegistry;
-import org.apache.camel.health.HealthCheckRepository;
-import org.apache.camel.impl.health.RegistryRepository;
-import org.apache.camel.impl.health.RoutePerformanceCounterEvaluators;
-import org.apache.camel.impl.health.RoutesHealthCheckRepository;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -34,28 +32,39 @@ import static org.junit.Assert.assertNotNull;
public class HealthCheckRegistryTest {
@Test
- public void testRepositories() {
+ public void testHealthCheckRoutes() throws Exception {
CamelContext context = createContext("org/apache/camel/spring/health/HealthCheckRegistryTest.xml");
- Collection<HealthCheckRepository> repos = HealthCheckRegistry.get(context).getRepositories();
- Assert.assertNotNull(repos);
- Assert.assertEquals(2, repos.size());
- Assert.assertTrue(repos.stream().anyMatch(RegistryRepository.class::isInstance));
- Assert.assertTrue(repos.stream().anyMatch(RoutesHealthCheckRepository.class::isInstance));
+ HealthCheckRegistry hc = context.getExtension(HealthCheckRegistry.class);
+ assertNotNull(hc);
- Optional<RoutesHealthCheckRepository> repo = repos.stream()
- .filter(RoutesHealthCheckRepository.class::isInstance)
- .map(RoutesHealthCheckRepository.class::cast)
- .findFirst();
+ List<HealthCheck> checks = hc.stream().collect(Collectors.toList());
+ Assert.assertEquals(2, checks.size());
- Assert.assertTrue(repo.isPresent());
- Assert.assertEquals(2, repo.get().evaluators().count());
- Assert.assertEquals(1, repo.get().evaluators().filter(RoutePerformanceCounterEvaluators.ExchangesFailed.class::isInstance).count());
- Assert.assertEquals(1, repo.get().evaluators().filter(RoutePerformanceCounterEvaluators.LastProcessingTime.class::isInstance).count());
- Assert.assertEquals(1, repo.get().evaluators("route-1").count());
- Assert.assertEquals(1, repo.get().evaluators("route-1").filter(RoutePerformanceCounterEvaluators.ExchangesInflight.class::isInstance).count());
- }
+ for (HealthCheck check : checks) {
+ HealthCheck.Result response = check.call();
+
+ Assert.assertEquals(HealthCheck.State.UP, response.getState());
+ Assert.assertFalse(response.getMessage().isPresent());
+ Assert.assertFalse(response.getError().isPresent());
+ }
+ context.getRouteController().stopRoute("foo");
+
+ for (HealthCheck check : checks) {
+ HealthCheck.Result response = check.call();
+ boolean foo = "foo".equals(response.getDetails().get("route.id"));
+ if (foo) {
+ Assert.assertEquals(HealthCheck.State.DOWN, response.getState());
+ Assert.assertTrue(response.getMessage().isPresent());
+ Assert.assertFalse(response.getError().isPresent());
+ } else {
+ Assert.assertEquals(HealthCheck.State.UP, response.getState());
+ Assert.assertFalse(response.getMessage().isPresent());
+ Assert.assertFalse(response.getError().isPresent());
+ }
+ }
+ }
protected CamelContext createContext(String classpathConfigFile) {
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(classpathConfigFile);
diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/health/HealthCheckRegistryTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/health/HealthCheckRegistryTest.xml
index 879a9df..aa11fb9 100644
--- a/components/camel-spring/src/test/resources/org/apache/camel/spring/health/HealthCheckRegistryTest.xml
+++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/health/HealthCheckRegistryTest.xml
@@ -25,32 +25,17 @@
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
- <bean id="hc-repo-routes" class="org.apache.camel.impl.health.RoutesHealthCheckRepository">
- <property name="evaluators">
- <list>
- <bean class="org.apache.camel.impl.health.RoutePerformanceCounterEvaluators.ExchangesFailed">
- <constructor-arg value="10"/>
- </bean>
- <bean class="org.apache.camel.impl.health.RoutePerformanceCounterEvaluators.LastProcessingTime">
- <constructor-arg value="1000"/>
- <constructor-arg value="1"/>
- </bean>
- </list>
- </property>
- <property name="routesEvaluators">
- <map>
- <entry key="route-1">
- <list>
- <bean class="org.apache.camel.impl.health.RoutePerformanceCounterEvaluators.ExchangesInflight">
- <constructor-arg value="10"/>
- </bean>
- </list>
- </entry>
- </map>
- </property>
- </bean>
+ <bean id="hc-repo-routes" class="org.apache.camel.impl.health.RoutesHealthCheckRepository"/>
<camelContext xmlns="http://camel.apache.org/schema/spring">
+ <route id="foo">
+ <from uri="direct:start"/>
+ <to uri="mock:foo"/>
+ </route>
+ <route id="bar">
+ <from uri="direct:start2"/>
+ <to uri="mock:bar"/>
+ </route>
</camelContext>
</beans>
\ No newline at end of file
diff --git a/core/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java b/core/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
index 0f854d1..bebafce 100644
--- a/core/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
+++ b/core/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
@@ -49,6 +49,7 @@ import org.apache.camel.component.properties.PropertiesComponent;
import org.apache.camel.component.properties.PropertiesLocation;
import org.apache.camel.component.properties.PropertiesParser;
import org.apache.camel.health.HealthCheckRegistry;
+import org.apache.camel.health.HealthCheckRepository;
import org.apache.camel.impl.engine.DefaultManagementStrategy;
import org.apache.camel.impl.transformer.TransformerKey;
import org.apache.camel.impl.validator.ValidatorKey;
@@ -366,7 +367,20 @@ public abstract class AbstractCamelContextFactoryBean<T extends ModelCamelContex
LOG.info("Using HealthCheckRegistry: {}", healthCheckRegistry);
getContext().setExtension(HealthCheckRegistry.class, healthCheckRegistry);
} else {
+ // okay attempt to inject this camel context into existing health check (if any)
healthCheckRegistry = HealthCheckRegistry.get(getContext());
+ if (healthCheckRegistry != null) {
+ healthCheckRegistry.setCamelContext(getContext());
+ }
+ }
+ if (healthCheckRegistry != null) {
+ // Health check repository
+ Set<HealthCheckRepository> repositories = getContext().getRegistry().findByType(HealthCheckRepository.class);
+ if (org.apache.camel.util.ObjectHelper.isNotEmpty(repositories)) {
+ for (HealthCheckRepository repository : repositories) {
+ healthCheckRegistry.register(repository);
+ }
+ }
}
// UuidGenerator
UuidGenerator uuidGenerator = getBeanForType(UuidGenerator.class);
diff --git a/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java b/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java
index df6f5dd..b7c389d 100644
--- a/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java
+++ b/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java
@@ -71,6 +71,10 @@ public class MainConfigurationPropertiesConfigurer extends org.apache.camel.supp
case "EndpointRuntimeStatisticsEnabled": target.setEndpointRuntimeStatisticsEnabled(property(camelContext, boolean.class, value)); return true;
case "fileconfigurations":
case "FileConfigurations": target.setFileConfigurations(property(camelContext, java.lang.String.class, value)); return true;
+ case "healthcheckenabled":
+ case "HealthCheckEnabled": target.setHealthCheckEnabled(property(camelContext, boolean.class, value)); return true;
+ case "healthcheckroutesenabled":
+ case "HealthCheckRoutesEnabled": target.setHealthCheckRoutesEnabled(property(camelContext, boolean.class, value)); return true;
case "inflightrepositorybrowseenabled":
case "InflightRepositoryBrowseEnabled": target.setInflightRepositoryBrowseEnabled(property(camelContext, boolean.class, value)); return true;
case "javaroutesexcludepattern":
@@ -214,6 +218,8 @@ public class MainConfigurationPropertiesConfigurer extends org.apache.camel.supp
answer.put("EndpointLazyStartProducer", boolean.class);
answer.put("EndpointRuntimeStatisticsEnabled", boolean.class);
answer.put("FileConfigurations", java.lang.String.class);
+ answer.put("HealthCheckEnabled", boolean.class);
+ answer.put("HealthCheckRoutesEnabled", boolean.class);
answer.put("InflightRepositoryBrowseEnabled", boolean.class);
answer.put("JavaRoutesExcludePattern", java.lang.String.class);
answer.put("JavaRoutesIncludePattern", java.lang.String.class);
@@ -328,6 +334,10 @@ public class MainConfigurationPropertiesConfigurer extends org.apache.camel.supp
case "EndpointRuntimeStatisticsEnabled": return target.isEndpointRuntimeStatisticsEnabled();
case "fileconfigurations":
case "FileConfigurations": return target.getFileConfigurations();
+ case "healthcheckenabled":
+ case "HealthCheckEnabled": return target.isHealthCheckEnabled();
+ case "healthcheckroutesenabled":
+ case "HealthCheckRoutesEnabled": return target.isHealthCheckRoutesEnabled();
case "inflightrepositorybrowseenabled":
case "InflightRepositoryBrowseEnabled": return target.isInflightRepositoryBrowseEnabled();
case "javaroutesexcludepattern":
diff --git a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
index fcb0e11..fea5222 100644
--- a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
+++ b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
@@ -33,6 +33,8 @@
{ "name": "camel.main.endpointLazyStartProducer", "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first mes [...]
{ "name": "camel.main.endpointRuntimeStatisticsEnabled", "description": "Sets whether endpoint runtime statistics is enabled (gathers runtime usage of each incoming and outgoing endpoints). The default value is false.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "boolean", "javaType": "boolean" },
{ "name": "camel.main.fileConfigurations", "description": "Directory to load additional configuration files that contains configuration values that takes precedence over any other configuration. This can be used to refer to files that may have secret configuration that has been mounted on the file system for containers. You can specify a pattern to load from sub directories and a name pattern such as \/var\/app\/secret\/.properties, multiple directories can be separated by comma.", " [...]
+ { "name": "camel.main.healthCheckEnabled", "description": "Whether health check is enabled. Is default enabled.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "boolean", "javaType": "boolean", "defaultValue": true },
+ { "name": "camel.main.healthCheckRoutesEnabled", "description": "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.", "sourceType": "org.apache.camel.main.DefaultConfiguration [...]
{ "name": "camel.main.inflightRepositoryBrowseEnabled", "description": "Sets whether the inflight repository should allow browsing each inflight exchange. This is by default disabled as there is a very slight performance overhead when enabled.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "boolean", "javaType": "boolean" },
{ "name": "camel.main.javaRoutesExcludePattern", "description": "Used for exclusive filtering component scanning of RouteBuilder classes with Component annotation. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. For example to exclude all classes starting with Bar use: **\/Bar* To exclude all routes form a specific package use: com\/mycompany\/bar\/* [...]
{ "name": "camel.main.javaRoutesIncludePattern", "description": "Used for inclusive filtering component scanning of RouteBuilder classes with Component annotation. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. For example to include all classes starting with Foo use: **\/Foo To include all routes form a specific package use: com\/mycompany\/foo\/* To in [...]
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 814865a..6d9b968 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
@@ -29,6 +29,7 @@ import org.apache.camel.TypeConverters;
import org.apache.camel.cloud.ServiceRegistry;
import org.apache.camel.cluster.CamelClusterService;
import org.apache.camel.health.HealthCheckRegistry;
+import org.apache.camel.health.HealthCheckRepository;
import org.apache.camel.model.Model;
import org.apache.camel.processor.interceptor.BacklogTracer;
import org.apache.camel.spi.AsyncProcessorAwaitManager;
@@ -388,6 +389,15 @@ public final class DefaultConfigurationConfigurer {
healthCheckRegistry.setCamelContext(camelContext);
}
}
+ if (healthCheckRegistry != null) {
+ // Health check repository
+ Set<HealthCheckRepository> repositories = registry.findByType(HealthCheckRepository.class);
+ if (org.apache.camel.util.ObjectHelper.isNotEmpty(repositories)) {
+ for (HealthCheckRepository repository : repositories) {
+ healthCheckRegistry.register(repository);
+ }
+ }
+ }
// set the default thread pool profile if defined
initThreadPoolProfiles(registry, camelContext);
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 922bcbe..0959292 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
@@ -95,9 +95,7 @@ public class ManagedCamelHealth implements ManagedCamelHealthMBean {
result.getCheck().getGroup(),
result.getState().name(),
result.getCheck().getConfiguration().isEnabled(),
- result.getCheck().getConfiguration().getInterval() != null
- ? result.getCheck().getConfiguration().getInterval().toMillis()
- : null,
+ result.getCheck().getConfiguration().getInterval(),
result.getCheck().getConfiguration().getFailureThreshold()
}
);
[camel] 02/03: camel-health - Make health simpler
Posted by da...@apache.org.
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 <cl...@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;
[camel] 01/03: camel-health - Make health simpler
Posted by da...@apache.org.
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 <cl...@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) {