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:03 UTC

[camel-examples] branch main updated: 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-examples.git


The following commit(s) were added to refs/heads/main by this push:
     new cb9586f  CAMEL-15133: camel-health - Resolve health-checks from classpath and make it friendlier to provide custom health checks.
cb9586f is described below

commit cb9586f2635af5739638e2a1867479fed8b9e8cc
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Nov 30 20:01:03 2021 +0100

    CAMEL-15133: camel-health - Resolve health-checks from classpath and make it friendlier to provide custom health checks.
---
 examples/main-health/pom.xml                       | 25 ++++++++++++++++-
 .../apache/camel/example/MonkeyHealthCheck.java    |  4 ++-
 .../org/apache/camel/example/MyApplication.java    |  3 ---
 .../org/apache/camel/example/MyConfiguration.java  | 31 ----------------------
 .../org/apache/camel/example/MyRouteBuilder.java   | 13 +++++----
 .../org/apache/camel/health-check/monkey-check     |  2 ++
 .../src/main/resources/application.properties      |  8 ++----
 7 files changed, 39 insertions(+), 47 deletions(-)

diff --git a/examples/main-health/pom.xml b/examples/main-health/pom.xml
index e2278e7..2355b06 100644
--- a/examples/main-health/pom.xml
+++ b/examples/main-health/pom.xml
@@ -17,7 +17,8 @@
     limitations under the License.
 
 -->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 
     <modelVersion>4.0.0</modelVersion>
 
@@ -94,6 +95,28 @@
 
     <build>
         <plugins>
+
+            <!-- to generate metadata for custom health-checks -->
+            <plugin>
+                <groupId>org.apache.camel</groupId>
+                <artifactId>camel-component-maven-plugin</artifactId>
+                <version>${camel.version}</version>
+                <configuration>
+                    <sourcesOutputDir>src/main/java</sourcesOutputDir>
+                    <resourcesOutputDir>src/main/resources</resourcesOutputDir>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>generate</id>
+                        <goals>
+                            <goal>generate</goal>
+                        </goals>
+                        <phase>process-classes</phase>
+                    </execution>
+                </executions>
+            </plugin>
+
+            <!-- to run via mvn camel:run -->
             <plugin>
                 <groupId>org.apache.camel</groupId>
                 <artifactId>camel-maven-plugin</artifactId>
diff --git a/examples/main-health/src/main/java/org/apache/camel/example/MonkeyHealthCheck.java b/examples/main-health/src/main/java/org/apache/camel/example/MonkeyHealthCheck.java
index f66ca76..ba6169e 100644
--- a/examples/main-health/src/main/java/org/apache/camel/example/MonkeyHealthCheck.java
+++ b/examples/main-health/src/main/java/org/apache/camel/example/MonkeyHealthCheck.java
@@ -20,6 +20,7 @@ import java.util.Map;
 
 import org.apache.camel.health.HealthCheckResultBuilder;
 import org.apache.camel.impl.health.AbstractHealthCheck;
+import org.apache.camel.spi.annotations.HealthCheck;
 
 /**
  * A chaos monkey health check that reports UP or DOWN in a chaotic way.
@@ -28,11 +29,12 @@ import org.apache.camel.impl.health.AbstractHealthCheck;
  * which is automatic discovered if bound in the {@link org.apache.camel.spi.Registry} and
  * used as part of Camel's health-check system.
  */
+@HealthCheck("monkey-check")
 public class MonkeyHealthCheck extends AbstractHealthCheck {
 
     private boolean up = true;
 
-    protected MonkeyHealthCheck() {
+    public MonkeyHealthCheck() {
         super("custom", "monkey");
     }
 
diff --git a/examples/main-health/src/main/java/org/apache/camel/example/MyApplication.java b/examples/main-health/src/main/java/org/apache/camel/example/MyApplication.java
index b6dce99..496b37d 100644
--- a/examples/main-health/src/main/java/org/apache/camel/example/MyApplication.java
+++ b/examples/main-health/src/main/java/org/apache/camel/example/MyApplication.java
@@ -29,9 +29,6 @@ public final class MyApplication {
     public static void main(String[] args) throws Exception {
         // use Camels Main class
         Main main = new Main();
-        // lets use a configuration class (you can specify multiple classes)
-        // (properties are automatic loaded from application.properties)
-        main.configure().addConfigurationClass(MyConfiguration.class);
         // and add the routes (you can specify multiple classes)
         main.configure().addRoutesBuilder(MyRouteBuilder.class);
         // now keep the application running until the JVM is terminated (ctrl + c or sigterm)
diff --git a/examples/main-health/src/main/java/org/apache/camel/example/MyConfiguration.java b/examples/main-health/src/main/java/org/apache/camel/example/MyConfiguration.java
deleted file mode 100644
index 6648f8e..0000000
--- a/examples/main-health/src/main/java/org/apache/camel/example/MyConfiguration.java
+++ /dev/null
@@ -1,31 +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.example;
-
-import org.apache.camel.BindToRegistry;
-
-/**
- * Class to configure the Camel application.
- */
-public class MyConfiguration {
-
-    @BindToRegistry
-    public MonkeyHealthCheck monkey() {
-        return new MonkeyHealthCheck();
-    }
-
-}
diff --git a/examples/main-health/src/main/java/org/apache/camel/example/MyRouteBuilder.java b/examples/main-health/src/main/java/org/apache/camel/example/MyRouteBuilder.java
index 997fd88..6eb457d 100644
--- a/examples/main-health/src/main/java/org/apache/camel/example/MyRouteBuilder.java
+++ b/examples/main-health/src/main/java/org/apache/camel/example/MyRouteBuilder.java
@@ -16,17 +16,20 @@
  */
 package org.apache.camel.example;
 
-import org.apache.camel.BeanInject;
+import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.health.HealthCheck;
+import org.apache.camel.health.HealthCheckResolver;
 
 public class MyRouteBuilder extends RouteBuilder {
 
-    // we can inject the bean via this annotation
-    @BeanInject("monkey")
-    MonkeyHealthCheck monkey;
-
     @Override
     public void configure() throws Exception {
+        // to trigger the health check to flip between UP and DOWN then lets call it as a Java bean from
+        // a route, and therefore we need to lookup and resolve the monkey-check
+        HealthCheckResolver resolver = getCamelContext().adapt(ExtendedCamelContext.class).getHealthCheckResolver();
+        final HealthCheck monkey = resolver.resolveHealthCheck("monkey");
+
         from("timer:foo?period={{myPeriod}}").routeId("timer")
             .bean(monkey, "chaos")
             .log("${body}");
diff --git a/examples/main-health/src/main/resources/META-INF/services/org/apache/camel/health-check/monkey-check b/examples/main-health/src/main/resources/META-INF/services/org/apache/camel/health-check/monkey-check
new file mode 100644
index 0000000..949d231
--- /dev/null
+++ b/examples/main-health/src/main/resources/META-INF/services/org/apache/camel/health-check/monkey-check
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.example.MonkeyHealthCheck
diff --git a/examples/main-health/src/main/resources/application.properties b/examples/main-health/src/main/resources/application.properties
index e796642..a4e51f1 100644
--- a/examples/main-health/src/main/resources/application.properties
+++ b/examples/main-health/src/main/resources/application.properties
@@ -19,12 +19,8 @@
 # here you can configure options on camel main (see MainConfigurationProperties class)
 camel.main.name = MyHealthyCamel
 
-# extended runtime statistics about bean introspection usage (java reflection)
-### camel.main.bean-introspection-extended-statistics=true
-### camel.main.bean-introspection-logging-level=INFO
-
-# enable JMX which allows to also control health check
-camel.main.jmx-enabled = true
+# turn on loading custom health-checks via classpath scanning
+camel.main.load-health-checks = true
 
 # enable supervised route controller which will startup routes in safe manner
 camel.main.route-controller-supervise-enabled = true