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 2023/08/15 13:00:43 UTC

[camel] branch main updated: CAMEL-19740: camel-stub - Add dev console (#11110)

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 a313b98598d CAMEL-19740: camel-stub - Add dev console (#11110)
a313b98598d is described below

commit a313b98598daeb55d53896317eecce5efe7ce6b5
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Aug 15 15:00:34 2023 +0200

    CAMEL-19740: camel-stub - Add dev console (#11110)
    
    CAMEL-19740: camel-stub - Add dev console
---
 .../apache/camel/component/seda/SedaComponent.java |   1 +
 .../apache/camel/component/seda/SedaEndpoint.java  |   9 ++
 .../services/org/apache/camel/dev-console/stub     |   2 +
 .../apache/camel/component/stub/StubConsole.java   | 137 +++++++++++++++++++++
 4 files changed, 149 insertions(+)

diff --git a/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaComponent.java b/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaComponent.java
index 13c1963eda5..3fc483236d6 100644
--- a/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaComponent.java
+++ b/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaComponent.java
@@ -241,6 +241,7 @@ public class SedaComponent extends DefaultComponent {
         } else {
             answer = createEndpoint(uri, this, queue, consumers);
         }
+        answer.setName(remaining);
 
         // if blockWhenFull is set on endpoint, defaultBlockWhenFull is ignored.
         boolean blockWhenFull = getAndRemoveParameter(parameters, "blockWhenFull", Boolean.class, defaultBlockWhenFull);
diff --git a/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaEndpoint.java b/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaEndpoint.java
index f1934eb03e1..1131e44a68a 100644
--- a/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaEndpoint.java
+++ b/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaEndpoint.java
@@ -274,6 +274,15 @@ public class SedaEndpoint extends DefaultEndpoint implements AsyncEndpoint, Brow
         }
     }
 
+    void setName(String name) {
+        this.name = name;
+    }
+
+    @ManagedAttribute(description = "Queue name")
+    public String getName() {
+        return name;
+    }
+
     /**
      * Define the queue instance which will be used by the endpoint.
      * <p/>
diff --git a/components/camel-stub/src/generated/resources/META-INF/services/org/apache/camel/dev-console/stub b/components/camel-stub/src/generated/resources/META-INF/services/org/apache/camel/dev-console/stub
new file mode 100644
index 00000000000..73eaf87a448
--- /dev/null
+++ b/components/camel-stub/src/generated/resources/META-INF/services/org/apache/camel/dev-console/stub
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.stub.StubConsole
diff --git a/components/camel-stub/src/main/java/org/apache/camel/component/stub/StubConsole.java b/components/camel-stub/src/main/java/org/apache/camel/component/stub/StubConsole.java
new file mode 100644
index 00000000000..0e5430d7b76
--- /dev/null
+++ b/components/camel-stub/src/main/java/org/apache/camel/component/stub/StubConsole.java
@@ -0,0 +1,137 @@
+/*
+ * 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.stub;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Queue;
+import java.util.Set;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.annotations.DevConsole;
+import org.apache.camel.support.MessageHelper;
+import org.apache.camel.support.console.AbstractDevConsole;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+
+@DevConsole("stub")
+public class StubConsole extends AbstractDevConsole {
+
+    /**
+     * To use either xml or json output format
+     */
+    public static final String FORMAT = "format";
+
+    public StubConsole() {
+        super("camel", "stub", "Stub", "Browse messages on stub");
+    }
+
+    @Override
+    protected String doCallText(Map<String, Object> options) {
+        StringBuilder sb = new StringBuilder();
+
+        StubComponent sc = getCamelContext().getComponent("stub", StubComponent.class);
+
+        List<StubEndpoint> list = getCamelContext().getEndpoints()
+                .stream().filter(e -> e instanceof StubEndpoint)
+                .map(StubEndpoint.class::cast)
+                .toList();
+
+        Set<String> names = new HashSet<>();
+        for (StubEndpoint se : list) {
+            String name = se.getName();
+            if (names.contains(name)) {
+                // queue may be shared between endpoints so only print once
+                continue;
+            } else {
+                names.add(name);
+            }
+
+            sb.append(String.format("Queue: %s (max: %d, size: %d)%n", name, se.getSize(), se.getCurrentQueueSize()));
+
+            // browse messages
+            Queue<Exchange> q = se.getQueue();
+            for (Exchange exchange : q) {
+                // dump to xml or json
+                try {
+                    String format = (String) options.get(FORMAT);
+                    String dump = null;
+                    if (format == null || "xml".equals(format)) {
+                        dump = MessageHelper.dumpAsXml(exchange.getMessage(), true, 4);
+                    } else if ("json".equals(format)) {
+                        dump = MessageHelper.dumpAsJSon(exchange.getMessage(), true, 4);
+                    }
+                    if (dump != null) {
+                        sb.append("\n").append(dump).append("\n");
+                    }
+                } catch (Exception e) {
+                    // ignore
+                }
+            }
+        }
+
+        return sb.toString();
+    }
+
+    @Override
+    protected JsonObject doCallJson(Map<String, Object> options) {
+        JsonObject root = new JsonObject();
+        JsonArray queues = new JsonArray();
+
+        List<StubEndpoint> list = getCamelContext().getEndpoints()
+                .stream().filter(e -> e instanceof StubEndpoint)
+                .map(StubEndpoint.class::cast)
+                .toList();
+
+        Set<String> names = new HashSet<>();
+        for (StubEndpoint se : list) {
+            String name = se.getName();
+            if (names.contains(name)) {
+                // queue may be shared between endpoints so only print once
+                continue;
+            } else {
+                names.add(name);
+            }
+
+            JsonObject jo = new JsonObject();
+            jo.put("name", name);
+            jo.put("max", se.getSize());
+            jo.put("size", se.getCurrentQueueSize());
+            List<JsonObject> arr = new ArrayList<>();
+            Queue<Exchange> q = se.getQueue();
+            for (Exchange exchange : q) {
+                try {
+                    JsonObject dump
+                            = MessageHelper.dumpAsJSonObject(exchange.getMessage(), false, true, true, false, true, 128 * 1024);
+                    arr.add(dump);
+                } catch (Exception e) {
+                    // ignore
+                }
+            }
+            if (!arr.isEmpty()) {
+                jo.put("messages", arr);
+            }
+            queues.add(jo);
+        }
+
+        root.put("queues", queues);
+        return root;
+    }
+
+}