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
The following commit(s) were added to refs/heads/main by this push: new c0de8c957ce CAMEL-18085 Introduction of ManagementMBeansLevel (#7584) c0de8c957ce is described below commit c0de8c957ce1d319b38568fa5e4f892bda625ab1 Author: JiriOndrusek <ondrusek.j...@gmail.com> AuthorDate: Wed May 11 14:38:41 2022 +0200 CAMEL-18085 Introduction of ManagementMBeansLevel (#7584) --- .../main/camel-main-configuration-metadata.json | 1 + .../resources/org/apache/camel/jaxb.index | 1 + .../org/apache/camel/ManagementMBeansLevel.java | 38 ++++++++++ .../java/org/apache/camel/spi/ManagementAgent.java | 20 ++++- .../MainConfigurationPropertiesConfigurer.java | 6 ++ .../camel-main-configuration-metadata.json | 1 + core/camel-main/src/main/docs/main.adoc | 3 +- .../camel/main/DefaultConfigurationProperties.java | 26 +++++++ .../camel/management/DefaultManagementAgent.java | 12 +++ .../management/JmxManagementLifecycleStrategy.java | 10 +++ .../ManagedMBeansAndStatisticsConflictTest.java | 51 +++++++++++++ .../ManagedMBeansLevelContextOnlyTest.java | 42 +++++++++++ ...edMBeansLevelContextOnlyWithAddedRouteTest.java | 71 ++++++++++++++++++ .../management/ManagedMBeansLevelDefaultTest.java | 41 +++++++++++ ...anagedMBeansLevelDefaultWithAddedRouteTest.java | 76 +++++++++++++++++++ .../ManagedMBeansLevelRoutesOnlyTest.java | 42 +++++++++++ .../management/ManagedMBeansLevelTestSupport.java | 86 ++++++++++++++++++++++ docs/user-manual/modules/ROOT/pages/jmx.adoc | 13 ++++ 18 files changed, 538 insertions(+), 2 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/main/camel-main-configuration-metadata.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/main/camel-main-configuration-metadata.json index 668d2cf6ae3..d6df43111f8 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/main/camel-main-configuration-metadata.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/main/camel-main-configuration-metadata.json @@ -53,6 +53,7 @@ { "name": "camel.main.javaRoutesExcludePattern", "description": "Used for exclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. For example to exclude all classes starting with Bar use: **\/Bar* To exclude all routes form a specific package use: com\/mycomp [...] { "name": "camel.main.javaRoutesIncludePattern", "description": "Used for inclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. Multiple patterns can be specified separated by comma. For example to include all classes starting with Foo use: **\/Foo To include a [...] { "name": "camel.main.jmxEnabled", "description": "Enable JMX in your Camel application.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "boolean", "javaType": "boolean", "defaultValue": true }, + { "name": "camel.main.jmxManagementMBeansLevel", "description": "Sets the mbeans registration level. The default value is Default.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "object", "javaType": "org.apache.camel.ManagementMBeansLevel", "defaultValue": "Default" }, { "name": "camel.main.jmxManagementNamePattern", "description": "The naming pattern for creating the CamelContext JMX management name. The default pattern is #name#", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "string", "javaType": "java.lang.String", "defaultValue": "#name#" }, { "name": "camel.main.jmxManagementStatisticsLevel", "description": "Sets the JMX statistics level, the level can be set to Extended to gather additional information The default value is Default.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "object", "javaType": "org.apache.camel.ManagementStatisticsLevel", "defaultValue": "Default", "enum": [ "Extended", "Default", "RoutesOnly", "Off" ] }, { "name": "camel.main.lightweight", "description": "Configure the context to be lightweight. This will trigger some optimizations and memory reduction options. Lightweight context have some limitations. At this moment, dynamic endpoint destinations are not supported.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "boolean", "javaType": "boolean", "defaultValue": "false" }, diff --git a/core/camel-api/src/generated/resources/org/apache/camel/jaxb.index b/core/camel-api/src/generated/resources/org/apache/camel/jaxb.index index 6d9b86e4fef..60b154c6a8f 100644 --- a/core/camel-api/src/generated/resources/org/apache/camel/jaxb.index +++ b/core/camel-api/src/generated/resources/org/apache/camel/jaxb.index @@ -2,6 +2,7 @@ BeanScope ExchangePattern LoggingLevel +ManagementMBeansLevel ManagementStatisticsLevel ShutdownRoute ShutdownRunningTask diff --git a/core/camel-api/src/main/java/org/apache/camel/ManagementMBeansLevel.java b/core/camel-api/src/main/java/org/apache/camel/ManagementMBeansLevel.java new file mode 100644 index 00000000000..421b374796a --- /dev/null +++ b/core/camel-api/src/main/java/org/apache/camel/ManagementMBeansLevel.java @@ -0,0 +1,38 @@ +/* + * 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; + +import javax.xml.bind.annotation.XmlEnum; + +/** + * Level of mbeans for registration + */ +@XmlEnum +public enum ManagementMBeansLevel { + + ContextOnly, + RoutesOnly, + Default; + + public boolean isRoutes() { + return ordinal() == Default.ordinal() || ordinal() == RoutesOnly.ordinal(); + } + + public boolean isProcessors() { + return ordinal() == Default.ordinal(); + } +} diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/ManagementAgent.java b/core/camel-api/src/main/java/org/apache/camel/spi/ManagementAgent.java index fad1c618206..199ddb88fde 100644 --- a/core/camel-api/src/main/java/org/apache/camel/spi/ManagementAgent.java +++ b/core/camel-api/src/main/java/org/apache/camel/spi/ManagementAgent.java @@ -20,6 +20,7 @@ import javax.management.JMException; import javax.management.MBeanServer; import javax.management.ObjectName; +import org.apache.camel.ManagementMBeansLevel; import org.apache.camel.ManagementStatisticsLevel; import org.apache.camel.Service; @@ -294,6 +295,23 @@ public interface ManagementAgent extends Service { */ ManagementStatisticsLevel getStatisticsLevel(); + /** + * Sets the mbeans registration level + * <p/> + * Default is {@link org.apache.camel.ManagementMBeansLevel#Default} + * <p/> + * + * @param level the new level + */ + void setMBeansLevel(ManagementMBeansLevel level); + + /** + * ยง Gets the mbeans registration level + * + * @return the level + */ + ManagementMBeansLevel getMBeansLevel(); + /** * Gets whether host IP Address to be used instead of host name. * @@ -303,7 +321,7 @@ public interface ManagementAgent extends Service { /** * Sets whether to use host IP Address - * + * * @param useHostIPAddress <tt>true</tt> to use IP Address. */ void setUseHostIPAddress(Boolean useHostIPAddress); diff --git a/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java b/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java index 8d2e873c166..5a2510cda73 100644 --- a/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java +++ b/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java @@ -101,6 +101,8 @@ public class MainConfigurationPropertiesConfigurer extends org.apache.camel.supp case "JavaRoutesIncludePattern": target.setJavaRoutesIncludePattern(property(camelContext, java.lang.String.class, value)); return true; case "jmxenabled": case "JmxEnabled": target.setJmxEnabled(property(camelContext, boolean.class, value)); return true; + case "jmxmanagementmbeanslevel": + case "JmxManagementMBeansLevel": target.setJmxManagementMBeansLevel(property(camelContext, org.apache.camel.ManagementMBeansLevel.class, value)); return true; case "jmxmanagementnamepattern": case "JmxManagementNamePattern": target.setJmxManagementNamePattern(property(camelContext, java.lang.String.class, value)); return true; case "jmxmanagementstatisticslevel": @@ -332,6 +334,8 @@ public class MainConfigurationPropertiesConfigurer extends org.apache.camel.supp case "JavaRoutesIncludePattern": return java.lang.String.class; case "jmxenabled": case "JmxEnabled": return boolean.class; + case "jmxmanagementmbeanslevel": + case "JmxManagementMBeansLevel": return org.apache.camel.ManagementMBeansLevel.class; case "jmxmanagementnamepattern": case "JmxManagementNamePattern": return java.lang.String.class; case "jmxmanagementstatisticslevel": @@ -564,6 +568,8 @@ public class MainConfigurationPropertiesConfigurer extends org.apache.camel.supp case "JavaRoutesIncludePattern": return target.getJavaRoutesIncludePattern(); case "jmxenabled": case "JmxEnabled": return target.isJmxEnabled(); + case "jmxmanagementmbeanslevel": + case "JmxManagementMBeansLevel": return target.getJmxManagementMBeansLevel(); case "jmxmanagementnamepattern": case "JmxManagementNamePattern": return target.getJmxManagementNamePattern(); case "jmxmanagementstatisticslevel": diff --git a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json index 668d2cf6ae3..d6df43111f8 100644 --- a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json +++ b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json @@ -53,6 +53,7 @@ { "name": "camel.main.javaRoutesExcludePattern", "description": "Used for exclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. For example to exclude all classes starting with Bar use: **\/Bar* To exclude all routes form a specific package use: com\/mycomp [...] { "name": "camel.main.javaRoutesIncludePattern", "description": "Used for inclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. Multiple patterns can be specified separated by comma. For example to include all classes starting with Foo use: **\/Foo To include a [...] { "name": "camel.main.jmxEnabled", "description": "Enable JMX in your Camel application.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "boolean", "javaType": "boolean", "defaultValue": true }, + { "name": "camel.main.jmxManagementMBeansLevel", "description": "Sets the mbeans registration level. The default value is Default.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "object", "javaType": "org.apache.camel.ManagementMBeansLevel", "defaultValue": "Default" }, { "name": "camel.main.jmxManagementNamePattern", "description": "The naming pattern for creating the CamelContext JMX management name. The default pattern is #name#", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "string", "javaType": "java.lang.String", "defaultValue": "#name#" }, { "name": "camel.main.jmxManagementStatisticsLevel", "description": "Sets the JMX statistics level, the level can be set to Extended to gather additional information The default value is Default.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "object", "javaType": "org.apache.camel.ManagementStatisticsLevel", "defaultValue": "Default", "enum": [ "Extended", "Default", "RoutesOnly", "Off" ] }, { "name": "camel.main.lightweight", "description": "Configure the context to be lightweight. This will trigger some optimizations and memory reduction options. Lightweight context have some limitations. At this moment, dynamic endpoint destinations are not supported.", "sourceType": "org.apache.camel.main.DefaultConfigurationProperties", "type": "boolean", "javaType": "boolean", "defaultValue": "false" }, diff --git a/core/camel-main/src/main/docs/main.adoc b/core/camel-main/src/main/docs/main.adoc index a9c5d2f5712..a387bd9e338 100644 --- a/core/camel-main/src/main/docs/main.adoc +++ b/core/camel-main/src/main/docs/main.adoc @@ -18,7 +18,7 @@ The following tables lists all the options: // main options: START === Camel Main configurations -The camel.main supports 112 options, which are listed below. +The camel.main supports 113 options, which are listed below. [width="100%",cols="2,5,^1,2",options="header"] |=== @@ -63,6 +63,7 @@ The camel.main supports 112 options, which are listed below. | *camel.main.javaRoutesExclude{zwsp}Pattern* | Used for exclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. For example to exclude all classes starting with Bar use: **/Bar* To exclude all routes form a specific package use: com/mycompany/bar/* To exclud [...] | *camel.main.javaRoutesInclude{zwsp}Pattern* | Used for inclusive filtering RouteBuilder classes which are collected from the registry or via classpath scanning. The exclusive filtering takes precedence over inclusive filtering. The pattern is using Ant-path style pattern. Multiple patterns can be specified separated by comma. Multiple patterns can be specified separated by comma. For example to include all classes starting with Foo use: **/Foo To include all routes form a speci [...] | *camel.main.jmxEnabled* | Enable JMX in your Camel application. | true | boolean +| *camel.main.jmxManagementMBeans{zwsp}Level* | Sets the mbeans registration level. The default value is Default. | Default | ManagementMBeansLevel | *camel.main.jmxManagementName{zwsp}Pattern* | The naming pattern for creating the CamelContext JMX management name. The default pattern is #name# | #name# | String | *camel.main.jmxManagement{zwsp}StatisticsLevel* | Sets the JMX statistics level, the level can be set to Extended to gather additional information The default value is Default. | Default | ManagementStatisticsLevel | *camel.main.lightweight* | Configure the context to be lightweight. This will trigger some optimizations and memory reduction options. Lightweight context have some limitations. At this moment, dynamic endpoint destinations are not supported. | false | boolean diff --git a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java index d1245f02606..3da030fb362 100644 --- a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java +++ b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java @@ -20,6 +20,7 @@ import java.util.HashMap; import java.util.Map; import org.apache.camel.LoggingLevel; +import org.apache.camel.ManagementMBeansLevel; import org.apache.camel.ManagementStatisticsLevel; import org.apache.camel.StartupSummaryLevel; import org.apache.camel.spi.Metadata; @@ -88,6 +89,8 @@ public abstract class DefaultConfigurationProperties<T> { private boolean useBreadcrumb; private boolean beanPostProcessorEnabled = true; @Metadata(defaultValue = "Default") + private ManagementMBeansLevel jmxManagementMBeansLevel = ManagementMBeansLevel.Default; + @Metadata(defaultValue = "Default") private ManagementStatisticsLevel jmxManagementStatisticsLevel = ManagementStatisticsLevel.Default; private String jmxManagementNamePattern = "#name#"; private boolean useMdcLogging; @@ -842,6 +845,19 @@ public abstract class DefaultConfigurationProperties<T> { this.beanPostProcessorEnabled = beanPostProcessorEnabled; } + public ManagementMBeansLevel getJmxManagementMBeansLevel() { + return jmxManagementMBeansLevel; + } + + /** + * Sets the mbeans registration level. + * + * The default value is Default. + */ + public void setJmxManagementMBeansLevel(ManagementMBeansLevel jmxManagementMBeansLevel) { + this.jmxManagementMBeansLevel = jmxManagementMBeansLevel; + } + public ManagementStatisticsLevel getJmxManagementStatisticsLevel() { return jmxManagementStatisticsLevel; } @@ -1977,6 +1993,16 @@ public abstract class DefaultConfigurationProperties<T> { return (T) this; } + /** + * Sets the mbeans registration level. + * + * The default value is Default. + */ + public T withJmxManagementMBeansLevel(ManagementMBeansLevel jmxManagementMBeansLevel) { + this.jmxManagementMBeansLevel = jmxManagementMBeansLevel; + return (T) this; + } + /** * Sets the JMX statistics level The level can be set to Extended to gather additional information * diff --git a/core/camel-management/src/main/java/org/apache/camel/management/DefaultManagementAgent.java b/core/camel-management/src/main/java/org/apache/camel/management/DefaultManagementAgent.java index 9b7f783eda6..8830df36a4d 100644 --- a/core/camel-management/src/main/java/org/apache/camel/management/DefaultManagementAgent.java +++ b/core/camel-management/src/main/java/org/apache/camel/management/DefaultManagementAgent.java @@ -34,6 +34,7 @@ import javax.management.ObjectName; import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; import org.apache.camel.ExtendedCamelContext; +import org.apache.camel.ManagementMBeansLevel; import org.apache.camel.ManagementStatisticsLevel; import org.apache.camel.api.management.JmxSystemPropertyKeys; import org.apache.camel.spi.ManagementAgent; @@ -74,6 +75,7 @@ public class DefaultManagementAgent extends ServiceSupport implements Management private Boolean useHostIPAddress = false; private String managementNamePattern = "#name#"; private ManagementStatisticsLevel statisticsLevel = ManagementStatisticsLevel.Default; + private ManagementMBeansLevel mBeansLevel = ManagementMBeansLevel.Default; public DefaultManagementAgent() { } @@ -286,6 +288,16 @@ public class DefaultManagementAgent extends ServiceSupport implements Management this.statisticsLevel = statisticsLevel; } + @Override + public ManagementMBeansLevel getMBeansLevel() { + return mBeansLevel; + } + + @Override + public void setMBeansLevel(ManagementMBeansLevel mBeansLevel) { + this.mBeansLevel = mBeansLevel; + } + @Override public CamelContext getCamelContext() { return camelContext; diff --git a/core/camel-management/src/main/java/org/apache/camel/management/JmxManagementLifecycleStrategy.java b/core/camel-management/src/main/java/org/apache/camel/management/JmxManagementLifecycleStrategy.java index b415b93d02e..63608f9378e 100644 --- a/core/camel-management/src/main/java/org/apache/camel/management/JmxManagementLifecycleStrategy.java +++ b/core/camel-management/src/main/java/org/apache/camel/management/JmxManagementLifecycleStrategy.java @@ -829,6 +829,11 @@ public class JmxManagementLifecycleStrategy extends ServiceSupport implements Li * Should the given processor be registered. */ protected boolean registerProcessor(ProcessorDefinition<?> processor) { + + //skip processors according the ManagementMBeansLevel + if (!getManagementStrategy().getManagementAgent().getMBeansLevel().isProcessors()) { + return false; + } // skip on exception if (processor instanceof OnExceptionDefinition) { return false; @@ -914,6 +919,11 @@ public class JmxManagementLifecycleStrategy extends ServiceSupport implements Li LOG.trace("Checking whether to register {} from route: {}", service, route); + //skip route according the ManagementMBeansLevel + if (!getManagementStrategy().getManagementAgent().getMBeansLevel().isRoutes()) { + return false; + } + ManagementAgent agent = getManagementStrategy().getManagementAgent(); if (agent == null) { // do not register if no agent diff --git a/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansAndStatisticsConflictTest.java b/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansAndStatisticsConflictTest.java new file mode 100644 index 00000000000..11a0c8ee5b3 --- /dev/null +++ b/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansAndStatisticsConflictTest.java @@ -0,0 +1,51 @@ +/* + * 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.management; + +import java.util.Set; + +import javax.management.ObjectName; + +import org.apache.camel.CamelContext; +import org.apache.camel.ManagementMBeansLevel; +import org.apache.camel.ManagementStatisticsLevel; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@DisabledOnOs(OS.AIX) +public class ManagedMBeansAndStatisticsConflictTest extends ManagedMBeansLevelTestSupport { + + public ManagedMBeansAndStatisticsConflictTest() { + super(ManagementMBeansLevel.ContextOnly); + } + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = super.createCamelContext(); + context.getManagementStrategy().getManagementAgent().setStatisticsLevel(ManagementStatisticsLevel.RoutesOnly); + return context; + } + + @Override + void assertResults(Set<ObjectName> contexts, Set<ObjectName> routes, Set<ObjectName> processors) { + assertEquals(1, contexts.size()); + assertEquals(0, routes.size()); + assertEquals(0, processors.size()); + } +} diff --git a/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelContextOnlyTest.java b/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelContextOnlyTest.java new file mode 100644 index 00000000000..b2816df0357 --- /dev/null +++ b/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelContextOnlyTest.java @@ -0,0 +1,42 @@ +/* + * 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.management; + +import java.util.Set; + +import javax.management.ObjectName; + +import org.apache.camel.ManagementMBeansLevel; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@DisabledOnOs(OS.AIX) +public class ManagedMBeansLevelContextOnlyTest extends ManagedMBeansLevelTestSupport { + + public ManagedMBeansLevelContextOnlyTest() { + super(ManagementMBeansLevel.ContextOnly); + } + + @Override + void assertResults(Set<ObjectName> contexts, Set<ObjectName> routes, Set<ObjectName> processors) { + assertEquals(1, contexts.size()); + assertEquals(0, routes.size()); + assertEquals(0, processors.size()); + } +} diff --git a/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelContextOnlyWithAddedRouteTest.java b/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelContextOnlyWithAddedRouteTest.java new file mode 100644 index 00000000000..1480c5fabe3 --- /dev/null +++ b/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelContextOnlyWithAddedRouteTest.java @@ -0,0 +1,71 @@ +/* + * 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.management; + +import java.util.Set; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import org.apache.camel.ManagementMBeansLevel; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests that mbeans for routes and processors are NOT added after addition of a 2nd route after CamelContext has been + * started, because the mbeansLevel is set to contextOnly. + */ +@DisabledOnOs(OS.AIX) +public class ManagedMBeansLevelContextOnlyWithAddedRouteTest extends ManagedMBeansLevelTestSupport { + + public ManagedMBeansLevelContextOnlyWithAddedRouteTest() { + super(ManagementMBeansLevel.ContextOnly); + } + + @Override + void assertResults(Set<ObjectName> contexts, Set<ObjectName> routes, Set<ObjectName> processors) { + assertEquals(1, contexts.size()); + assertEquals(0, routes.size()); + assertEquals(0, processors.size()); + } + + @Test + @Override + public void test() throws Exception { + MBeanServer mbeanServer = getMBeanServer(); + //test state after before adding a new route + assertMBeans(mbeanServer); + + log.info(">>>>>>>>>>>>>>>>> adding 2nd route <<<<<<<<<<<<<<"); + // add a 2nd route + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:bar").routeId("bar").to("mock:bar"); + } + }); + log.info(">>>>>>>>>>>>>>>>> adding 2nd route DONE <<<<<<<<<<<<<<"); + + //no route or processor MBean should be created + assertMBeans(mbeanServer); + } + +} diff --git a/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelDefaultTest.java b/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelDefaultTest.java new file mode 100644 index 00000000000..9e685a0add4 --- /dev/null +++ b/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelDefaultTest.java @@ -0,0 +1,41 @@ +/* + * 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.management; + +import java.util.Set; + +import javax.management.ObjectName; + +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@DisabledOnOs(OS.AIX) +public class ManagedMBeansLevelDefaultTest extends ManagedMBeansLevelTestSupport { + + public ManagedMBeansLevelDefaultTest() { + super(null); + } + + @Override + void assertResults(Set<ObjectName> contexts, Set<ObjectName> routes, Set<ObjectName> processors) { + assertEquals(1, contexts.size()); + assertEquals(1, routes.size()); + assertEquals(1, processors.size()); + } +} diff --git a/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelDefaultWithAddedRouteTest.java b/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelDefaultWithAddedRouteTest.java new file mode 100644 index 00000000000..704b2557428 --- /dev/null +++ b/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelDefaultWithAddedRouteTest.java @@ -0,0 +1,76 @@ +/* + * 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.management; + +import java.util.Set; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import org.apache.camel.ManagementMBeansLevel; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests that mbeans for routes and processors are added after addition of a 2nd route after CamelContext has been + * started, because the mbeansLevel is set to contextOnly. + */ +@DisabledOnOs(OS.AIX) +public class ManagedMBeansLevelDefaultWithAddedRouteTest extends ManagedMBeansLevelTestSupport { + + public ManagedMBeansLevelDefaultWithAddedRouteTest() { + super(ManagementMBeansLevel.Default); + } + + @Override + void assertResults(Set<ObjectName> contexts, Set<ObjectName> routes, Set<ObjectName> processors) { + assertEquals(1, contexts.size()); + assertEquals(1, routes.size()); + assertEquals(1, processors.size()); + } + + @Test + @Override + public void test() throws Exception { + MBeanServer mbeanServer = getMBeanServer(); + //test state after before adding a new route + assertMBeans(mbeanServer); + + log.info(">>>>>>>>>>>>>>>>> adding 2nd route <<<<<<<<<<<<<<"); + // add a 2nd route + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:bar").routeId("bar").to("mock:bar"); + } + }); + log.info(">>>>>>>>>>>>>>>>> adding 2nd route DONE <<<<<<<<<<<<<<"); + + //no route or processor MBean should be created + Set<ObjectName> contexts = mbeanServer.queryNames(new ObjectName("*:type=context,*"), null); + Set<ObjectName> routes = mbeanServer.queryNames(new ObjectName("*:type=routes,*"), null); + Set<ObjectName> processors = mbeanServer.queryNames(new ObjectName("*:type=processors,*"), null); + assertEquals(1, contexts.size()); + assertEquals(2, routes.size()); + assertEquals(2, processors.size()); + } + +} diff --git a/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelRoutesOnlyTest.java b/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelRoutesOnlyTest.java new file mode 100644 index 00000000000..e0e4e42e264 --- /dev/null +++ b/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelRoutesOnlyTest.java @@ -0,0 +1,42 @@ +/* + * 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.management; + +import java.util.Set; + +import javax.management.ObjectName; + +import org.apache.camel.ManagementMBeansLevel; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@DisabledOnOs(OS.AIX) +public class ManagedMBeansLevelRoutesOnlyTest extends ManagedMBeansLevelTestSupport { + + public ManagedMBeansLevelRoutesOnlyTest() { + super(ManagementMBeansLevel.RoutesOnly); + } + + @Override + void assertResults(Set<ObjectName> contexts, Set<ObjectName> routes, Set<ObjectName> processors) { + assertEquals(1, contexts.size()); + assertEquals(1, routes.size()); + assertEquals(0, processors.size()); + } +} diff --git a/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelTestSupport.java b/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelTestSupport.java new file mode 100644 index 00000000000..d0bb63ee6a4 --- /dev/null +++ b/core/camel-management/src/test/java/org/apache/camel/management/ManagedMBeansLevelTestSupport.java @@ -0,0 +1,86 @@ +/* + * 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.management; + +import java.util.Set; + +import javax.management.MBeanServer; +import javax.management.MalformedObjectNameException; +import javax.management.ObjectName; + +import org.apache.camel.CamelContext; +import org.apache.camel.ManagementMBeansLevel; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; + +@DisabledOnOs(OS.AIX) +public abstract class ManagedMBeansLevelTestSupport extends ManagementTestSupport { + + private final ManagementMBeansLevel level; + + public ManagedMBeansLevelTestSupport(ManagementMBeansLevel level) { + this.level = level; + } + + abstract void assertResults(Set<ObjectName> contexts, Set<ObjectName> routes, Set<ObjectName> processors); + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = super.createCamelContext(); + //set level if provided + if (level != null) { + context.getManagementStrategy().getManagementAgent().setMBeansLevel(level); + } + return context; + } + + @Test + public void test() throws Exception { + MockEndpoint result = getMockEndpoint("mock:result"); + result.expectedMessageCount(1); + + template.sendBodyAndHeader("direct:start", "Hello World", "foo", "123"); + + assertMockEndpointsSatisfied(); + + // get the stats for the route + MBeanServer mbeanServer = getMBeanServer(); + + assertMBeans(mbeanServer); + } + + void assertMBeans(MBeanServer mbeanServer) throws MalformedObjectNameException { + Set<ObjectName> contexts = mbeanServer.queryNames(new ObjectName("*:type=context,*"), null); + Set<ObjectName> routes = mbeanServer.queryNames(new ObjectName("*:type=routes,*"), null); + Set<ObjectName> processors = mbeanServer.queryNames(new ObjectName("*:type=processors,*"), null); + + assertResults(contexts, routes, processors); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start").to("mock:result"); + } + }; + } +} diff --git a/docs/user-manual/modules/ROOT/pages/jmx.adoc b/docs/user-manual/modules/ROOT/pages/jmx.adoc index 206f5da1f3f..ef0f77671b5 100644 --- a/docs/user-manual/modules/ROOT/pages/jmx.adoc +++ b/docs/user-manual/modules/ROOT/pages/jmx.adoc @@ -82,6 +82,10 @@ The Camel JMX can be configured with the following options. |`mbeanObjectDomainName` |`org.apache.camel.jmx.mbeanObjectDomainName` |`org.apache.camel` |The JMX domain that all object names will use +|`mbeansLevel` | |`Default` +|Configures the level for which MBeans are registered. +See section _Registering MBeans for specific resources only_ for more details. + |`onlyRegisterProcessorWithCustomId` |`org.apache.camel.jmx.onlyRegisterProcessorWithCustomId` |`false` |If this option is enabled then only processors with a @@ -113,6 +117,15 @@ usage of each incoming and outgoing endpoints). from MBean names and attributes. |======================================================================= +=== Registering MBeans for specific resources only + +By default Camel registers MBeans for the context, all the routes and the processors when its starting. You can set a level whether MBeans are registered or not when Camel starts up. The levels are: + +* `Default` - Camel will register MBeans for the context, all the routes and the processors. + +* `RoutesOnly` - Camel will register MBeans for the context and for all the routes. (not for any processor). + +* `ContextOnly` - Camel will register MBeans for the context (neither for any route nor for any processor). === Registering new MBeans for new routes or endpoints