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 2022/03/04 14:38:08 UTC
[camel] 01/03: CAMEL-17727: camel-kafka - Use a single health-check repository to hold all consumer/producer 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 ecf84c541d2d3752d9e9795691d6c6ae40cd3783
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Mar 4 15:12:16 2022 +0100
CAMEL-17727: camel-kafka - Use a single health-check repository to hold all consumer/producer checks.
---
.../org/apache/camel/health-check/camel-kafka | 2 +
.../camel/component/kafka/KafkaConsumer.java | 38 +++++++---
.../kafka/KafkaHealthCheckRepository.java | 88 ++++++++++++++++++++++
.../camel/component/kafka/KafkaProducer.java | 22 +++---
4 files changed, 129 insertions(+), 21 deletions(-)
diff --git a/components/camel-kafka/src/generated/resources/META-INF/services/org/apache/camel/health-check/camel-kafka b/components/camel-kafka/src/generated/resources/META-INF/services/org/apache/camel/health-check/camel-kafka
new file mode 100644
index 0000000..911c924
--- /dev/null
+++ b/components/camel-kafka/src/generated/resources/META-INF/services/org/apache/camel/health-check/camel-kafka
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.kafka.KafkaHealthCheckRepository
diff --git a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java
index 185d8ac..03c9ecb 100644
--- a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java
+++ b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaConsumer.java
@@ -24,12 +24,11 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
-import org.apache.camel.CamelContextAware;
import org.apache.camel.Processor;
import org.apache.camel.ResumeAware;
import org.apache.camel.component.kafka.consumer.support.KafkaConsumerResumeStrategy;
-import org.apache.camel.health.HealthCheck;
import org.apache.camel.health.HealthCheckAware;
+import org.apache.camel.health.HealthCheckRegistry;
import org.apache.camel.spi.StateRepository;
import org.apache.camel.support.BridgeExceptionHandlerToErrorHandler;
import org.apache.camel.support.DefaultConsumer;
@@ -46,6 +45,7 @@ public class KafkaConsumer extends DefaultConsumer implements ResumeAware<KafkaC
protected ExecutorService executor;
private final KafkaEndpoint endpoint;
+ private KafkaConsumerHealthCheck consumerHealthCheck;
// This list helps to work around the infinite loop of KAFKA-1894
private final List<KafkaFetchRecords> tasks = new ArrayList<>();
private volatile boolean stopOffsetRepo;
@@ -71,16 +71,6 @@ public class KafkaConsumer extends DefaultConsumer implements ResumeAware<KafkaC
protected void doBuild() throws Exception {
super.doBuild();
- // build health-check
- String rid = getRouteId();
- if (rid == null) {
- // not from a route so need some other uuid
- rid = endpoint.getCamelContext().getUuidGenerator().generateUuid();
- }
- HealthCheck hc = new KafkaConsumerHealthCheck(this, rid);
- CamelContextAware.trySetCamelContext(hc, endpoint.getCamelContext());
- setHealthCheck(hc);
-
if (endpoint.getComponent().getPollExceptionStrategy() != null) {
pollExceptionStrategy = endpoint.getComponent().getPollExceptionStrategy();
} else {
@@ -125,6 +115,21 @@ public class KafkaConsumer extends DefaultConsumer implements ResumeAware<KafkaC
endpoint.getConfiguration().isBreakOnFirstError());
super.doStart();
+ HealthCheckRegistry hcr = endpoint.getCamelContext().getExtension(HealthCheckRegistry.class);
+ if (hcr != null) {
+ String rid = getRouteId();
+ if (rid == null) {
+ // not from a route so need some other uuid
+ rid = endpoint.getCamelContext().getUuidGenerator().generateUuid();
+ }
+ consumerHealthCheck = new KafkaConsumerHealthCheck(this, rid);
+
+ hcr.getRepository("camel-kafka").ifPresent(r -> {
+ KafkaHealthCheckRepository kr = (KafkaHealthCheckRepository) r;
+ kr.addHealthCheck(consumerHealthCheck);
+ });
+ }
+
// is the offset repository already started?
StateRepository<String, String> repo = endpoint.getConfiguration().getOffsetRepository();
if (repo instanceof ServiceSupport) {
@@ -159,6 +164,15 @@ public class KafkaConsumer extends DefaultConsumer implements ResumeAware<KafkaC
protected void doStop() throws Exception {
LOG.info("Stopping Kafka consumer on topic: {}", endpoint.getConfiguration().getTopic());
+ HealthCheckRegistry hcr = endpoint.getCamelContext().getExtension(HealthCheckRegistry.class);
+ if (hcr != null) {
+ hcr.getRepository("camel-kafka").ifPresent(r -> {
+ KafkaHealthCheckRepository kr = (KafkaHealthCheckRepository) r;
+ kr.removeHealthCheck(consumerHealthCheck);
+ });
+ consumerHealthCheck = null;
+ }
+
if (executor != null) {
if (getEndpoint() != null && getEndpoint().getCamelContext() != null) {
// signal kafka consumer to stop
diff --git a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaHealthCheckRepository.java b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaHealthCheckRepository.java
new file mode 100644
index 0000000..be54414
--- /dev/null
+++ b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaHealthCheckRepository.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.kafka;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Stream;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.DeferredContextBinding;
+import org.apache.camel.NonManagedService;
+import org.apache.camel.StaticService;
+import org.apache.camel.health.HealthCheck;
+import org.apache.camel.health.HealthCheckRepository;
+import org.apache.camel.support.service.ServiceSupport;
+
+/**
+ * Repository for camel-kafka {@link HealthCheck}s.
+ */
+@org.apache.camel.spi.annotations.HealthCheck("camel-kafka")
+@DeferredContextBinding
+public class KafkaHealthCheckRepository extends ServiceSupport
+ implements CamelContextAware, HealthCheckRepository, StaticService, NonManagedService {
+
+ private final List<HealthCheck> checks = new ArrayList<>();
+ private volatile CamelContext context;
+ private boolean enabled = true;
+
+ public KafkaHealthCheckRepository() {
+ }
+
+ @Override
+ public void setCamelContext(CamelContext camelContext) {
+ this.context = camelContext;
+ }
+
+ @Override
+ public String getId() {
+ return "camel-kafka";
+ }
+
+ @Override
+ public CamelContext getCamelContext() {
+ return context;
+ }
+
+ @Override
+ public boolean isEnabled() {
+ return enabled;
+ }
+
+ @Override
+ public void setEnabled(boolean enabled) {
+ this.enabled = enabled;
+ }
+
+ @Override
+ public Stream<HealthCheck> stream() {
+ return this.context != null && enabled
+ ? checks.stream()
+ : Stream.empty();
+ }
+
+ public void addHealthCheck(HealthCheck healthCheck) {
+ CamelContextAware.trySetCamelContext(healthCheck, getCamelContext());
+ this.checks.add(healthCheck);
+ }
+
+ public void removeHealthCheck(HealthCheck healthCheck) {
+ this.checks.remove(healthCheck);
+ }
+
+}
diff --git a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaProducer.java b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaProducer.java
index 43abcf9..8256697 100644
--- a/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaProducer.java
+++ b/components/camel-kafka/src/main/java/org/apache/camel/component/kafka/KafkaProducer.java
@@ -61,7 +61,7 @@ public class KafkaProducer extends DefaultAsyncProducer {
@SuppressWarnings("rawtypes")
private org.apache.kafka.clients.producer.Producer kafkaProducer;
- private KafkaProducerHealthCheck healthCheck;
+ private KafkaProducerHealthCheck producerHealthCheck;
private String clientId;
private final KafkaEndpoint endpoint;
private final KafkaConfiguration configuration;
@@ -178,19 +178,23 @@ public class KafkaProducer extends DefaultAsyncProducer {
// install producer health-check
HealthCheckRegistry hcr = getEndpoint().getCamelContext().getExtension(HealthCheckRegistry.class);
if (hcr != null) {
- healthCheck = new KafkaProducerHealthCheck(this, clientId);
- hcr.register(healthCheck);
+ producerHealthCheck = new KafkaProducerHealthCheck(this, clientId);
+ hcr.getRepository("camel-kafka").ifPresent(r -> {
+ KafkaHealthCheckRepository kr = (KafkaHealthCheckRepository) r;
+ kr.addHealthCheck(producerHealthCheck);
+ });
}
}
@Override
protected void doStop() throws Exception {
- if (healthCheck != null) {
- HealthCheckRegistry hcr = getEndpoint().getCamelContext().getExtension(HealthCheckRegistry.class);
- if (hcr != null) {
- hcr.unregister(healthCheck);
- }
- healthCheck = null;
+ HealthCheckRegistry hcr = endpoint.getCamelContext().getExtension(HealthCheckRegistry.class);
+ if (hcr != null) {
+ hcr.getRepository("camel-kafka").ifPresent(r -> {
+ KafkaHealthCheckRepository kr = (KafkaHealthCheckRepository) r;
+ kr.removeHealthCheck(producerHealthCheck);
+ });
+ producerHealthCheck = null;
}
if (kafkaProducer != null && closeKafkaProducer) {