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/06/12 08:39:36 UTC

[camel] 02/02: CAMEL-18597: ThrottlingExceptionRoutePolicy should expose exceptions type names in JMX

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 9685546e16e7870e0dc5aea0188ebdd6cab7cbd8
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jun 12 10:39:14 2023 +0200

    CAMEL-18597: ThrottlingExceptionRoutePolicy should expose exceptions type names in JMX
---
 .../mbean/ManagedThrottlingExceptionRoutePolicyMBean.java   |  4 ++++
 .../mbean/ManagedThrottlingExceptionRoutePolicy.java        | 13 +++++++++++++
 .../ManagedThrottlingExceptionRoutePolicyTest.java          | 13 +++++++++++--
 .../camel/throttling/ThrottlingExceptionRoutePolicy.java    |  4 ++++
 4 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedThrottlingExceptionRoutePolicyMBean.java b/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedThrottlingExceptionRoutePolicyMBean.java
index b0acd368691..f6899cf5ea4 100644
--- a/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedThrottlingExceptionRoutePolicyMBean.java
+++ b/core/camel-management-api/src/main/java/org/apache/camel/api/management/mbean/ManagedThrottlingExceptionRoutePolicyMBean.java
@@ -21,6 +21,9 @@ import org.apache.camel.api.management.ManagedOperation;
 
 public interface ManagedThrottlingExceptionRoutePolicyMBean extends ManagedServiceMBean {
 
+    @ManagedAttribute(description = "The class of the exception to throttle")
+    String[] getExceptionTypes();
+
     @ManagedAttribute(description = "How long to wait before moving open circuit to half open")
     Long getHalfOpenAfter();
 
@@ -53,4 +56,5 @@ public interface ManagedThrottlingExceptionRoutePolicyMBean extends ManagedServi
 
     @ManagedAttribute(description = "Number ms since the circuit was opened")
     Long getOpenAt();
+
 }
diff --git a/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedThrottlingExceptionRoutePolicy.java b/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedThrottlingExceptionRoutePolicy.java
index 3fa4cd2ee6a..3cd4cd91f37 100644
--- a/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedThrottlingExceptionRoutePolicy.java
+++ b/core/camel-management/src/main/java/org/apache/camel/management/mbean/ManagedThrottlingExceptionRoutePolicy.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.management.mbean;
 
+import java.util.List;
+
 import org.apache.camel.CamelContext;
 import org.apache.camel.api.management.ManagedResource;
 import org.apache.camel.api.management.mbean.ManagedThrottlingExceptionRoutePolicyMBean;
@@ -37,6 +39,17 @@ public class ManagedThrottlingExceptionRoutePolicy extends ManagedService
         return policy;
     }
 
+    @Override
+    public String[] getExceptionTypes() {
+        if (policy.getThrottledExceptions() != null) {
+            List<String> types = policy.getThrottledExceptions().stream().map(Class::getName)
+                    .toList();
+            return types.toArray(new String[0]);
+        } else {
+            return null;
+        }
+    }
+
     @Override
     public Long getHalfOpenAfter() {
         return getPolicy().getHalfOpenAfter();
diff --git a/core/camel-management/src/test/java/org/apache/camel/management/ManagedThrottlingExceptionRoutePolicyTest.java b/core/camel-management/src/test/java/org/apache/camel/management/ManagedThrottlingExceptionRoutePolicyTest.java
index 2552653ab84..e1f7cf61e5f 100644
--- a/core/camel-management/src/test/java/org/apache/camel/management/ManagedThrottlingExceptionRoutePolicyTest.java
+++ b/core/camel-management/src/test/java/org/apache/camel/management/ManagedThrottlingExceptionRoutePolicyTest.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.management;
 
+import java.io.IOException;
+import java.util.List;
 import java.util.Set;
 
 import javax.management.JMX;
@@ -84,6 +86,12 @@ public class ManagedThrottlingExceptionRoutePolicyTest extends ManagementTestSup
         String myState = proxy.currentState();
         assertEquals("State closed, failures 0", myState);
 
+        // exception types
+        String[] types = proxy.getExceptionTypes();
+        assertEquals(2, types.length);
+        assertEquals("java.io.IOException", types[0]);
+        assertEquals("java.lang.UnsupportedOperationException", types[1]);
+
         // the route has no failures
         Integer val = proxy.getCurrentFailures();
         assertEquals(0, val.intValue());
@@ -140,7 +148,8 @@ public class ManagedThrottlingExceptionRoutePolicyTest extends ManagementTestSup
 
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
-        ThrottlingExceptionRoutePolicy policy = new ThrottlingExceptionRoutePolicy(10, 1000, 5000, null);
+        ThrottlingExceptionRoutePolicy policy = new ThrottlingExceptionRoutePolicy(10, 1000, 5000,
+                List.of(IOException.class, UnsupportedOperationException.class));
         policy.setHalfOpenHandler(new DummyHandler());
 
         return new RouteBuilder() {
@@ -161,7 +170,7 @@ public class ManagedThrottlingExceptionRoutePolicyTest extends ManagementTestSup
         public void process(Exchange exchange) throws Exception {
             // need to sleep a little to cause last failure to be slow
             Thread.sleep(50);
-            throw new RuntimeException("boom!");
+            throw new IOException("boom!");
         }
 
     }
diff --git a/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java b/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
index e35d833d139..4f1b7f8ed5a 100644
--- a/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
+++ b/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
@@ -100,6 +100,10 @@ public class ThrottlingExceptionRoutePolicy extends RoutePolicySupport implement
         return camelContext;
     }
 
+    public List<Class<?>> getThrottledExceptions() {
+        return throttledExceptions;
+    }
+
     @Override
     public void onInit(Route route) {
         LOG.debug("Initializing ThrottlingExceptionRoutePolicy route policy");