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 2021/11/30 20:30:59 UTC
[camel] 08/10: CAMEL-15133: camel-health - Resolve health-checks from classpath and make it friendlier to provide custom health checks.
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 2363c926d865153d517868fd7fd90d6ef588518a
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Nov 30 20:00:20 2021 +0100
CAMEL-15133: camel-health - Resolve health-checks from classpath and make it friendlier to provide custom health checks.
---
.../apache/camel/health/HealthCheckResolver.java | 18 +++++-------
.../impl/engine/DefaultHealthCheckResolver.java | 34 +++++++++++++++-------
.../camel/impl/health/MyFooHealthCheckTest.java | 4 +--
.../impl/health/DefaultHealthCheckRegistry.java | 4 +--
.../impl/health/DefaultHealthChecksLoader.java | 11 +++++--
5 files changed, 43 insertions(+), 28 deletions(-)
diff --git a/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckResolver.java b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckResolver.java
index 547284c..35f71af 100644
--- a/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckResolver.java
+++ b/core/camel-api/src/main/java/org/apache/camel/health/HealthCheckResolver.java
@@ -1,28 +1,26 @@
package org.apache.camel.health;
-import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
/**
* A pluggable strategy for resolving health checks in a loosely coupled manner
*/
-public interface HealthCheckResolver {
+public interface HealthCheckResolver extends CamelContextAware {
/**
* Resolves the given {@link HealthCheck}.
*
- * @param id the id of the {@link HealthCheck}
- * @param context the camel context
- * @return the resolved {@link HealthCheck}, or <tt>null</tt> if not found
+ * @param id the id of the {@link HealthCheck}
+ * @return the resolved {@link HealthCheck}, or <tt>null</tt> if not found
*/
- HealthCheck resolveHealthCheck(String id, CamelContext context);
+ HealthCheck resolveHealthCheck(String id);
/**
* Resolves the given {@link HealthCheckRepository}.
*
- * @param id the id of the {@link HealthCheckRepository}
- * @param context the camel context
- * @return the resolved {@link HealthCheckRepository}, or <tt>null</tt> if not found
+ * @param id the id of the {@link HealthCheckRepository}
+ * @return the resolved {@link HealthCheckRepository}, or <tt>null</tt> if not found
*/
- HealthCheckRepository resolveHealthCheckRepository(String id, CamelContext context);
+ HealthCheckRepository resolveHealthCheckRepository(String id);
}
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultHealthCheckResolver.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultHealthCheckResolver.java
index 08b8c29..bcdb377 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultHealthCheckResolver.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultHealthCheckResolver.java
@@ -17,6 +17,7 @@
package org.apache.camel.impl.engine;
import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.NoFactoryAvailableException;
import org.apache.camel.health.HealthCheck;
@@ -28,18 +29,29 @@ import org.apache.camel.spi.FactoryFinder;
* Default health check resolver that looks for health checks factories in
* <b>META-INF/services/org/apache/camel/health-check/</b>.
*/
-public class DefaultHealthCheckResolver implements HealthCheckResolver {
+public class DefaultHealthCheckResolver implements HealthCheckResolver, CamelContextAware {
public static final String HEALTH_CHECK_RESOURCE_PATH = "META-INF/services/org/apache/camel/health-check/";
protected FactoryFinder healthCheckFactory;
+ private CamelContext camelContext;
@Override
- public HealthCheck resolveHealthCheck(String id, CamelContext context) {
+ public CamelContext getCamelContext() {
+ return camelContext;
+ }
+
+ @Override
+ public void setCamelContext(CamelContext camelContext) {
+ this.camelContext = camelContext;
+ }
+
+ @Override
+ public HealthCheck resolveHealthCheck(String id) {
// lookup in registry first
- HealthCheck answer = context.getRegistry().lookupByNameAndType(id + "-health-check", HealthCheck.class);
+ HealthCheck answer = camelContext.getRegistry().lookupByNameAndType(id + "-health-check", HealthCheck.class);
if (answer == null) {
- answer = context.getRegistry().lookupByNameAndType(id, HealthCheck.class);
+ answer = camelContext.getRegistry().lookupByNameAndType(id, HealthCheck.class);
}
if (answer != null) {
return answer;
@@ -47,7 +59,7 @@ public class DefaultHealthCheckResolver implements HealthCheckResolver {
Class<?> type = null;
try {
- type = findHealthCheck(id, context);
+ type = findHealthCheck(id, camelContext);
} catch (NoFactoryAvailableException e) {
// ignore
} catch (Exception e) {
@@ -56,7 +68,7 @@ public class DefaultHealthCheckResolver implements HealthCheckResolver {
if (type != null) {
if (HealthCheck.class.isAssignableFrom(type)) {
- return (HealthCheck) context.getInjector().newInstance(type, false);
+ return (HealthCheck) camelContext.getInjector().newInstance(type, false);
} else {
throw new IllegalArgumentException(
"Resolving health-check: " + id + " detected type conflict: Not a HealthCheck implementation. Found: "
@@ -68,12 +80,12 @@ public class DefaultHealthCheckResolver implements HealthCheckResolver {
}
@Override
- public HealthCheckRepository resolveHealthCheckRepository(String id, CamelContext context) {
+ public HealthCheckRepository resolveHealthCheckRepository(String id) {
// lookup in registry first
HealthCheckRepository answer
- = context.getRegistry().lookupByNameAndType(id + "-health-check-repository", HealthCheckRepository.class);
+ = camelContext.getRegistry().lookupByNameAndType(id + "-health-check-repository", HealthCheckRepository.class);
if (answer == null) {
- answer = context.getRegistry().lookupByNameAndType(id, HealthCheckRepository.class);
+ answer = camelContext.getRegistry().lookupByNameAndType(id, HealthCheckRepository.class);
}
if (answer != null) {
return answer;
@@ -81,7 +93,7 @@ public class DefaultHealthCheckResolver implements HealthCheckResolver {
Class<?> type = null;
try {
- type = findHealthCheckRepository(id, context);
+ type = findHealthCheckRepository(id, camelContext);
} catch (NoFactoryAvailableException e) {
// ignore
} catch (Exception e) {
@@ -90,7 +102,7 @@ public class DefaultHealthCheckResolver implements HealthCheckResolver {
if (type != null) {
if (HealthCheckRepository.class.isAssignableFrom(type)) {
- return (HealthCheckRepository) context.getInjector().newInstance(type, false);
+ return (HealthCheckRepository) camelContext.getInjector().newInstance(type, false);
} else {
throw new IllegalArgumentException(
"Resolving health-check-repository: " + id
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/health/MyFooHealthCheckTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/health/MyFooHealthCheckTest.java
index e2f5147..7b7c083 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/health/MyFooHealthCheckTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/health/MyFooHealthCheckTest.java
@@ -38,7 +38,7 @@ public class MyFooHealthCheckTest extends ContextTestSupport {
context.start();
HealthCheck hc
- = context.adapt(ExtendedCamelContext.class).getHealthCheckResolver().resolveHealthCheck("myfoo", context);
+ = context.adapt(ExtendedCamelContext.class).getHealthCheckResolver().resolveHealthCheck("myfoo");
Assertions.assertNotNull(hc);
Assertions.assertEquals("acme", hc.getGroup());
@@ -55,7 +55,7 @@ public class MyFooHealthCheckTest extends ContextTestSupport {
context.start();
HealthCheck hc
- = context.adapt(ExtendedCamelContext.class).getHealthCheckResolver().resolveHealthCheck("myfoo", context);
+ = context.adapt(ExtendedCamelContext.class).getHealthCheckResolver().resolveHealthCheck("myfoo");
Assertions.assertNotNull(hc);
HealthCheckRegistry hcr = context.getExtension(HealthCheckRegistry.class);
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 6b55419..d602162 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
@@ -127,7 +127,7 @@ public class DefaultHealthCheckRegistry extends ServiceSupport implements Health
.orElse(camelContext.getRegistry().findByTypeWithName(HealthCheck.class).get(id));
if (answer == null) {
HealthCheckResolver resolver = camelContext.adapt(ExtendedCamelContext.class).getHealthCheckResolver();
- answer = resolver.resolveHealthCheck(id, camelContext);
+ answer = resolver.resolveHealthCheck(id);
}
return answer;
@@ -140,7 +140,7 @@ public class DefaultHealthCheckRegistry extends ServiceSupport implements Health
if (answer == null) {
// discover via classpath (try first via -health-check-repository and then id as-is)
HealthCheckResolver resolver = camelContext.adapt(ExtendedCamelContext.class).getHealthCheckResolver();
- answer = resolver.resolveHealthCheckRepository(id, camelContext);
+ answer = resolver.resolveHealthCheckRepository(id);
}
return answer;
diff --git a/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthChecksLoader.java b/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthChecksLoader.java
index 86cb771..c9efaa8 100644
--- a/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthChecksLoader.java
+++ b/core/camel-health/src/main/java/org/apache/camel/impl/health/DefaultHealthChecksLoader.java
@@ -62,7 +62,7 @@ public class DefaultHealthChecksLoader {
if (acceptResource(resource)) {
String id = extractId(resource);
LOG.trace("Loading HealthCheck: {}", id);
- HealthCheck hc = healthCheckResolver.resolveHealthCheck(id, camelContext);
+ HealthCheck hc = healthCheckResolver.resolveHealthCheck(id);
if (hc != null) {
LOG.debug("Loaded HealthCheck: {}/{}", hc.getGroup(), hc.getId());
answer.add(hc);
@@ -84,7 +84,7 @@ public class DefaultHealthChecksLoader {
}
// this is an out of the box health-check
- if (loc.endsWith("context-health")) {
+ if (loc.endsWith("context-check")) {
return false;
}
@@ -93,7 +93,12 @@ public class DefaultHealthChecksLoader {
protected String extractId(Resource resource) {
String loc = resource.getLocation();
- return StringHelper.after(loc, META_INF_SERVICES + "/");
+ loc = StringHelper.after(loc, META_INF_SERVICES + "/");
+ // remove -check suffix
+ if (loc != null && loc.endsWith("-check")) {
+ loc = loc.substring(0, loc.length() - 6);
+ }
+ return loc;
}
}