You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2023/03/17 12:03:41 UTC

[camel-quarkus] branch main updated: camel-quarkus-management: Added tests for managed beans and updated docs. Fixes #4661

This is an automated email from the ASF dual-hosted git repository.

jamesnetherton pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/main by this push:
     new aa852160bc camel-quarkus-management: Added tests for managed beans and updated docs. Fixes #4661
aa852160bc is described below

commit aa852160bc98db11a3d35731c4d69ecd3fa29c99
Author: Darren Coleman <dc...@redhat.com>
AuthorDate: Wed Jan 11 10:54:11 2023 +0000

    camel-quarkus-management: Added tests for managed beans and updated docs.
    Fixes #4661
---
 .../pages/reference/extensions/management.adoc     | 14 +++++
 .../management/runtime/src/main/doc/usage.adoc     |  9 ++++
 integration-tests-jvm/management/pom.xml           | 18 +++++++
 .../it/{Routes.java => ManagedCounter.java}        | 20 +++++--
 .../quarkus/component/management/it/Routes.java    | 10 +++-
 .../component/management/it/ManagementTest.java    | 61 +++++++++++++++++++++-
 6 files changed, 124 insertions(+), 8 deletions(-)

diff --git a/docs/modules/ROOT/pages/reference/extensions/management.adoc b/docs/modules/ROOT/pages/reference/extensions/management.adoc
index d6a2be531b..db055589d3 100644
--- a/docs/modules/ROOT/pages/reference/extensions/management.adoc
+++ b/docs/modules/ROOT/pages/reference/extensions/management.adoc
@@ -33,6 +33,20 @@ ifeval::[{doc-show-user-guide-link} == true]
 Check the xref:user-guide/index.adoc[User guide] for more information about writing Camel Quarkus applications.
 endif::[]
 
+[id="extensions-management-usage"]
+== Usage
+For information on using Managed Beans in Camel, consult the xref:manual::jmx.adoc[JMX section of the Camel Manual].
+
+[id="extensions-management-usage-enabling-and-disabling-jmx"]
+=== Enabling and Disabling JMX
+
+JMX can be enabled or disabled in Camel-Quarkus by any of the following methods:
+
+. Adding or removing the `camel-quarkus-management` extension.
+. Setting the `camel.main.jmxEnabled` configuration property to a boolean value.
+. Setting the system property `-Dorg.apache.camel.jmx.disabled` to a boolean value.
+
+
 [id="extensions-management-camel-quarkus-limitations"]
 == Camel Quarkus limitations
 
diff --git a/extensions-jvm/management/runtime/src/main/doc/usage.adoc b/extensions-jvm/management/runtime/src/main/doc/usage.adoc
new file mode 100644
index 0000000000..1f962b1fef
--- /dev/null
+++ b/extensions-jvm/management/runtime/src/main/doc/usage.adoc
@@ -0,0 +1,9 @@
+For information on using Managed Beans in Camel, consult the xref:manual::jmx.adoc[JMX section of the Camel Manual].
+
+=== Enabling and Disabling JMX
+
+JMX can be enabled or disabled in Camel-Quarkus by any of the following methods:
+
+. Adding or removing the `camel-quarkus-management` extension.
+. Setting the `camel.main.jmxEnabled` configuration property to a boolean value.
+. Setting the system property `-Dorg.apache.camel.jmx.disabled` to a boolean value.
\ No newline at end of file
diff --git a/integration-tests-jvm/management/pom.xml b/integration-tests-jvm/management/pom.xml
index a994d0154f..15b7dd6fe0 100644
--- a/integration-tests-jvm/management/pom.xml
+++ b/integration-tests-jvm/management/pom.xml
@@ -39,6 +39,10 @@
             <groupId>org.apache.camel.quarkus</groupId>
             <artifactId>camel-quarkus-management</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-jackson</artifactId>
+        </dependency>
 
         <!-- test dependencies -->
         <dependency>
@@ -46,6 +50,7 @@
             <artifactId>quarkus-junit5</artifactId>
             <scope>test</scope>
         </dependency>
+        
     </dependencies>
 
     <profiles>
@@ -71,6 +76,19 @@
                         </exclusion>
                     </exclusions>
                 </dependency>
+                <dependency>
+                    <groupId>org.apache.camel.quarkus</groupId>
+                    <artifactId>camel-quarkus-jackson-deployment</artifactId>
+                    <version>${project.version}</version>
+                    <type>pom</type>
+                    <scope>test</scope>
+                    <exclusions>
+                        <exclusion>
+                            <groupId>*</groupId>
+                            <artifactId>*</artifactId>
+                        </exclusion>
+                    </exclusions>
+                </dependency>
                 <dependency>
                     <groupId>org.apache.camel.quarkus</groupId>
                     <artifactId>camel-quarkus-management-deployment</artifactId>
diff --git a/integration-tests-jvm/management/src/main/java/org/apache/camel/quarkus/component/management/it/Routes.java b/integration-tests-jvm/management/src/main/java/org/apache/camel/quarkus/component/management/it/ManagedCounter.java
similarity index 64%
copy from integration-tests-jvm/management/src/main/java/org/apache/camel/quarkus/component/management/it/Routes.java
copy to integration-tests-jvm/management/src/main/java/org/apache/camel/quarkus/component/management/it/ManagedCounter.java
index e411a5a09e..cbf84327d1 100644
--- a/integration-tests-jvm/management/src/main/java/org/apache/camel/quarkus/component/management/it/Routes.java
+++ b/integration-tests-jvm/management/src/main/java/org/apache/camel/quarkus/component/management/it/ManagedCounter.java
@@ -16,11 +16,21 @@
  */
 package org.apache.camel.quarkus.component.management.it;
 
-import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.api.management.ManagedAttribute;
+import org.apache.camel.api.management.ManagedOperation;
+import org.apache.camel.api.management.ManagedResource;
 
-public class Routes extends RouteBuilder {
-    @Override
-    public void configure() throws Exception {
-        from("direct:start").setBody().constant("Hello World");
+@ManagedResource
+public class ManagedCounter {
+    private int count = 0;
+
+    @ManagedAttribute(description = "The count value")
+    public int getCount() {
+        return count;
+    }
+
+    @ManagedOperation(description = "Increment the counter")
+    public void increment() {
+        this.count++;
     }
 }
diff --git a/integration-tests-jvm/management/src/main/java/org/apache/camel/quarkus/component/management/it/Routes.java b/integration-tests-jvm/management/src/main/java/org/apache/camel/quarkus/component/management/it/Routes.java
index e411a5a09e..7f2aa5ece9 100644
--- a/integration-tests-jvm/management/src/main/java/org/apache/camel/quarkus/component/management/it/Routes.java
+++ b/integration-tests-jvm/management/src/main/java/org/apache/camel/quarkus/component/management/it/Routes.java
@@ -21,6 +21,14 @@ import org.apache.camel.builder.RouteBuilder;
 public class Routes extends RouteBuilder {
     @Override
     public void configure() throws Exception {
-        from("direct:start").setBody().constant("Hello World");
+        from("direct:start").routeId("hello").setBody().constant("Hello World");
+
+        from("direct:count").routeId("count")
+                .bean(ManagedCounter.class, "increment").id("counter");
+
+        // This route ensures that a dataformat is available in the context.
+        from("direct:dataformat").routeId("dataformat")
+                .setBody(constant("Hello"))
+                .marshal().json();
     }
 }
diff --git a/integration-tests-jvm/management/src/test/java/org/apache/camel/quarkus/component/management/it/ManagementTest.java b/integration-tests-jvm/management/src/test/java/org/apache/camel/quarkus/component/management/it/ManagementTest.java
index 70a1d2c319..b99cdb83bf 100644
--- a/integration-tests-jvm/management/src/test/java/org/apache/camel/quarkus/component/management/it/ManagementTest.java
+++ b/integration-tests-jvm/management/src/test/java/org/apache/camel/quarkus/component/management/it/ManagementTest.java
@@ -24,7 +24,13 @@ import javax.management.ObjectInstance;
 import javax.management.ObjectName;
 
 import io.quarkus.test.junit.QuarkusTest;
+import jakarta.inject.Inject;
+import org.apache.camel.CamelContext;
+import org.apache.camel.ProducerTemplate;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -32,9 +38,37 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 @QuarkusTest
 class ManagementTest {
 
+    private MBeanServer server;
+
+    @Inject
+    ProducerTemplate template;
+
+    @Inject
+    CamelContext camelContext;
+
+    @BeforeEach
+    public void setUp() {
+        server = ManagementFactory.getPlatformMBeanServer();
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = { "components", "consumers", "context", "dataformats", "endpoints", "processors", "routes",
+            "services" })
+    public void testManagementObjects(String type) throws Exception {
+        // Look up an object instance by type
+        ObjectName objectName = new ObjectName("org.apache.camel:type=" + type + ",*");
+        Set<ObjectInstance> mbeans = server.queryMBeans(objectName, null);
+        assertTrue(mbeans.size() > 0);
+
+        // The CamelId attribute is common to all managed Camel objects,
+        // and should match the name of the CamelContext.
+        ObjectInstance mbean = mbeans.iterator().next();
+        String camelId = (String) server.getAttribute(mbean.getObjectName(), "CamelId");
+        assertEquals(camelContext.getName(), camelId);
+    }
+
     @Test
-    public void testCamelManagement() throws Exception {
-        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
+    public void testDumpRoutesAsXml() throws Exception {
         ObjectName objectName = new ObjectName("org.apache.camel:type=context,*");
         Set<ObjectInstance> mbeans = server.queryMBeans(objectName, null);
         assertEquals(1, mbeans.size());
@@ -43,4 +77,27 @@ class ManagementTest {
         String routeXML = (String) server.invoke(instance.getObjectName(), "dumpRoutesAsXml", new Object[] {}, new String[] {});
         assertTrue(routeXML.contains("<from uri=\"direct:start\"/>"));
     }
+
+    @Test
+    public void testManagedBean() throws Exception {
+        ObjectName objectName = new ObjectName("org.apache.camel:type=processors,name=\"counter\",*");
+        Set<ObjectInstance> mbeans = server.queryMBeans(objectName, null);
+        assertEquals(1, mbeans.size());
+        ObjectInstance instance = mbeans.iterator().next();
+
+        // Counter should be initialized to 0
+        Integer count = (Integer) server.getAttribute(instance.getObjectName(), "Count");
+        assertEquals(0, count);
+
+        // Calling the increment() method should set counter to 1
+        server.invoke(instance.getObjectName(), "increment", new Object[] {}, new String[] {});
+        count = (Integer) server.getAttribute(instance.getObjectName(), "Count");
+        assertEquals(1, count);
+
+        // Call the "direct:count" endpoint to increment the counter
+        template.requestBody("direct:count", "");
+
+        count = (Integer) server.getAttribute(instance.getObjectName(), "Count");
+        assertEquals(2, count);
+    }
 }