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 446b34a camel-health - microprofile-health make it work better with
camel-quarkus as it has some special builditem in its feature to discover from
classpath health-check implementations and auto register them. So we need to
handle this and have its own context check too.
446b34a is described below
commit 446b34a5eec403e07973d35aabfb84370d138076
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun May 31 11:33:16 2020 +0200
camel-health - microprofile-health make it work better with camel-quarkus
as it has some special builditem in its feature to discover from classpath
health-check implementations and auto register them. So we need to handle this
and have its own context check too.
---
.../AbstractCamelMicroProfileHealthCheck.java | 5 +++-
.../health/CamelMicroProfileContextCheck.java | 30 +++++-----------------
.../health/CamelMicroProfileLivenessCheck.java | 3 ---
.../health/CamelMicroProfileReadinessCheck.java | 3 ---
.../health/CamelMicroProfileHealthCheckTest.java | 2 --
.../apache/camel/health/HealthCheckRegistry.java | 5 ++++
.../camel/impl/engine/AbstractCamelContext.java | 4 ++-
.../impl/health/DefaultHealthCheckRegistry.java | 25 ++++++++++++++++--
...ory.java => HealthCheckRegistryRepository.java} | 12 ++++++---
.../camel/main/DefaultConfigurationConfigurer.java | 30 ++++++++--------------
10 files changed, 59 insertions(+), 60 deletions(-)
diff --git
a/components/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/AbstractCamelMicroProfileHealthCheck.java
b/components/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/AbstractCamelMicroProfileHealthCheck.java
index 5e55165..fcdd558 100644
---
a/components/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/AbstractCamelMicroProfileHealthCheck.java
+++
b/components/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/AbstractCamelMicroProfileHealthCheck.java
@@ -46,7 +46,10 @@ public abstract class AbstractCamelMicroProfileHealthCheck
implements HealthChec
builder.name(getHealthCheckName());
if (camelContext != null) {
- Collection<Result> results =
HealthCheckHelper.invoke(camelContext, (HealthCheckFilter) check ->
check.getGroup() != null &&
check.getGroup().equals(getHealthGroupFilterExclude()));
+ Collection<Result> results = HealthCheckHelper.invoke(camelContext,
+ (HealthCheckFilter) check ->
+ // skip context as we have our own context check
+ check.getId().equals("context") ||
check.getGroup() != null &&
check.getGroup().equals(getHealthGroupFilterExclude()));
if (!results.isEmpty()) {
builder.up();
}
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 770fb5a..1e754a7 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
@@ -16,17 +16,15 @@
*/
package org.apache.camel.microprofile.health;
-import java.util.Map;
-
import javax.annotation.PostConstruct;
-import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
+import java.util.Map;
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.health.HealthCheckRegistry;
+import org.apache.camel.impl.health.ContextHealthCheck;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
@@ -38,37 +36,21 @@ import org.eclipse.microprofile.health.Readiness;
*/
@Readiness
@Liveness
-@ApplicationScoped
public class CamelMicroProfileContextCheck implements HealthCheck,
CamelContextAware {
@Inject
CamelContext camelContext;
- private org.apache.camel.health.HealthCheck contextHealthCheck;
-
- @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");
- if (!hcr.getCheck("context").isPresent()) {
- contextHealthCheck = (org.apache.camel.health.HealthCheck)
hcr.resolveById("context");
- if (contextHealthCheck != null) {
- hcr.register(contextHealthCheck);
- }
- }
- }
- }
-
@Override
public HealthCheckResponse call() {
final HealthCheckResponseBuilder builder =
HealthCheckResponse.builder();
builder.name("camel");
builder.down();
- if (contextHealthCheck != null) {
- Result result = contextHealthCheck.call();
+ if (camelContext != null) {
+ ContextHealthCheck chc = new ContextHealthCheck();
+ chc.setCamelContext(camelContext);
+ Result result = chc.call();
Map<String, Object> details = result.getDetails();
builder.withData("name", details.get("context.name").toString());
builder.withData("contextStatus",
details.get("context.status").toString());
diff --git
a/components/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileLivenessCheck.java
b/components/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileLivenessCheck.java
index 426b6e0..b8025a2 100644
---
a/components/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileLivenessCheck.java
+++
b/components/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileLivenessCheck.java
@@ -16,15 +16,12 @@
*/
package org.apache.camel.microprofile.health;
-import javax.enterprise.context.ApplicationScoped;
-
import org.eclipse.microprofile.health.Liveness;
/**
* Liveness checks
*/
@Liveness
-@ApplicationScoped
public class CamelMicroProfileLivenessCheck extends
AbstractCamelMicroProfileHealthCheck {
@Override
diff --git
a/components/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileReadinessCheck.java
b/components/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileReadinessCheck.java
index 8ab254d..c497841 100644
---
a/components/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileReadinessCheck.java
+++
b/components/camel-microprofile-health/src/main/java/org/apache/camel/microprofile/health/CamelMicroProfileReadinessCheck.java
@@ -16,15 +16,12 @@
*/
package org.apache.camel.microprofile.health;
-import javax.enterprise.context.ApplicationScoped;
-
import org.eclipse.microprofile.health.Readiness;
/**
* Readiness checks
*/
@Readiness
-@ApplicationScoped
public class CamelMicroProfileReadinessCheck extends
AbstractCamelMicroProfileHealthCheck {
@Override
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 5aeb3a2..c40f342 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,7 +33,6 @@ 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();
@@ -56,7 +55,6 @@ 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 a389035..b34fd7e 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
@@ -88,6 +88,11 @@ public interface HealthCheckRegistry extends
CamelContextAware, StaticService, I
}
/**
+ * Returns the repository identified by the given <code>id</code> if
available.
+ */
+ Optional<HealthCheckRepository> getRepository(String id);
+
+ /**
* Returns an optional {@link HealthCheckRegistry}, by default no registry
is
* present and it must be explicit activated. Components can
register/unregister
* health checks in response to life-cycle events (i.e. start/stop).
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 287b4a7..9d005d2 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
@@ -2498,7 +2498,9 @@ public abstract class AbstractCamelContext extends
BaseService
HealthCheckRegistry hcr = getExtension(HealthCheckRegistry.class);
if (hcr == null) {
// install health-check registry if discovered from classpath
- setExtension(HealthCheckRegistry.class,
createHealthCheckRegistry());
+ hcr = createHealthCheckRegistry();
+ hcr.setCamelContext(this);
+ setExtension(HealthCheckRegistry.class, hcr);
}
// Call all registered trackers with this context
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 b45943c..1c2b811 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
@@ -16,6 +16,7 @@
*/
package org.apache.camel.impl.health;
+import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Stream;
@@ -29,6 +30,7 @@ import org.apache.camel.health.HealthCheckRepository;
import org.apache.camel.spi.FactoryFinder;
import org.apache.camel.spi.annotations.JdkService;
import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -52,7 +54,7 @@ public class DefaultHealthCheckRegistry extends
ServiceSupport implements Health
public DefaultHealthCheckRegistry(CamelContext camelContext) {
this.checks = new CopyOnWriteArraySet<>();
this.repositories = new CopyOnWriteArraySet<>();
- this.repositories.add(new RegistryRepository());
+ this.repositories.add(new HealthCheckRegistryRepository());
setCamelContext(camelContext);
}
@@ -114,6 +116,9 @@ public class DefaultHealthCheckRegistry extends
ServiceSupport implements Health
if (answer == null) {
answer = resolveHealthCheckRepositoryById(id);
}
+ if (answer instanceof CamelContextAware) {
+ ((CamelContextAware) answer).setCamelContext(camelContext);
+ }
return answer;
}
@@ -166,6 +171,10 @@ public class DefaultHealthCheckRegistry extends
ServiceSupport implements Health
if (obj instanceof HealthCheck) {
HealthCheck healthCheck = (HealthCheck) obj;
+ // do we have this already
+ if (getCheck(healthCheck.getId()).isPresent()) {
+ return false;
+ }
boolean result = checks.add(healthCheck);
if (result) {
if (obj instanceof CamelContextAware) {
@@ -177,8 +186,11 @@ public class DefaultHealthCheckRegistry extends
ServiceSupport implements Health
return result;
} else {
HealthCheckRepository repository = (HealthCheckRepository) obj;
+ // do we have this already
+ if (getRepository(repository.getId()).isPresent()) {
+ return false;
+ }
boolean result = this.repositories.add(repository);
-
if (result) {
if (repository instanceof CamelContextAware) {
((CamelContextAware)
repository).setCamelContext(camelContext);
@@ -218,6 +230,15 @@ public class DefaultHealthCheckRegistry extends
ServiceSupport implements Health
//
// ************************************
+ /**
+ * Returns the repository identified by the given <code>id</code> if
available.
+ */
+ public Optional<HealthCheckRepository> getRepository(String id) {
+ return repositories.stream()
+ .filter(r -> ObjectHelper.equal(r.getId(), id))
+ .findFirst();
+ }
+
@Override
public Stream<HealthCheck> stream() {
if (enabled) {
diff --git
a/core/camel-health/src/main/java/org/apache/camel/impl/health/RegistryRepository.java
b/core/camel-health/src/main/java/org/apache/camel/impl/health/HealthCheckRegistryRepository.java
similarity index 82%
rename from
core/camel-health/src/main/java/org/apache/camel/impl/health/RegistryRepository.java
rename to
core/camel-health/src/main/java/org/apache/camel/impl/health/HealthCheckRegistryRepository.java
index edb735e..888e1c1 100644
---
a/core/camel-health/src/main/java/org/apache/camel/impl/health/RegistryRepository.java
+++
b/core/camel-health/src/main/java/org/apache/camel/impl/health/HealthCheckRegistryRepository.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.impl.health;
+import java.util.Set;
import java.util.stream.Stream;
import org.apache.camel.CamelContext;
@@ -28,7 +29,7 @@ import org.apache.camel.health.HealthCheckRepository;
*
* Camel will use this by default, so there is no need to register this
manually.
*/
-public class RegistryRepository implements CamelContextAware,
HealthCheckRepository {
+public class HealthCheckRegistryRepository implements CamelContextAware,
HealthCheckRepository {
private CamelContext context;
@Override
@@ -48,8 +49,11 @@ public class RegistryRepository implements
CamelContextAware, HealthCheckReposit
@Override
public Stream<HealthCheck> stream() {
- return this.context != null
- ? this.context.getRegistry().findByType(HealthCheck.class).stream()
- : Stream.empty();
+ if (context != null) {
+ Set<HealthCheck> set =
this.context.getRegistry().findByType(HealthCheck.class);
+ return set.stream();
+ } 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 b15775b..6959bd1 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
@@ -216,15 +216,19 @@ public final class DefaultConfigurationConfigurer {
HealthCheckRegistry hc =
camelContext.getExtension(HealthCheckRegistry.class);
if (hc != null && config.isHealthCheckEnabled()) {
// register context health-check by default
- Object context = hc.resolveById("context");
- if (context != null) {
- hc.register(context);
+ if (!hc.getCheck("context").isPresent()) {
+ Object context = hc.resolveById("context");
+ if (context != null) {
+ hc.register(context);
+ }
}
// register routes if enabled
if (config.isHealthCheckRoutesEnabled()) {
- Object routes = hc.resolveById("routes");
- if (routes != null) {
- hc.register(routes);
+ if (!hc.getCheck("routes").isPresent()) {
+ Object routes = hc.resolveById("routes");
+ if (routes != null) {
+ hc.register(routes);
+ }
}
}
}
@@ -406,16 +410,6 @@ public final class DefaultConfigurationConfigurer {
initThreadPoolProfiles(registry, camelContext);
}
- private static <T> void registerPropertyForBeanType(final Registry
registry, final Class<T> beanType, final Consumer<T> propertySetter) {
- T propertyBean = getSingleBeanOfType(registry, beanType);
- if (propertyBean == null) {
- return;
- }
-
- LOG.info("Using custom {}: {}", beanType.getSimpleName(),
propertyBean);
- propertySetter.accept(propertyBean);
- }
-
private static <T> T getSingleBeanOfType(Registry registry, Class<T> type)
{
Map<String, T> beans = registry.findByTypeWithName(type);
if (beans.size() == 1) {
@@ -425,10 +419,6 @@ public final class DefaultConfigurationConfigurer {
}
}
- private static <T> void registerPropertiesForBeanTypes(final Registry
registry, final Class<T> beanType, final Consumer<T> propertySetter) {
- registerPropertiesForBeanTypesWithCondition(registry, beanType, b ->
true, propertySetter);
- }
-
private static <T> void registerPropertiesForBeanTypesWithCondition(final
Registry registry, final Class<T> beanType, final Predicate<T> condition,
final
Consumer<T> propertySetter) {
final Map<String, T> beans = registry.findByTypeWithName(beanType);