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 2018/07/12 07:37:42 UTC

[camel] 01/03: CAMEL-12633: camel-jmx - Allow consumer to alos filter observed attribute.

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

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 81243a00e8f27fa68e8e2e582267c6cc3ab07eae
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Jul 11 10:09:58 2018 +0200

    CAMEL-12633: camel-jmx - Allow consumer to alos filter observed attribute.
---
 .../camel-jmx/src/main/docs/jmx-component.adoc     |  2 +-
 .../apache/camel/component/jmx/JMXConsumer.java    |  8 +++
 .../apache/camel/component/jmx/JMXEndpoint.java    |  2 +-
 .../jmx/CamelJmxConsumerObserveAttributeTest.java  | 64 ++++++++++++++++++++++
 4 files changed, 74 insertions(+), 2 deletions(-)

diff --git a/components/camel-jmx/src/main/docs/jmx-component.adoc b/components/camel-jmx/src/main/docs/jmx-component.adoc
index ecabd57..0e7677d 100644
--- a/components/camel-jmx/src/main/docs/jmx-component.adoc
+++ b/components/camel-jmx/src/main/docs/jmx-component.adoc
@@ -57,7 +57,7 @@ with the following path and query parameters:
 | *monitorType* (consumer) | The type of monitor to create. One of string, gauge, counter (monitor types only). |  | String
 | *objectDomain* (consumer) | *Required* The domain for the mbean you're connecting to |  | String
 | *objectName* (consumer) | The name key for the mbean you're connecting to. This value is mutually exclusive with the object properties that get passed. |  | String
-| *observedAttribute* (consumer) | The attribute to observe for the monitor bean (monitor types only). |  | String
+| *observedAttribute* (consumer) | The attribute to observe for the monitor bean or consumer. |  | String
 | *exceptionHandler* (consumer) | To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this options is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored. |  | ExceptionHandler
 | *exchangePattern* (consumer) | Sets the exchange pattern when the consumer creates an exchange. |  | ExchangePattern
 | *executorService* (advanced) | To use a custom shared thread pool for the consumers. By default each consume has their own thread-pool to process and route notifications. |  | ExecutorService
diff --git a/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXConsumer.java b/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXConsumer.java
index d2db3ae..3419afa 100644
--- a/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXConsumer.java
+++ b/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXConsumer.java
@@ -23,6 +23,7 @@ import java.util.Map;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
+import javax.management.AttributeChangeNotificationFilter;
 import javax.management.MBeanServerConnection;
 import javax.management.Notification;
 import javax.management.NotificationFilter;
@@ -250,6 +251,13 @@ public class JMXConsumer extends DefaultConsumer implements NotificationListener
         JMXEndpoint ep = getEndpoint();
         NotificationFilter nf = ep.getNotificationFilter();
 
+        // if we should observe a single attribute then use filter
+        if (nf == null && ep.getObservedAttribute() != null) {
+            AttributeChangeNotificationFilter acnf = new AttributeChangeNotificationFilter();
+            acnf.enableAttribute(ep.getObservedAttribute());
+            nf = acnf;
+        }
+
         ObjectName objectName = ep.getJMXObjectName();
 
         getServerConnection().addNotificationListener(objectName, this, nf, ep.getHandback());
diff --git a/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXEndpoint.java b/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXEndpoint.java
index 9833774..3f7d67f 100644
--- a/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXEndpoint.java
+++ b/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXEndpoint.java
@@ -79,7 +79,7 @@ public class JMXEndpoint extends DefaultEndpoint {
     private String objectName;
 
     /**
-     * The attribute to observe for the monitor bean (monitor types only).
+     * The attribute to observe for the monitor bean or consumer.
      */
     @UriParam
     private String observedAttribute;
diff --git a/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/CamelJmxConsumerObserveAttributeTest.java b/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/CamelJmxConsumerObserveAttributeTest.java
new file mode 100644
index 0000000..f42da21
--- /dev/null
+++ b/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/CamelJmxConsumerObserveAttributeTest.java
@@ -0,0 +1,64 @@
+/**
+ * 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.jmx;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.api.management.mbean.ManagedRouteMBean;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class CamelJmxConsumerObserveAttributeTest extends CamelTestSupport {
+
+    @Override
+    protected boolean useJmx() {
+        return true;
+    }
+
+    @Test
+    public void testJmxConsumer() throws Exception {
+        getMockEndpoint("mock:result").expectedMinimumMessageCount(1);
+        getMockEndpoint("mock:result").message(0).body().contains("<newValue>true</newValue>");
+        getMockEndpoint("mock:result").message(0).body().contains("<attributeName>Tracing</attributeName>");
+
+        // change the attribute so JMX triggers but should be filtered
+        ManagedRouteMBean mr = context.getManagedRoute("foo", ManagedRouteMBean.class);
+        mr.setStatisticsEnabled(true);
+
+        // change the attribute so JMX triggers
+        mr = context.getManagedRoute("foo", ManagedRouteMBean.class);
+        mr.setTracing(true);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                String id = getContext().getName();
+
+                fromF("jmx:platform?objectDomain=org.apache.camel&key.context=%s&key.type=routes&key.name=\"foo\"&observedAttribute=Tracing", id).routeId("jmxRoute")
+                    .to("log:jmx")
+                    .to("mock:result");
+
+                from("direct:foo").routeId("foo").to("log:foo", "mock:foo");
+            }
+        };
+    }
+}