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 10:48:47 UTC
[camel] branch master updated: CAMLE-15126: camel-health - Simpler
API to resolve health checks or its variation with repository that are used
for routes.
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 5bdee74 CAMLE-15126: camel-health - Simpler API to resolve health checks or its variation with repository that are used for routes.
5bdee74 is described below
commit 5bdee7456028428203af0649d778be38253ec81c
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri May 29 12:48:19 2020 +0200
CAMLE-15126: camel-health - Simpler API to resolve health checks or its variation with repository that are used for routes.
---
...CamelMicroProfileHealthCheckRepositoryTest.java | 4 +-
.../apache/camel/health/HealthCheckRegistry.java | 31 +++-----
.../impl/health/DefaultHealthCheckRegistry.java | 82 +++++++++++++---------
.../health/DefaultHealthCheckRegistryTest.java | 4 +-
.../camel/management/ManagedHealthCheckTest.java | 4 +-
5 files changed, 64 insertions(+), 61 deletions(-)
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 b4b89d1..113948b 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
@@ -33,7 +33,7 @@ public class CamelMicroProfileHealthCheckRepositoryTest extends CamelMicroProfil
public void testCamelHealthRepositoryUpStatus() {
HealthCheckRegistry healthCheckRegistry = HealthCheckRegistry.get(context);
// enable routes health check
- HealthCheckRepository hc = healthCheckRegistry.resolveHealthCheckRepositoryById("routes");
+ Object hc = healthCheckRegistry.resolveById("routes");
healthCheckRegistry.register(hc);
CamelMicroProfileReadinessCheck readinessCheck = new CamelMicroProfileReadinessCheck();
@@ -57,7 +57,7 @@ public class CamelMicroProfileHealthCheckRepositoryTest extends CamelMicroProfil
public void testCamelHealthRepositoryDownStatus() throws Exception {
HealthCheckRegistry healthCheckRegistry = HealthCheckRegistry.get(context);
// enable routes health check
- HealthCheckRepository hc = healthCheckRegistry.resolveHealthCheckRepositoryById("routes");
+ Object hc = healthCheckRegistry.resolveById("routes");
healthCheckRegistry.register(hc);
CamelMicroProfileReadinessCheck readinessCheck = new CamelMicroProfileReadinessCheck();
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 4b55514..74f8dec 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,39 +37,26 @@ public interface HealthCheckRegistry extends CamelContextAware, StaticService {
String FACTORY = "health-check-registry";
/**
- * Resolves {@link HealthCheck} by id.
+ * Resolves {@link HealthCheck} or {@link HealthCheckRepository} by id.
*
* Will first lookup in this {@link HealthCheckRegistry} and then {@link org.apache.camel.spi.Registry},
* and lastly do classpath scanning via {@link org.apache.camel.spi.annotations.ServiceFactory}.
- * The classpath scanning is attempted first with id-health-check as the key, and then with id as fallback
- * if not found the first time.
- */
- HealthCheck resolveHealthCheckById(String id);
-
- /**
- * Resolves {@link HealthCheckRepository} by id.
+ * The classpath scanning is attempted first with id-health-check or id-health-check-repository as the key,
+ * and then with id as fallback if not found the first time.
*
- * Will first lookup in this {@link HealthCheckRegistry} and then {@link org.apache.camel.spi.Registry},
- * and lastly do classpath scanning via {@link org.apache.camel.spi.annotations.ServiceFactory}.
- * The classpath scanning is attempted first with id-health-check-repository as the key, and then with id as fallback
- * if not found the first time.
- */
- HealthCheckRepository resolveHealthCheckRepositoryById(String id);
-
- /**
- * Registers a {@link HealthCheck}.
+ * @return either {@link HealthCheck} or {@link HealthCheckRepository}, or <tt>null</tt> if none found.
*/
- boolean register(HealthCheck check);
+ Object resolveById(String id);
/**
- * Unregisters a {@link HealthCheck}.
+ * Registers a {@link HealthCheck} or {@link HealthCheckRepository}.
*/
- boolean unregister(HealthCheck check);
+ boolean register(Object obj);
/**
- * Registers a {@link HealthCheckRepository}.
+ * Unregisters a {@link HealthCheck} or {@link HealthCheckRepository}.
*/
- boolean register(HealthCheckRepository repository);
+ boolean unregister(Object obj);
/**
* A collection of health check IDs.
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 db0df07..db82c2f 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
@@ -87,8 +87,16 @@ public class DefaultHealthCheckRegistry extends ServiceSupport implements Health
}
@Override
+ public Object resolveById(String id) {
+ Object answer = resolveHealthCheckById(id);
+ if (answer == null) {
+ answer = resolveHealthCheckRepositoryById(id);
+ }
+ return answer;
+ }
+
@SuppressWarnings("unchecked")
- public HealthCheck resolveHealthCheckById(String id) {
+ private HealthCheck resolveHealthCheckById(String id) {
HealthCheck answer =
checks.stream().filter(h -> h.getId().equals(id)).findFirst()
.orElse(camelContext.getRegistry().findByTypeWithName(HealthCheck.class).get(id));
@@ -107,8 +115,8 @@ public class DefaultHealthCheckRegistry extends ServiceSupport implements Health
return answer;
}
- @Override
- public HealthCheckRepository resolveHealthCheckRepositoryById(String id) {
+ @SuppressWarnings("unchecked")
+ private HealthCheckRepository resolveHealthCheckRepositoryById(String id) {
HealthCheckRepository answer =
repositories.stream().filter(h -> h.getId().equals(id)).findFirst()
.orElse(camelContext.getRegistry().findByTypeWithName(HealthCheckRepository.class).get(id));
@@ -128,50 +136,60 @@ public class DefaultHealthCheckRegistry extends ServiceSupport implements Health
}
@Override
- public boolean register(HealthCheck check) {
- if (check == null) {
+ public boolean register(Object obj) {
+ boolean accept = obj instanceof HealthCheck || obj instanceof HealthCheckRepository;
+ if (!accept) {
throw new IllegalArgumentException();
}
- boolean result = checks.add(check);
- if (result) {
- if (check instanceof CamelContextAware) {
- ((CamelContextAware) check).setCamelContext(camelContext);
- }
+ if (obj instanceof HealthCheck) {
+ HealthCheck healthCheck = (HealthCheck) obj;
+ boolean result = checks.add(healthCheck);
+ if (result) {
+ if (obj instanceof CamelContextAware) {
+ ((CamelContextAware) obj).setCamelContext(camelContext);
+ }
- LOG.debug("HealthCheck with id {} successfully registered", check.getId());
- }
+ LOG.debug("HealthCheck with id {} successfully registered", healthCheck.getId());
+ }
+ return result;
+ } else {
+ HealthCheckRepository repository = (HealthCheckRepository) obj;
+ boolean result = this.repositories.add(repository);
- return result;
- }
+ if (result) {
+ if (repository instanceof CamelContextAware) {
+ ((CamelContextAware) repository).setCamelContext(camelContext);
+ }
- @Override
- public boolean unregister(HealthCheck check) {
- boolean result = checks.remove(check);
- if (result) {
- LOG.debug("HealthCheck with id {} successfully un-registered", check.getId());
+ LOG.debug("HealthCheckRepository with id {} successfully registered", repository.getId());
+ }
+ return result;
}
-
- return result;
}
@Override
- public boolean register(HealthCheckRepository repository) {
- if (repository == null) {
+ public boolean unregister(Object obj) {
+ boolean accept = obj instanceof HealthCheck || obj instanceof HealthCheckRepository;
+ if (!accept) {
throw new IllegalArgumentException();
}
- boolean result = this.repositories.add(repository);
-
- if (result) {
- if (repository instanceof CamelContextAware) {
- ((CamelContextAware) repository).setCamelContext(camelContext);
+ if (obj instanceof HealthCheck) {
+ HealthCheck healthCheck = (HealthCheck) obj;
+ boolean result = checks.remove(healthCheck);
+ if (result) {
+ LOG.debug("HealthCheck with id {} successfully un-registered", healthCheck.getId());
}
-
- LOG.debug("HealthCheckRepository with id {} successfully registered", repository.getId());
+ return result;
+ } else {
+ HealthCheckRepository repository = (HealthCheckRepository) obj;
+ boolean result = this.repositories.remove(repository);
+ if (result) {
+ LOG.debug("HealthCheckRepository with id {} successfully un-registered", repository.getId());
+ }
+ return result;
}
-
- return result;
}
// ************************************
diff --git a/core/camel-health/src/test/java/org/apache/camel/impl/health/DefaultHealthCheckRegistryTest.java b/core/camel-health/src/test/java/org/apache/camel/impl/health/DefaultHealthCheckRegistryTest.java
index 384d0f2..0aa4914 100644
--- a/core/camel-health/src/test/java/org/apache/camel/impl/health/DefaultHealthCheckRegistryTest.java
+++ b/core/camel-health/src/test/java/org/apache/camel/impl/health/DefaultHealthCheckRegistryTest.java
@@ -117,7 +117,7 @@ public class DefaultHealthCheckRegistryTest {
HealthCheckRegistry registry = new DefaultHealthCheckRegistry();
registry.setCamelContext(context);
- HealthCheck hc = registry.resolveHealthCheckById("context");
+ HealthCheck hc = (HealthCheck) registry.resolveById("context");
Assert.assertNotNull(hc);
Assert.assertEquals("camel", hc.getGroup());
Assert.assertEquals("context", hc.getId());
@@ -150,7 +150,7 @@ public class DefaultHealthCheckRegistryTest {
HealthCheckRegistry registry = new DefaultHealthCheckRegistry();
registry.setCamelContext(context);
- HealthCheckRepository hc = registry.resolveHealthCheckRepositoryById("routes");
+ HealthCheckRepository hc = (HealthCheckRepository) registry.resolveById("routes");
Assert.assertNotNull(hc);
Assert.assertEquals("routes", hc.getId());
Assert.assertTrue(hc instanceof RoutesHealthCheckRepository);
diff --git a/core/camel-management/src/test/java/org/apache/camel/management/ManagedHealthCheckTest.java b/core/camel-management/src/test/java/org/apache/camel/management/ManagedHealthCheckTest.java
index 9f332a7..445c6d7 100644
--- a/core/camel-management/src/test/java/org/apache/camel/management/ManagedHealthCheckTest.java
+++ b/core/camel-management/src/test/java/org/apache/camel/management/ManagedHealthCheckTest.java
@@ -19,12 +19,10 @@ package org.apache.camel.management;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.openmbean.TabularData;
-
import java.util.Collection;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.health.HealthCheck;
import org.apache.camel.health.HealthCheckRegistry;
import org.apache.camel.impl.health.DefaultHealthCheckRegistry;
import org.junit.Test;
@@ -38,7 +36,7 @@ public class ManagedHealthCheckTest extends ManagementTestSupport {
// install health check manually
HealthCheckRegistry registry = new DefaultHealthCheckRegistry();
registry.setCamelContext(context);
- HealthCheck hc = registry.resolveHealthCheckById("context");
+ Object hc = registry.resolveById("context");
registry.register(hc);
context.setExtension(HealthCheckRegistry.class, registry);