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 572f0211d6c0b94a93e82b508f5a5cdc4dd11bd7 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Thu Jul 12 09:37:23 2018 +0200 CAMEL-12633: camel-jmx - You can now observe attribute and use stringToCompare for the attribute to only trigger when matching in the regular consumer. --- .../camel-jmx/src/main/docs/jmx-component.adoc | 6 +- .../apache/camel/component/jmx/JMXConsumer.java | 6 +- .../jmx/JMXConsumerNotificationFilter.java | 61 ++++++++++++++++++ .../apache/camel/component/jmx/JMXEndpoint.java | 15 +++-- ...sumerObserveAttributeMatchStringDifferTest.java | 73 ++++++++++++++++++++++ ...JmxConsumerObserveAttributeMatchStringTest.java | 72 +++++++++++++++++++++ 6 files changed, 221 insertions(+), 12 deletions(-) diff --git a/components/camel-jmx/src/main/docs/jmx-component.adoc b/components/camel-jmx/src/main/docs/jmx-component.adoc index 0e7677d..9eb59f9 100644 --- a/components/camel-jmx/src/main/docs/jmx-component.adoc +++ b/components/camel-jmx/src/main/docs/jmx-component.adoc @@ -68,6 +68,9 @@ with the following path and query parameters: | *reconnectOnConnection Failure* (advanced) | If true the consumer will attempt to reconnect to the JMX server when any connection failure occurs. The consumer will attempt to re-establish the JMX connection every 'x' seconds until the connection is made-- where 'x' is the configured reconnectionDelay | false | boolean | *synchronous* (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean | *testConnectionOnStartup* (advanced) | If true the consumer will throw an exception if unable to establish the JMX connection upon startup. If false, the consumer will attempt to establish the JMX connection every 'x' seconds until the connection is made -- where 'x' is the configured reconnectionDelay | true | boolean +| *notifyDiffer* (string) | If true, will fire a notification when the string attribute differs from the string to compare (string monitor or consumer). By default the consumer will notify match if observed attribute and string to compare has been configured. | false | boolean +| *notifyMatch* (string) | If true, will fire a notification when the string attribute matches the string to compare (string monitor or consumer). By default the consumer will notify match if observed attribute and string to compare has been configured. | false | boolean +| *stringToCompare* (string) | Value for attribute to compare (string monitor or consumer). By default the consumer will notify match if observed attribute and string to compare has been configured. | | String | *initThreshold* (counter) | Initial threshold for the monitor. The value must exceed this before notifications are fired (counter monitor only). | | int | *modulus* (counter) | The value at which the counter is reset to zero (counter monitor only). | | int | *offset* (counter) | The amount to increment the threshold after it's been exceeded (counter monitor only). | | int @@ -78,9 +81,6 @@ with the following path and query parameters: | *thresholdLow* (gauge) | Value for the gauge's low threshold (gauge monitor only). | | Double | *password* (security) | Credentials for making a remote connection | | String | *user* (security) | Credentials for making a remote connection | | String -| *notifyDiffer* (string) | If true, the string monitor will fire a notification when the string attribute differs from the string to compare (string monitor only). | false | boolean -| *notifyMatch* (string) | If true, the string monitor will fire a notification when the string attribute matches the string to compare (string monitor only). | false | boolean -| *stringToCompare* (string) | Value for the string monitor's string to compare (string monitor only). | | String |=== // endpoint options: END 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 ee02284..1e6e9a3 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 @@ -253,9 +253,9 @@ public class JMXConsumer extends DefaultConsumer implements NotificationListener // 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; + LOG.debug("Observing attribute: {}", ep.getObservedAttribute()); + boolean match = !ep.isNotifyDiffer(); + nf = new JMXConsumerNotificationFilter(ep.getObservedAttribute(), ep.getStringToCompare(), match); } ObjectName objectName = ep.getJMXObjectName(); diff --git a/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXConsumerNotificationFilter.java b/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXConsumerNotificationFilter.java new file mode 100644 index 0000000..d5620b0 --- /dev/null +++ b/components/camel-jmx/src/main/java/org/apache/camel/component/jmx/JMXConsumerNotificationFilter.java @@ -0,0 +1,61 @@ +/** + * 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 javax.management.AttributeChangeNotification; +import javax.management.AttributeChangeNotificationFilter; +import javax.management.Notification; + +/** + * {@link javax.management.NotificationFilter} that observes an attribute and optionally + * matches when the new value matches a string. + */ +public class JMXConsumerNotificationFilter extends AttributeChangeNotificationFilter { + + private final String stringToCompare; + private final boolean notifyMatch; + + public JMXConsumerNotificationFilter(String observedAttribute, String stringToCompare, boolean notifyMatch) { + enableAttribute(observedAttribute); + this.stringToCompare = stringToCompare; + this.notifyMatch = notifyMatch; + } + + @Override + public boolean isNotificationEnabled(Notification notification) { + boolean enabled = super.isNotificationEnabled(notification); + if (!enabled) { + return false; + } + + boolean match = false; + if (stringToCompare != null) { + AttributeChangeNotification acn = (AttributeChangeNotification) notification; + Object newValue = acn.getNewValue(); + // special for null + if ("null".equals(stringToCompare) && newValue == null) { + match = true; + } else if (newValue != null) { + match = stringToCompare.equals(newValue.toString()); + } + return notifyMatch == match; + } else { + return true; + } + } + +} 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 3f7d67f..b174b92 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 @@ -145,21 +145,24 @@ public class JMXEndpoint extends DefaultEndpoint { private Double thresholdLow; /** - * If true, the string monitor will fire a notification when the string attribute differs from the string to compare (string monitor only). + * If true, will fire a notification when the string attribute differs from the string to compare (string monitor or consumer). + * By default the consumer will notify match if observed attribute and string to compare has been configured. */ - @UriParam(label = "string") + @UriParam(label = "consumer,string") private boolean notifyDiffer; /** - * If true, the string monitor will fire a notification when the string attribute matches the string to compare (string monitor only). + * If true, will fire a notification when the string attribute matches the string to compare (string monitor or consumer). + * By default the consumer will notify match if observed attribute and string to compare has been configured. */ - @UriParam(label = "string") + @UriParam(label = "consumer,string") private boolean notifyMatch; /** - * Value for the string monitor's string to compare (string monitor only). + * Value for attribute to compare (string monitor or consumer). + * By default the consumer will notify match if observed attribute and string to compare has been configured. */ - @UriParam(label = "string") + @UriParam(label = "consumer,string") private String stringToCompare; /** diff --git a/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/CamelJmxConsumerObserveAttributeMatchStringDifferTest.java b/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/CamelJmxConsumerObserveAttributeMatchStringDifferTest.java new file mode 100644 index 0000000..90534fb --- /dev/null +++ b/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/CamelJmxConsumerObserveAttributeMatchStringDifferTest.java @@ -0,0 +1,73 @@ +/** + * 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 CamelJmxConsumerObserveAttributeMatchStringDifferTest 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>false</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); + + // change the attribute so JMX triggers + mr = context.getManagedRoute("foo", ManagedRouteMBean.class); + mr.setTracing(false); + + // 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&stringToCompare=true¬ifyDiffer=true", id) + .routeId("jmxRoute") + .to("log:jmx") + .to("mock:result"); + + from("direct:foo").routeId("foo").to("log:foo", "mock:foo"); + } + }; + } +} diff --git a/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/CamelJmxConsumerObserveAttributeMatchStringTest.java b/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/CamelJmxConsumerObserveAttributeMatchStringTest.java new file mode 100644 index 0000000..764004c --- /dev/null +++ b/components/camel-jmx/src/test/java/org/apache/camel/component/jmx/CamelJmxConsumerObserveAttributeMatchStringTest.java @@ -0,0 +1,72 @@ +/** + * 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 CamelJmxConsumerObserveAttributeMatchStringTest 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>false</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); + + // change the attribute so JMX triggers + mr = context.getManagedRoute("foo", ManagedRouteMBean.class); + mr.setTracing(false); + + // 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&stringToCompare=false", id).routeId("jmxRoute") + .to("log:jmx") + .to("mock:result"); + + from("direct:foo").routeId("foo").to("log:foo", "mock:foo"); + } + }; + } +}