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
The following commit(s) were added to refs/heads/master by this push: new 3056514 camel-health - Log if health check is enabled and allow 3rd party to set id so we can see from where its loaded/in-use 3056514 is described below commit 30565142fbfbc129720e0dff0172ed01030e7bfa Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat May 30 09:44:53 2020 +0200 camel-health - Log if health check is enabled and allow 3rd party to set id so we can see from where its loaded/in-use --- .../health/CamelMicroProfileContextCheck.java | 24 ++++++++++++++-------- ...CamelMicroProfileHealthCheckRepositoryTest.java | 1 - .../health/CamelMicroProfileHealthCheckTest.java | 2 ++ .../apache/camel/health/HealthCheckRegistry.java | 3 ++- .../camel/impl/engine/AbstractCamelContext.java | 5 +++++ .../impl/health/DefaultHealthCheckRegistry.java | 11 ++++++++++ 6 files changed, 36 insertions(+), 10 deletions(-) diff --git a/components/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileContextCheck.java b/components/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileContextCheck.java index f5fb772..6280380 100644 --- a/components/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileContextCheck.java +++ b/components/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileContextCheck.java @@ -18,13 +18,14 @@ package org.apache.camel.microprofile.health; import java.util.Map; +import javax.annotation.PostConstruct; import javax.inject.Inject; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; import org.apache.camel.health.HealthCheck.Result; import org.apache.camel.health.HealthCheck.State; -import org.apache.camel.impl.health.ContextHealthCheck; +import org.apache.camel.health.HealthCheckRegistry; import org.eclipse.microprofile.health.HealthCheck; import org.eclipse.microprofile.health.HealthCheckResponse; import org.eclipse.microprofile.health.HealthCheckResponseBuilder; @@ -39,12 +40,21 @@ import org.eclipse.microprofile.health.Readiness; public class CamelMicroProfileContextCheck implements HealthCheck, CamelContextAware { @Inject - private CamelContext camelContext; + CamelContext camelContext; - private ContextHealthCheck contextHealthCheck = new ContextHealthCheck(); + private org.apache.camel.health.HealthCheck contextHealthCheck; - public CamelMicroProfileContextCheck() { - contextHealthCheck.getConfiguration().setEnabled(true); + @PostConstruct + public void init() { + HealthCheckRegistry hcr = camelContext.getExtension(HealthCheckRegistry.class); + if (hcr != null) { + // load and register context health check into Camel and use it here with microprofile + hcr.setId("camel-microprofile-health"); + contextHealthCheck = (org.apache.camel.health.HealthCheck) hcr.resolveById("context"); + if (contextHealthCheck != null) { + hcr.register(contextHealthCheck); + } + } } @Override @@ -53,9 +63,7 @@ public class CamelMicroProfileContextCheck implements HealthCheck, CamelContextA builder.name("camel"); builder.down(); - if (camelContext != null) { - contextHealthCheck.setCamelContext(camelContext); - + if (contextHealthCheck != null) { Result result = contextHealthCheck.call(); Map<String, Object> details = result.getDetails(); builder.withData("name", details.get("context.name").toString()); diff --git a/components/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckRepositoryTest.java b/components/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckRepositoryTest.java index 113948b..8883998 100644 --- a/components/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckRepositoryTest.java +++ b/components/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckRepositoryTest.java @@ -23,7 +23,6 @@ import io.smallrye.health.SmallRyeHealth; import org.apache.camel.RoutesBuilder; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.health.HealthCheckRegistry; -import org.apache.camel.health.HealthCheckRepository; import org.eclipse.microprofile.health.HealthCheckResponse.State; import org.junit.Test; diff --git a/components/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckTest.java b/components/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckTest.java index c40f342..5aeb3a2 100644 --- a/components/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckTest.java +++ b/components/camel-microprofile-health/src/test/java/org/apache/camel/microprofile/health/CamelMicroProfileHealthCheckTest.java @@ -33,6 +33,7 @@ public class CamelMicroProfileHealthCheckTest extends CamelMicroProfileHealthTes context.setNameStrategy(new ExplicitCamelContextNameStrategy("health-context")); CamelMicroProfileContextCheck check = new CamelMicroProfileContextCheck(); check.setCamelContext(context); + check.init(); reporter.addHealthCheck(check); SmallRyeHealth health = reporter.getHealth(); @@ -55,6 +56,7 @@ public class CamelMicroProfileHealthCheckTest extends CamelMicroProfileHealthTes context.setNameStrategy(new ExplicitCamelContextNameStrategy("health-context")); CamelMicroProfileContextCheck check = new CamelMicroProfileContextCheck(); check.setCamelContext(context); + check.init(); reporter.addHealthCheck(check); context.stop(); 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 025a23a..a389035 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 @@ -24,12 +24,13 @@ import java.util.stream.Stream; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; import org.apache.camel.StaticService; +import org.apache.camel.spi.IdAware; import org.apache.camel.util.ObjectHelper; /** * A registry for health checks. */ -public interface HealthCheckRegistry extends CamelContextAware, StaticService { +public interface HealthCheckRegistry extends CamelContextAware, StaticService, IdAware { /** * Service factory key. diff --git a/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java index 0972e19..287b4a7 100644 --- a/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java +++ b/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java @@ -2810,6 +2810,11 @@ public abstract class AbstractCamelContext extends BaseService LOG.debug("BeanIntrospection invoked {} times during starting Camel", invokedCounter); } + HealthCheckRegistry hcr = getExtension(HealthCheckRegistry.class); + if (hcr != null && hcr.isEnabled()) { + LOG.info("HealthCheck enabled ({})", hcr.getId()); + } + // starting will continue in the start method } 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 0285df4..b45943c 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 @@ -39,6 +39,7 @@ import org.slf4j.LoggerFactory; public class DefaultHealthCheckRegistry extends ServiceSupport implements HealthCheckRegistry { private static final Logger LOG = LoggerFactory.getLogger(DefaultHealthCheckRegistry.class); + private String id = "camel-health"; private final Set<HealthCheck> checks; private final Set<HealthCheckRepository> repositories; private CamelContext camelContext; @@ -57,6 +58,16 @@ public class DefaultHealthCheckRegistry extends ServiceSupport implements Health } @Override + public String getId() { + return id; + } + + @Override + public void setId(String id) { + this.id = id; + } + + @Override public boolean isEnabled() { return enabled; }