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 ff25b04f0f28f29b8688ece39a6b691a8fdd698e Author: Andrea Cosentino <[email protected]> AuthorDate: Wed Feb 1 13:34:08 2023 +0100 CAMEL-18131 - camel-health - Add health checks for components that has extension for connectivity verification - AWS MQ Signed-off-by: Andrea Cosentino <[email protected]> --- components/camel-aws/camel-aws2-mq/pom.xml | 4 ++ .../component/aws2/mq/MQ2ClientHealthCheck.java | 77 ++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/components/camel-aws/camel-aws2-mq/pom.xml b/components/camel-aws/camel-aws2-mq/pom.xml index 4d68aa8eefb..f96370116f7 100644 --- a/components/camel-aws/camel-aws2-mq/pom.xml +++ b/components/camel-aws/camel-aws2-mq/pom.xml @@ -62,5 +62,9 @@ <artifactId>camel-core-catalog</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-health</artifactId> + </dependency> </dependencies> </project> diff --git a/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/MQ2ClientHealthCheck.java b/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/MQ2ClientHealthCheck.java new file mode 100644 index 00000000000..c2e1a44672f --- /dev/null +++ b/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/MQ2ClientHealthCheck.java @@ -0,0 +1,77 @@ +/* + * 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.mq; + +import org.apache.camel.health.HealthCheckResultBuilder; +import org.apache.camel.impl.health.AbstractHealthCheck; +import org.apache.camel.util.ObjectHelper; +import software.amazon.awssdk.awscore.exception.AwsServiceException; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.mq.MqClient; +import software.amazon.awssdk.services.mq.model.ListBrokersRequest; + +import java.util.Map; + +public class MQ2ClientHealthCheck extends AbstractHealthCheck { + + private final MQ2Endpoint mq2Endpoint; + + public MQ2ClientHealthCheck(MQ2Endpoint mq2Endpoint, String clientId) { + super("camel", "aws2-mq-client-" + clientId); + this.mq2Endpoint = mq2Endpoint; + } + + @Override + public boolean isLiveness() { + // this health check is only readiness + return false; + } + + @Override + protected void doCall(HealthCheckResultBuilder builder, Map<String, Object> options) { + MQ2Configuration configuration = mq2Endpoint.getConfiguration(); + if (ObjectHelper.isNotEmpty(configuration.getRegion())) { + if (!MqClient.serviceMetadata().regions().contains(Region.of(configuration.getRegion()))) { + builder.message("The service is not supported in this region"); + builder.down(); + return; + } + } + try { + MqClient mqClient = mq2Endpoint.getAmazonMqClient(); + + mqClient.listBrokers(ListBrokersRequest.builder().maxResults(1).build()); + } catch (AwsServiceException e) { + builder.message(e.getMessage()); + builder.error(e); + if (ObjectHelper.isNotEmpty(e.statusCode())) { + builder.detail(SERVICE_STATUS_CODE, e.statusCode()); + } + if (ObjectHelper.isNotEmpty(e.awsErrorDetails().errorCode())) { + builder.detail(SERVICE_ERROR_CODE, e.awsErrorDetails().errorCode()); + } + builder.down(); + return; + } catch (Exception e) { + builder.error(e); + builder.down(); + return; + } + builder.up(); + } +}
