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/10/31 14:39:48 UTC

[camel] branch main updated: camel-mp-fault-tolerance - Add dev console and Camel CLI command

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


The following commit(s) were added to refs/heads/main by this push:
     new e7fb04ba197 camel-mp-fault-tolerance - Add dev console and Camel CLI command
e7fb04ba197 is described below

commit e7fb04ba197ea6f2c50abaadedfc234390209517
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Oct 31 15:38:54 2022 +0100

    camel-mp-fault-tolerance - Add dev console and Camel CLI command
---
 .../camel-microprofile-fault-tolerance/pom.xml     |  4 +
 .../org/apache/camel/dev-console/fault-tolerance   |  2 +
 .../faulttolerance/FaultToleranceConsole.java      | 92 ++++++++++++++++++++++
 .../faulttolerance/FaultToleranceProcessor.java    | 15 ++++
 .../component/resilience4j/ResilienceConsole.java  | 16 ++++
 .../camel/cli/connector/LocalCliConnector.java     |  7 ++
 .../core/commands/process/ListCircuitBreaker.java  | 42 +++++++++-
 7 files changed, 175 insertions(+), 3 deletions(-)

diff --git a/components/camel-microprofile/camel-microprofile-fault-tolerance/pom.xml b/components/camel-microprofile/camel-microprofile-fault-tolerance/pom.xml
index 375108d1840..29de46657a5 100644
--- a/components/camel-microprofile/camel-microprofile-fault-tolerance/pom.xml
+++ b/components/camel-microprofile/camel-microprofile-fault-tolerance/pom.xml
@@ -45,6 +45,10 @@
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-core-reifier</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-console</artifactId>
+        </dependency>
 
         <dependency>
             <groupId>org.eclipse.microprofile.fault-tolerance</groupId>
diff --git a/components/camel-microprofile/camel-microprofile-fault-tolerance/src/generated/resources/META-INF/services/org/apache/camel/dev-console/fault-tolerance b/components/camel-microprofile/camel-microprofile-fault-tolerance/src/generated/resources/META-INF/services/org/apache/camel/dev-console/fault-tolerance
new file mode 100644
index 00000000000..ad90d68e7b0
--- /dev/null
+++ b/components/camel-microprofile/camel-microprofile-fault-tolerance/src/generated/resources/META-INF/services/org/apache/camel/dev-console/fault-tolerance
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.microprofile.faulttolerance.FaultToleranceConsole
diff --git a/components/camel-microprofile/camel-microprofile-fault-tolerance/src/main/java/org/apache/camel/component/microprofile/faulttolerance/FaultToleranceConsole.java b/components/camel-microprofile/camel-microprofile-fault-tolerance/src/main/java/org/apache/camel/component/microprofile/faulttolerance/FaultToleranceConsole.java
new file mode 100644
index 00000000000..b3d351c54a5
--- /dev/null
+++ b/components/camel-microprofile/camel-microprofile-fault-tolerance/src/main/java/org/apache/camel/component/microprofile/faulttolerance/FaultToleranceConsole.java
@@ -0,0 +1,92 @@
+/*
+ * 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.microprofile.faulttolerance;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.Processor;
+import org.apache.camel.Route;
+import org.apache.camel.impl.console.AbstractDevConsole;
+import org.apache.camel.spi.annotations.DevConsole;
+import org.apache.camel.util.json.JsonObject;
+
+@DevConsole("fault-tolerance")
+public class FaultToleranceConsole extends AbstractDevConsole {
+
+    public FaultToleranceConsole() {
+        super("camel", "fault-tolerance", "MicroProfile Fault Tolerance Circuit Breaker",
+              "Display circuit breaker information");
+    }
+
+    @Override
+    protected String doCallText(Map<String, Object> options) {
+        StringBuilder sb = new StringBuilder();
+
+        List<FaultToleranceProcessor> cbs = new ArrayList<>();
+        for (Route route : getCamelContext().getRoutes()) {
+            List<Processor> list = route.filter("*");
+            for (Processor p : list) {
+                if (p instanceof FaultToleranceProcessor) {
+                    cbs.add((FaultToleranceProcessor) p);
+                }
+            }
+        }
+        // sort by ids
+        cbs.sort(Comparator.comparing(FaultToleranceProcessor::getId));
+
+        for (FaultToleranceProcessor cb : cbs) {
+            String id = cb.getId();
+            String rid = cb.getRouteId();
+            String state = cb.getCircuitBreakerState();
+            sb.append(String.format("    %s/%s: %s\n", rid, id, state));
+        }
+
+        return sb.toString();
+    }
+
+    @Override
+    protected JsonObject doCallJson(Map<String, Object> options) {
+        JsonObject root = new JsonObject();
+
+        List<FaultToleranceProcessor> cbs = new ArrayList<>();
+        for (Route route : getCamelContext().getRoutes()) {
+            List<Processor> list = route.filter("*");
+            for (Processor p : list) {
+                if (p instanceof FaultToleranceProcessor) {
+                    cbs.add((FaultToleranceProcessor) p);
+                }
+            }
+        }
+        // sort by ids
+        cbs.sort(Comparator.comparing(FaultToleranceProcessor::getId));
+
+        final List<JsonObject> list = new ArrayList<>();
+        for (FaultToleranceProcessor cb : cbs) {
+            JsonObject jo = new JsonObject();
+            jo.put("id", cb.getId());
+            jo.put("routeId", cb.getRouteId());
+            jo.put("state", cb.getCircuitBreakerState());
+            list.add(jo);
+        }
+        root.put("circuitBreakers", list);
+
+        return root;
+    }
+}
diff --git a/components/camel-microprofile/camel-microprofile-fault-tolerance/src/main/java/org/apache/camel/component/microprofile/faulttolerance/FaultToleranceProcessor.java b/components/camel-microprofile/camel-microprofile-fault-tolerance/src/main/java/org/apache/camel/component/microprofile/faulttolerance/FaultToleranceProcessor.java
index 12b46714c15..10112f11714 100644
--- a/components/camel-microprofile/camel-microprofile-fault-tolerance/src/main/java/org/apache/camel/component/microprofile/faulttolerance/FaultToleranceProcessor.java
+++ b/components/camel-microprofile/camel-microprofile-fault-tolerance/src/main/java/org/apache/camel/component/microprofile/faulttolerance/FaultToleranceProcessor.java
@@ -205,6 +205,21 @@ public class FaultToleranceProcessor extends AsyncProcessorSupport
         return config.getBulkheadWaitingTaskQueue();
     }
 
+    @ManagedAttribute(description = "Returns the current state of the circuit breaker")
+    public String getCircuitBreakerState() {
+        if (circuitBreaker != null) {
+            int state = circuitBreaker.currentState();
+            if (state == 2) {
+                return "HALF_OPEN";
+            } else if (state == 1) {
+                return "OPEN";
+            } else {
+                return "CLOSED";
+            }
+        }
+        return null;
+    }
+
     @Override
     public List<Processor> next() {
         if (!hasNext()) {
diff --git a/components/camel-resilience4j/src/main/java/org/apache/camel/component/resilience4j/ResilienceConsole.java b/components/camel-resilience4j/src/main/java/org/apache/camel/component/resilience4j/ResilienceConsole.java
index 1e41d832ee2..6ce344eaf51 100644
--- a/components/camel-resilience4j/src/main/java/org/apache/camel/component/resilience4j/ResilienceConsole.java
+++ b/components/camel-resilience4j/src/main/java/org/apache/camel/component/resilience4j/ResilienceConsole.java
@@ -1,3 +1,19 @@
+/*
+ * 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.resilience4j;
 
 import java.util.ArrayList;
diff --git a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
index 6e1ba6d7cba..fc2117d0c2b 100644
--- a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
+++ b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
@@ -412,6 +412,13 @@ public class LocalCliConnector extends ServiceSupport implements CliConnector, C
                         root.put("resilience4j", json);
                     }
                 }
+                DevConsole dc11 = dcr.resolveById("fault-tolerance");
+                if (dc11 != null) {
+                    JsonObject json = (JsonObject) dc11.call(DevConsole.MediaType.JSON);
+                    if (json != null && !json.isEmpty()) {
+                        root.put("fault-tolerance", json);
+                    }
+                }
             }
             // various details
             JsonObject services = collectServices();
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListCircuitBreaker.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListCircuitBreaker.java
index 7da62408aaa..8dc0c8bfca1 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListCircuitBreaker.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListCircuitBreaker.java
@@ -91,6 +91,21 @@ public class ListCircuitBreaker extends ProcessBaseCommand {
                                 }
                             }
                         }
+                        mo = (JsonObject) root.get("fault-tolerance");
+                        if (mo != null) {
+                            JsonArray arr = (JsonArray) mo.get("circuitBreakers");
+                            if (arr != null) {
+                                for (int i = 0; i < arr.size(); i++) {
+                                    row = baseRow.copy();
+                                    JsonObject jo = (JsonObject) arr.get(i);
+                                    row.component = "camel-microprofile-fault-tolerance";
+                                    row.id = jo.getString("id");
+                                    row.routeId = jo.getString("routeId");
+                                    row.state = jo.getString("state");
+                                    rows.add(row);
+                                }
+                            }
+                        }
                     }
                 });
 
@@ -107,13 +122,13 @@ public class ListCircuitBreaker extends ProcessBaseCommand {
                     new Column().header("ID").dataAlign(HorizontalAlign.LEFT).with(r -> r.id),
                     new Column().header("STATE").dataAlign(HorizontalAlign.LEFT).with(r -> r.state),
                     new Column().header("PENDING").headerAlign(HorizontalAlign.RIGHT).dataAlign(HorizontalAlign.RIGHT)
-                            .with(r -> "" + r.bufferedCalls),
+                            .with(this::getPending),
                     new Column().header("SUCCESS").headerAlign(HorizontalAlign.RIGHT).dataAlign(HorizontalAlign.RIGHT)
-                            .with(r -> "" + r.successfulCalls),
+                            .with(this::getSuccess),
                     new Column().header("FAIL").headerAlign(HorizontalAlign.CENTER).dataAlign(HorizontalAlign.RIGHT)
                             .with(this::getFailure),
                     new Column().header("REJECT").headerAlign(HorizontalAlign.RIGHT).dataAlign(HorizontalAlign.RIGHT)
-                            .with(r -> "" + r.notPermittedCalls))));
+                            .with(this::getReject))));
         }
 
         return 0;
@@ -148,6 +163,27 @@ public class ListCircuitBreaker extends ProcessBaseCommand {
         }
     }
 
+    private String getPending(Row r) {
+        if ("camel-resilience4j".equals(r.component)) {
+            return "" + r.bufferedCalls;
+        }
+        return "";
+    }
+
+    private String getSuccess(Row r) {
+        if ("camel-resilience4j".equals(r.component)) {
+            return "" + r.successfulCalls;
+        }
+        return "";
+    }
+
+    private String getReject(Row r) {
+        if ("camel-resilience4j".equals(r.component)) {
+            return "" + r.notPermittedCalls;
+        }
+        return "";
+    }
+
     private static class Row implements Cloneable {
         String pid;
         String name;