You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2022/09/09 13:01:56 UTC

[camel] 01/06: CAMEL-18466 - Camel-AWS2-SQS: Move from ComponentVerifierExtension to HealtCheck

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit a7f912c70bf5a7defdbef2c6073e61588c32e0d3
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Fri Sep 9 14:39:01 2022 +0200

    CAMEL-18466 - Camel-AWS2-SQS: Move from ComponentVerifierExtension to HealtCheck
---
 components/camel-aws/camel-aws2-sqs/pom.xml        |  4 +
 .../camel/component/aws2/sqs/Sqs2Component.java    |  2 -
 .../aws2/sqs/Sqs2ComponentVerifierExtension.java   | 94 ----------------------
 .../camel/component/aws2/sqs/Sqs2Consumer.java     | 12 +++
 .../aws2/sqs/Sqs2ConsumerHealthCheck.java          | 87 ++++++++++++++++++++
 .../aws2/sqs/Sqs2HealthCheckRepository.java        | 83 +++++++++++++++++++
 6 files changed, 186 insertions(+), 96 deletions(-)

diff --git a/components/camel-aws/camel-aws2-sqs/pom.xml b/components/camel-aws/camel-aws2-sqs/pom.xml
index db2b494cc5a..674da30bde3 100644
--- a/components/camel-aws/camel-aws2-sqs/pom.xml
+++ b/components/camel-aws/camel-aws2-sqs/pom.xml
@@ -54,6 +54,10 @@
             <groupId>commons-io</groupId>
             <artifactId>commons-io</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-health</artifactId>
+        </dependency>
 
         <!-- test infra -->
         <dependency>
diff --git a/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Component.java b/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Component.java
index c55c45f31d1..ea497fd7f34 100644
--- a/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Component.java
+++ b/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Component.java
@@ -44,8 +44,6 @@ public class Sqs2Component extends DefaultComponent {
 
     public Sqs2Component(CamelContext context) {
         super(context);
-
-        registerExtension(new Sqs2ComponentVerifierExtension());
     }
 
     @Override
diff --git a/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2ComponentVerifierExtension.java b/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2ComponentVerifierExtension.java
deleted file mode 100644
index 3ff8399c6bb..00000000000
--- a/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2ComponentVerifierExtension.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * 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.aws2.sqs;
-
-import java.util.Map;
-
-import org.apache.camel.component.extension.verifier.DefaultComponentVerifierExtension;
-import org.apache.camel.component.extension.verifier.ResultBuilder;
-import org.apache.camel.component.extension.verifier.ResultErrorBuilder;
-import org.apache.camel.component.extension.verifier.ResultErrorHelper;
-import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
-import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
-import software.amazon.awssdk.core.exception.SdkClientException;
-import software.amazon.awssdk.regions.Region;
-import software.amazon.awssdk.services.sqs.SqsClient;
-import software.amazon.awssdk.services.sqs.SqsClientBuilder;
-
-public class Sqs2ComponentVerifierExtension extends DefaultComponentVerifierExtension {
-
-    public Sqs2ComponentVerifierExtension() {
-        this("aws2-sqs");
-    }
-
-    public Sqs2ComponentVerifierExtension(String scheme) {
-        super(scheme);
-    }
-
-    // *********************************
-    // Parameters validation
-    // *********************************
-
-    @Override
-    protected Result verifyParameters(Map<String, Object> parameters) {
-
-        ResultBuilder builder = ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.PARAMETERS)
-                .error(ResultErrorHelper.requiresOption("accessKey", parameters))
-                .error(ResultErrorHelper.requiresOption("secretKey", parameters))
-                .error(ResultErrorHelper.requiresOption("region", parameters));
-
-        // Validate using the catalog
-
-        super.verifyParametersAgainstCatalog(builder, parameters);
-
-        return builder.build();
-    }
-
-    // *********************************
-    // Connectivity validation
-    // *********************************
-
-    @Override
-    protected Result verifyConnectivity(Map<String, Object> parameters) {
-        ResultBuilder builder = ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.CONNECTIVITY);
-
-        try {
-            Sqs2Configuration configuration = setProperties(new Sqs2Configuration(), parameters);
-            if (!SqsClient.serviceMetadata().regions().contains(Region.of(configuration.getRegion()))) {
-                ResultErrorBuilder errorBuilder = ResultErrorBuilder.withCodeAndDescription(
-                        VerificationError.StandardCode.ILLEGAL_PARAMETER, "The service is not supported in this region");
-                return builder.error(errorBuilder.build()).build();
-            }
-            AwsBasicCredentials cred = AwsBasicCredentials.create(configuration.getAccessKey(), configuration.getSecretKey());
-            SqsClientBuilder clientBuilder = SqsClient.builder();
-            SqsClient client = clientBuilder.credentialsProvider(StaticCredentialsProvider.create(cred))
-                    .region(Region.of(configuration.getRegion())).build();
-            client.listQueues();
-        } catch (SdkClientException e) {
-            ResultErrorBuilder errorBuilder
-                    = ResultErrorBuilder.withCodeAndDescription(VerificationError.StandardCode.AUTHENTICATION, e.getMessage())
-                            .detail("aws_sqs_exception_message", e.getMessage())
-                            .detail(VerificationError.ExceptionAttribute.EXCEPTION_CLASS, e.getClass().getName())
-                            .detail(VerificationError.ExceptionAttribute.EXCEPTION_INSTANCE, e);
-
-            builder.error(errorBuilder.build());
-        } catch (Exception e) {
-            builder.error(ResultErrorBuilder.withException(e).build());
-        }
-        return builder.build();
-    }
-}
diff --git a/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Consumer.java b/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Consumer.java
index feb6c219653..768076f86ee 100644
--- a/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Consumer.java
+++ b/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Consumer.java
@@ -34,6 +34,7 @@ import org.apache.camel.ExchangePropertyKey;
 import org.apache.camel.ExtendedExchange;
 import org.apache.camel.Message;
 import org.apache.camel.Processor;
+import org.apache.camel.health.HealthCheckHelper;
 import org.apache.camel.spi.HeaderFilterStrategy;
 import org.apache.camel.spi.ScheduledPollConsumerScheduler;
 import org.apache.camel.spi.Synchronization;
@@ -69,6 +70,8 @@ public class Sqs2Consumer extends ScheduledBatchPollingConsumer {
     private transient String sqsConsumerToString;
     private Collection<String> attributeNames;
     private Collection<String> messageAttributeNames;
+    private Sqs2HealthCheckRepository healthCheckRepository;
+    private Sqs2ConsumerHealthCheck consumerHealthCheck;
 
     public Sqs2Consumer(Sqs2Endpoint endpoint, Processor processor) {
         super(endpoint, processor);
@@ -351,6 +354,15 @@ public class Sqs2Consumer extends ScheduledBatchPollingConsumer {
         }
 
         super.doStart();
+
+        healthCheckRepository = HealthCheckHelper.getHealthCheckRepository(getEndpoint().getCamelContext(),
+                Sqs2HealthCheckRepository.REPOSITORY_ID, Sqs2HealthCheckRepository.class);
+
+        if (healthCheckRepository != null) {
+            consumerHealthCheck = new Sqs2ConsumerHealthCheck(this, getRouteId());
+            healthCheckRepository.addHealthCheck(consumerHealthCheck);
+        }
+
     }
 
     @Override
diff --git a/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2ConsumerHealthCheck.java b/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2ConsumerHealthCheck.java
new file mode 100644
index 00000000000..9aba1d38040
--- /dev/null
+++ b/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2ConsumerHealthCheck.java
@@ -0,0 +1,87 @@
+/*
+ * 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.aws2.sqs;
+
+import org.apache.camel.health.HealthCheckResultBuilder;
+import org.apache.camel.impl.health.AbstractHealthCheck;
+import org.apache.camel.util.ObjectHelper;
+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.sqs.SqsClient;
+import software.amazon.awssdk.services.sqs.SqsClientBuilder;
+
+import java.util.Map;
+
+public class Sqs2ConsumerHealthCheck extends AbstractHealthCheck {
+
+    private final Sqs2Consumer sqs2Consumer;
+    private final String routeId;
+
+    public Sqs2ConsumerHealthCheck(Sqs2Consumer sqs2Consumer, String routeId) {
+        super("camel", "aws2-sqs-consumer-" + routeId);
+        this.sqs2Consumer = sqs2Consumer;
+        this.routeId = routeId;
+    }
+
+    @Override
+    public boolean isLiveness() {
+        // this health check is only readiness
+        return false;
+    }
+
+    @Override
+    protected void doCall(HealthCheckResultBuilder builder, Map<String, Object> options) {
+
+        try {
+            Sqs2Configuration configuration = sqs2Consumer.getConfiguration();
+            if (!SqsClient.serviceMetadata().regions().contains(Region.of(configuration.getRegion()))) {
+                builder.message("The service is not supported in this region");
+                builder.down();
+                return;
+            }
+            SqsClient client;
+            if (!configuration.isUseDefaultCredentialsProvider()) {
+                AwsBasicCredentials cred
+                        = AwsBasicCredentials.create(configuration.getAccessKey(), configuration.getSecretKey());
+                SqsClientBuilder clientBuilder = SqsClient.builder();
+                client = clientBuilder.credentialsProvider(StaticCredentialsProvider.create(cred))
+                        .region(Region.of(configuration.getRegion())).build();
+            } else if (ObjectHelper.isNotEmpty(configuration.getAmazonSQSClient())) {
+                client = configuration.getAmazonSQSClient();
+            } else {
+                SqsClientBuilder clientBuilder = SqsClient.builder();
+                client = clientBuilder.region(Region.of(configuration.getRegion())).build();
+            }
+            client.listQueues();
+        } catch (SdkClientException e) {
+            builder.message(e.getMessage());
+            builder.error(e);
+            builder.down();
+            return;
+
+        } catch (Exception e) {
+            builder.error(e);
+            builder.down();
+            return;
+        }
+
+        builder.up();
+
+    }
+}
diff --git a/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2HealthCheckRepository.java b/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2HealthCheckRepository.java
new file mode 100644
index 00000000000..ea71485af1f
--- /dev/null
+++ b/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2HealthCheckRepository.java
@@ -0,0 +1,83 @@
+/*
+ * 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.aws2.sqs;
+
+import org.apache.camel.*;
+import org.apache.camel.health.HealthCheck;
+import org.apache.camel.health.HealthCheckRepository;
+import org.apache.camel.support.service.ServiceSupport;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.stream.Stream;
+
+/**
+ * Repository for camel-aws2-sqs {@link HealthCheck}s.
+ */
+@org.apache.camel.spi.annotations.HealthCheck("camel-aws2-sqs-repository")
+@DeferredContextBinding
+public class Sqs2HealthCheckRepository extends ServiceSupport
+        implements CamelContextAware, HealthCheckRepository, StaticService, NonManagedService {
+
+    public static final String REPOSITORY_ID = "camel-aws2-sqs";
+
+    private final List<HealthCheck> checks = new CopyOnWriteArrayList<>();
+    private volatile CamelContext context;
+    private boolean enabled = true;
+
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.context = camelContext;
+    }
+
+    @Override
+    public String getId() {
+        return REPOSITORY_ID;
+    }
+
+    @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);
+    }
+
+}