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/08/20 16:01:13 UTC

[camel] 02/02: CAMEL-18412: camel-jbang - Add support for hawtio

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

commit 9bbd14f39b3602dd3df2e0e3e334f39d84f73486
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Aug 20 18:00:46 2022 +0200

    CAMEL-18412: camel-jbang - Add support for hawtio
---
 .../modules/ROOT/pages/camel-jbang.adoc            | 27 +++++++++++++-
 .../dsl/jbang/core/commands/process/Hawtio.java    | 43 ++++++++++++++++++++--
 .../dsl/jbang/core/commands/process/Jolokia.java   | 14 +++++--
 .../jbang/core/commands/process/ListProcess.java   |  2 +-
 .../jbang/core/commands/process/StopProcess.java   |  4 +-
 5 files changed, 79 insertions(+), 11 deletions(-)

diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
index 9fd0731765d..fbd9e01eb92 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
@@ -568,8 +568,8 @@ as all the JMX management information, and not but least to visualize the Camel
 with live performance metrics. Hawtio is a handy tool for many years, and we have made it
 easy to use Hawtio with Camel JBang.
 
-To let Hawtio able to inspect the Camel integrations, then at first you need to attach
-the Jolokia JVM Agent in the running integration, this can be done as follows:
+To let Hawtio able to inspect the Camel integrations, then the Jolokia JVM Agent
+must be installed in the running integration, this can be done, either explicit as follows:
 
 [source,bash]
 ----
@@ -587,6 +587,17 @@ Started Jolokia for PID 37928
 http://127.0.0.1:8778/jolokia/
 ----
 
+Instead of using PID you can also attach by name pattern. In this example because the
+two Camel integrations have unique names (foo and dude), then you can also attach Jolokia
+without knowing the PID as follows:
+
+[source,bash]
+----
+camel jolokia du
+Started Jolokia for PID 44805
+http://127.0.0.1:8778/jolokia/
+----
+
 Then you can launch https://hawt.io/[Hawtio] using Camel JBang:
 
 [source,bash]
@@ -612,6 +623,18 @@ camel jolokia 37928 --stop
 Stopped Jolokia for PID 37928
 ----
 
+It is also possible to do this with only one command, as follows:
+
+[source,bash]
+----
+camel hawtio dude
+----
+
+Where _dude_ is the name of the running Camel integration. When you stop Hawtio (using `ctrl` + `c`)
+then Camel will attempt to uninstall the Jolokia JVM Agent, however this may not be
+able to do this always, because the JVM is being terminated which can prevent camel-jbang
+from doing JVM process communication to the running Camel integration.
+
 === Scripting from terminal using pipes
 
 You can also execute a Camel JBang file as a script that can be used for terminal scripting with pipes and filters.
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/Hawtio.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/Hawtio.java
index a27c8483927..928295ec0d0 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/Hawtio.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/Hawtio.java
@@ -33,20 +33,24 @@ import picocli.CommandLine.Command;
 @Command(name = "hawtio", description = "Launch Hawtio web console")
 public class Hawtio extends CamelCommand {
 
+    @CommandLine.Parameters(description = "Name or pid of running Camel integration", arity = "1")
+    String name;
+
     @CommandLine.Option(names = { "--version" },
                         description = "Version of the Hawtio web console", defaultValue = "2.15.0")
-    private String version = "2.15.0";
+    String version = "2.15.0";
 
     // use port 8888 as 8080 is too commonly used
     @CommandLine.Option(names = { "--port" },
                         description = "Port number to use for Hawtio web console", defaultValue = "8888")
-    private int port = 8888;
+    int port = 8888;
 
     @CommandLine.Option(names = { "--openUrl" },
                         description = "To automatic open Hawtio web console in the web browser", defaultValue = "true")
-    private boolean openUrl = true;
+    boolean openUrl = true;
 
     private final CountDownLatch shutdownLatch = new CountDownLatch(1);
+    private volatile long pid;
 
     public Hawtio(CamelJBangMain main) {
         super(main);
@@ -54,6 +58,39 @@ public class Hawtio extends CamelCommand {
 
     @Override
     public Integer call() throws Exception {
+        int exit;
+        if (name == null) {
+            exit = callHawtio();
+        } else {
+            // attach jolokia before calling hawtio and disconnect afterwards
+            try {
+                exit = connectJolokia();
+                if (exit == 0) {
+                    exit = callHawtio();
+                }
+            } finally {
+                disconnectJolokia();
+            }
+        }
+        return exit;
+    }
+
+    protected Integer connectJolokia() throws Exception {
+        Jolokia jolokia = new Jolokia(getMain());
+        jolokia.name = name;
+        int exit = jolokia.call();
+        this.pid = jolokia.getPid();
+        return exit;
+    }
+
+    protected void disconnectJolokia() throws Exception {
+        Jolokia jolokia = new Jolokia(getMain());
+        jolokia.name = "" + pid;
+        jolokia.stop = true;
+        jolokia.call();
+    }
+
+    protected Integer callHawtio() throws Exception {
         ClassLoader cl = createClassLoader();
 
         MavenDependencyDownloader downloader = new MavenDependencyDownloader();
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/Jolokia.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/Jolokia.java
index a1316835857..5a278726f4a 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/Jolokia.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/Jolokia.java
@@ -30,11 +30,13 @@ import picocli.CommandLine.Command;
 public class Jolokia extends ProcessBaseCommand {
 
     @CommandLine.Parameters(description = "Name or pid of running Camel integration", arity = "1")
-    private String name;
+    String name;
 
     @CommandLine.Option(names = { "--stop" },
                         description = "Stops the Jolokia JVM Agent in the running Camel integration")
-    private boolean stop;
+    boolean stop;
+
+    private volatile long pid;
 
     public Jolokia(CamelJBangMain main) {
         super(main);
@@ -51,7 +53,7 @@ public class Jolokia extends ProcessBaseCommand {
             return 0;
         }
 
-        long pid = pids.get(0);
+        this.pid = pids.get(0);
         int exitCode;
         try {
             OptionsAndArgs options;
@@ -81,4 +83,10 @@ public class Jolokia extends ProcessBaseCommand {
         return exitCode;
     }
 
+    /**
+     * The pid of the running Camel integration that was discovered and used
+     */
+    long getPid() {
+        return pid;
+    }
 }
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListProcess.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListProcess.java
index 6579e9b4368..6167614cc0a 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListProcess.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListProcess.java
@@ -27,7 +27,7 @@ public class ListProcess extends ProcessBaseCommand {
 
     @CommandLine.Option(names = { "--sort" },
                         description = "Sort by pid, name or age", defaultValue = "pid")
-    private String sort;
+    String sort;
 
     public ListProcess(CamelJBangMain main) {
         super(main);
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/StopProcess.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/StopProcess.java
index e04d613920f..8444780101a 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/StopProcess.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/StopProcess.java
@@ -28,11 +28,11 @@ import picocli.CommandLine.Command;
 public class StopProcess extends ProcessBaseCommand {
 
     @CommandLine.Parameters(description = "Name or pid of running Camel integration", arity = "0..1")
-    private String name;
+    String name;
 
     @CommandLine.Option(names = { "--all" },
                         description = "To stop all running Camel integrations")
-    private boolean all;
+    boolean all;
 
     public StopProcess(CamelJBangMain main) {
         super(main);