Author: davsclaus
Date: Wed Oct 3 13:01:22 2012
New Revision: 1393469
URL: http://svn.apache.org/viewvc?rev=1393469&view=rev
Log:
CAMEL-5675: Improved startup performance. Especially when JMX enabled.
Introspection and JMX mbean assembling uses a internal class introspection
cache to speedup.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/management/BigRouteTest.java
(with props)
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementMBeanAssembler.java
camel/trunk/camel-core/src/main/java/org/apache/camel/management/MBeanInfoAssembler.java
camel/trunk/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=1393469&r1=1393468&r2=1393469&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
Wed Oct 3 13:01:22 2012
@@ -119,6 +119,7 @@ import org.apache.camel.support.ServiceS
import org.apache.camel.util.CamelContextHelper;
import org.apache.camel.util.EndpointHelper;
import org.apache.camel.util.EventHelper;
+import org.apache.camel.util.IntrospectionSupport;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.ServiceHelper;
import org.apache.camel.util.StopWatch;
@@ -1651,12 +1652,16 @@ public class DefaultCamelContext extends
// shutdown executor service and management as the last one
shutdownServices(executorServiceManager);
shutdownServices(managementStrategy);
+ shutdownServices(managementMBeanAssembler);
shutdownServices(lifecycleStrategies);
// do not clear lifecycleStrategies as we can start Camel again and
get the route back as before
// stop the lazy created so they can be re-created on restart
forceStopLazyInitialization();
+ // stop to clear introspection cache
+ IntrospectionSupport.stop();
+
stopWatch.stop();
if (log.isInfoEnabled()) {
log.info("Uptime {}", getUptime());
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementMBeanAssembler.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementMBeanAssembler.java?rev=1393469&r1=1393468&r2=1393469&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementMBeanAssembler.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementMBeanAssembler.java
Wed Oct 3 13:01:22 2012
@@ -24,11 +24,13 @@ import javax.management.modelmbean.Model
import javax.management.modelmbean.ModelMBeanInfo;
import javax.management.modelmbean.RequiredModelMBean;
+import org.apache.camel.Service;
import org.apache.camel.api.management.ManagedInstance;
import org.apache.camel.api.management.ManagedResource;
import org.apache.camel.api.management.NotificationSenderAware;
import org.apache.camel.spi.ManagementMBeanAssembler;
import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.ServiceHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -39,7 +41,7 @@ import org.slf4j.LoggerFactory;
*
* @version
*/
-public class DefaultManagementMBeanAssembler implements
ManagementMBeanAssembler {
+public class DefaultManagementMBeanAssembler implements
ManagementMBeanAssembler, Service {
protected final Logger log = LoggerFactory.getLogger(getClass());
private final MBeanInfoAssembler assembler;
@@ -86,4 +88,13 @@ public class DefaultManagementMBeanAssem
return mbean;
}
+ @Override
+ public void start() throws Exception {
+ ServiceHelper.startService(assembler);
+ }
+
+ @Override
+ public void stop() throws Exception {
+ ServiceHelper.stopService(assembler);
+ }
}
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/management/MBeanInfoAssembler.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/MBeanInfoAssembler.java?rev=1393469&r1=1393468&r2=1393469&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/management/MBeanInfoAssembler.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/management/MBeanInfoAssembler.java
Wed Oct 3 13:01:22 2012
@@ -17,7 +17,7 @@
package org.apache.camel.management;
import java.lang.reflect.Method;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
@@ -30,12 +30,14 @@ import javax.management.modelmbean.Model
import javax.management.modelmbean.ModelMBeanNotificationInfo;
import javax.management.modelmbean.ModelMBeanOperationInfo;
+import org.apache.camel.Service;
import org.apache.camel.api.management.ManagedAttribute;
import org.apache.camel.api.management.ManagedNotification;
import org.apache.camel.api.management.ManagedNotifications;
import org.apache.camel.api.management.ManagedOperation;
import org.apache.camel.api.management.ManagedResource;
import org.apache.camel.util.IntrospectionSupport;
+import org.apache.camel.util.LRUSoftCache;
import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -45,10 +47,31 @@ import org.slf4j.LoggerFactory;
* details from the {@link ManagedResource}, {@link ManagedAttribute}, {@link
ManagedOperation},
* {@link ManagedNotification}, and {@link ManagedNotifications} annotations.
*/
-public class MBeanInfoAssembler {
+public class MBeanInfoAssembler implements Service {
private static final Logger LOG =
LoggerFactory.getLogger(MBeanInfoAssembler.class);
+ // use a cache to speedup gathering JMX MBeanInfo for known classes
+ private final Map<Class<?>, MBeanAttributesAndOperations> cache = new
LRUSoftCache<Class<?>, MBeanAttributesAndOperations>(1000);
+
+ @Override
+ public void start() throws Exception {
+ // noop
+ }
+
+ @Override
+ public void stop() throws Exception {
+ cache.clear();
+ }
+
+ /**
+ * Structure to hold cached mbean attributes and operations for a given
class.
+ */
+ private static final class MBeanAttributesAndOperations {
+ private Map<String, ManagedAttributeInfo> attributes;
+ private Set<ManagedOperationInfo> operations;
+ }
+
/**
* Gets the {@link ModelMBeanInfo} for the given managed bean
*
@@ -60,7 +83,7 @@ public class MBeanInfoAssembler {
*/
public ModelMBeanInfo getMBeanInfo(Object defaultManagedBean, Object
customManagedBean, String objectName) throws JMException {
// maps and lists to contain information about attributes and
operations
- Map<String, ManagedAttributeInfo> attributes = new HashMap<String,
ManagedAttributeInfo>();
+ Map<String, ManagedAttributeInfo> attributes = new
LinkedHashMap<String, ManagedAttributeInfo>();
Set<ManagedOperationInfo> operations = new
LinkedHashSet<ManagedOperationInfo>();
Set<ModelMBeanAttributeInfo> mBeanAttributes = new
LinkedHashSet<ModelMBeanAttributeInfo>();
Set<ModelMBeanOperationInfo> mBeanOperations = new
LinkedHashSet<ModelMBeanOperationInfo>();
@@ -93,8 +116,28 @@ public class MBeanInfoAssembler {
}
private void extractAttributesAndOperations(Class<?> managedClass,
Map<String, ManagedAttributeInfo> attributes, Set<ManagedOperationInfo>
operations) {
+ MBeanAttributesAndOperations cached = cache.get(managedClass);
+ if (cached == null) {
+ doExtractAttributesAndOperations(managedClass, attributes,
operations);
+ cached = new MBeanAttributesAndOperations();
+ cached.attributes = new LinkedHashMap<String,
ManagedAttributeInfo>(attributes);
+ cached.operations = new
LinkedHashSet<ManagedOperationInfo>(operations);
+
+ // clear before we re-add them
+ attributes.clear();
+ operations.clear();
+
+ // add to cache
+ cache.put(managedClass, cached);
+ }
+
+ attributes.putAll(cached.attributes);
+ operations.addAll(cached.operations);
+ }
+
+ private void doExtractAttributesAndOperations(Class<?> managedClass,
Map<String, ManagedAttributeInfo> attributes, Set<ManagedOperationInfo>
operations) {
// extract the class
- doExtractAttributesAndOperations(managedClass, attributes, operations);
+ doDoExtractAttributesAndOperations(managedClass, attributes,
operations);
// and then any sub classes
if (managedClass.getSuperclass() != null) {
@@ -102,7 +145,7 @@ public class MBeanInfoAssembler {
// skip any JDK classes
if (!clazz.getName().startsWith("java")) {
LOG.trace("Extracting attributes and operations from sub
class: {}", clazz);
- extractAttributesAndOperations(clazz, attributes, operations);
+ doExtractAttributesAndOperations(clazz, attributes,
operations);
}
}
@@ -115,31 +158,39 @@ public class MBeanInfoAssembler {
continue;
}
LOG.trace("Extracting attributes and operations from
implemented interface: {}", clazz);
- extractAttributesAndOperations(clazz, attributes, operations);
+ doExtractAttributesAndOperations(clazz, attributes,
operations);
}
}
}
- private void doExtractAttributesAndOperations(Class<?> managedClass,
Map<String, ManagedAttributeInfo> attributes, Set<ManagedOperationInfo>
operations) {
+ private void doDoExtractAttributesAndOperations(Class<?> managedClass,
Map<String, ManagedAttributeInfo> attributes, Set<ManagedOperationInfo>
operations) {
LOG.trace("Extracting attributes and operations from class: {}",
managedClass);
- for (Method method : managedClass.getDeclaredMethods()) {
- LOG.trace("Extracting attributes and operations from method: {}",
method);
- ManagedAttribute ma = method.getAnnotation(ManagedAttribute.class);
+ // introspect the class, and leverage the cache to have better
performance
+ IntrospectionSupport.ClassInfo cache =
IntrospectionSupport.cacheClass(managedClass);
+
+ for (IntrospectionSupport.MethodInfo cacheInfo : cache.methods) {
+ // must be from declaring class
+ if (cacheInfo.method.getDeclaringClass() != managedClass) {
+ continue;
+ }
+
+ LOG.trace("Extracting attributes and operations from method: {}",
cacheInfo.method);
+ ManagedAttribute ma =
cacheInfo.method.getAnnotation(ManagedAttribute.class);
if (ma != null) {
String key;
String desc = ma.description();
Method getter = null;
Method setter = null;
- if (IntrospectionSupport.isGetter(method)) {
- key = IntrospectionSupport.getGetterShorthandName(method);
- getter = method;
- } else if (IntrospectionSupport.isSetter(method)) {
- key = IntrospectionSupport.getSetterShorthandName(method);
- setter = method;
+ if (cacheInfo.isGetter) {
+ key = cacheInfo.getterOrSetterShorthandName;
+ getter = cacheInfo.method;
+ } else if (cacheInfo.isSetter) {
+ key = cacheInfo.getterOrSetterShorthandName;
+ setter = cacheInfo.method;
} else {
- throw new IllegalArgumentException("@ManagedAttribute can
only be used on Java bean methods, was: " + method + " on bean: " +
managedClass);
+ throw new IllegalArgumentException("@ManagedAttribute can
only be used on Java bean methods, was: " + cacheInfo.method + " on bean: " +
managedClass);
}
// they key must be capitalized
@@ -161,10 +212,10 @@ public class MBeanInfoAssembler {
}
// operations
- ManagedOperation mo = method.getAnnotation(ManagedOperation.class);
+ ManagedOperation mo =
cacheInfo.method.getAnnotation(ManagedOperation.class);
if (mo != null) {
String desc = mo.description();
- Method operation = method;
+ Method operation = cacheInfo.method;
operations.add(new ManagedOperationInfo(desc, operation));
}
}
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java?rev=1393469&r1=1393468&r2=1393469&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java
Wed Oct 3 13:01:22 2012
@@ -41,6 +41,14 @@ import org.slf4j.LoggerFactory;
/**
* Helper for introspections of beans.
+ * <p/>
+ * <b>Important: </b> Its recommended to call the {@link #stop()} method when
+ * {@link org.apache.camel.CamelContext} is being stopped. This allows to
clear the introspection cache.
+ * <p/>
+ * This implementation will skip methods from <tt>java.lang.Object</tt> and
<tt>java.lang.reflect.Proxy</tt>.
+ * <p/>
+ * This implementation will use a cache when the {@link #getProperties(Object,
java.util.Map, String)}
+ * method is being used. Also the {@link #cacheClass(Class)} method gives
access to the introspect cache.
*/
public final class IntrospectionSupport {
@@ -48,6 +56,7 @@ public final class IntrospectionSupport
private static final Pattern GETTER_PATTERN =
Pattern.compile("(get|is)[A-Z].*");
private static final Pattern SETTER_PATTERN =
Pattern.compile("set[A-Z].*");
private static final List<Method> EXCLUDED_METHODS = new
ArrayList<Method>();
+ private static final Map<Class<?>, ClassInfo> CACHE = new
LRUSoftCache<Class<?>, ClassInfo>(1000);
static {
// exclude all java.lang.Object methods as we dont want to invoke them
@@ -57,11 +66,39 @@ public final class IntrospectionSupport
}
/**
+ * Structure of an introspected class.
+ */
+ public static final class ClassInfo {
+ public Class<?> clazz;
+ public MethodInfo[] methods;
+ }
+
+ /**
+ * Structure of an introspected method.
+ */
+ public static final class MethodInfo {
+ public Method method;
+ public Boolean isGetter;
+ public Boolean isSetter;
+ public String getterOrSetterShorthandName;
+ public Boolean hasGetterAndSetter;
+ }
+
+ /**
* Utility classes should not have a public constructor.
*/
private IntrospectionSupport() {
}
+ /**
+ * {@link org.apache.camel.CamelContext} should call this stop method when
its stopping.
+ * <p/>
+ * This implementation will clear its introspection cache.
+ */
+ public static void stop() {
+ CACHE.clear();
+ }
+
public static boolean isGetter(Method method) {
String name = method.getName();
Class<?> type = method.getReturnType();
@@ -144,45 +181,98 @@ public final class IntrospectionSupport
optionPrefix = "";
}
- Class<?> clazz = target.getClass();
- Method[] methods = clazz.getMethods();
- for (Method method : methods) {
- if (EXCLUDED_METHODS.contains(method)) {
- continue;
- }
- try {
- // must be properties which have setters
- if (isGetter(method) && hasSetter(target, method)) {
+ ClassInfo cache = cacheClass(target.getClass());
+
+ for (int i = 0; i < cache.methods.length; i++) {
+ MethodInfo info = cache.methods[i];
+ Method method = info.method;
+ // we can only get properties if we have both a getter and a setter
+ if (info.isGetter && info.hasGetterAndSetter) {
+ String name = info.getterOrSetterShorthandName;
+ try {
Object value = method.invoke(target);
- String name = getGetterShorthandName(method);
properties.put(optionPrefix + name, value);
rc = true;
+ } catch (Exception e) {
+ // ignore
}
- } catch (Exception e) {
- // ignore
}
}
return rc;
}
- public static boolean hasSetter(Object target, Method getter) {
- String name = getGetterShorthandName(getter);
-
- Class<?> clazz = target.getClass();
+ /**
+ * Introspects the given class.
+ *
+ * @param clazz the class
+ * @return the introspection result as a {@link ClassInfo} structure.
+ */
+ public static ClassInfo cacheClass(Class<?> clazz) {
+ ClassInfo cache = CACHE.get(clazz);
+ if (cache == null) {
+ cache = doIntrospectClass(clazz);
+ CACHE.put(clazz, cache);
+ }
+ return cache;
+ }
+
+ private static ClassInfo doIntrospectClass(Class<?> clazz) {
+ ClassInfo answer = new ClassInfo();
+ answer.clazz = clazz;
+
+ // loop each method on the class and gather details about the method
+ // especially about getter/setters
+ List<MethodInfo> found = new ArrayList<MethodInfo>();
Method[] methods = clazz.getMethods();
for (Method method : methods) {
if (EXCLUDED_METHODS.contains(method)) {
continue;
}
- if (isSetter(method)) {
- if (name.equals(getSetterShorthandName(method))) {
- return true;
+
+ MethodInfo cache = new MethodInfo();
+ cache.method = method;
+ if (isGetter(method)) {
+ cache.isGetter = true;
+ cache.isSetter = false;
+ cache.getterOrSetterShorthandName =
getGetterShorthandName(method);
+ } else if (isSetter(method)) {
+ cache.isGetter = false;
+ cache.isSetter = true;
+ cache.getterOrSetterShorthandName =
getSetterShorthandName(method);
+ } else {
+ cache.isGetter = false;
+ cache.isSetter = false;
+ cache.hasGetterAndSetter = false;
+ }
+ found.add(cache);
+ }
+
+ // for all getter/setter, find out if there is a corresponding
getter/setter,
+ // so we have a read/write bean property.
+ for (MethodInfo info : found) {
+ info.hasGetterAndSetter = false;
+ if (info.isGetter) {
+ // loop and find the matching setter
+ for (MethodInfo info2 : found) {
+ if (info2.isSetter &&
info.getterOrSetterShorthandName.equals(info2.getterOrSetterShorthandName)) {
+ info.hasGetterAndSetter = true;
+ break;
+ }
+ }
+ } else if (info.isSetter) {
+ // loop and find the matching getter
+ for (MethodInfo info2 : found) {
+ if (info2.isGetter &&
info.getterOrSetterShorthandName.equals(info2.getterOrSetterShorthandName)) {
+ info.hasGetterAndSetter = true;
+ break;
+ }
}
}
}
- return false;
+ answer.methods = found.toArray(new MethodInfo[found.size()]);
+ return answer;
}
public static boolean hasProperties(Map<String, Object> properties, String
optionPrefix) {
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/management/BigRouteTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/BigRouteTest.java?rev=1393469&view=auto
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/management/BigRouteTest.java
(added)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/management/BigRouteTest.java
Wed Oct 3 13:01:22 2012
@@ -0,0 +1,56 @@
+/**
+ * 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 org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.RouteDefinition;
+
+/**
+ *
+ */
+public class BigRouteTest extends ContextTestSupport {
+
+ @Override
+ protected boolean useJmx() {
+ return true;
+ }
+
+ public void testBigRoute() throws Exception {
+ for (int i = 0; i < 1000; i++) {
+ getMockEndpoint("mock:" + i).expectedMessageCount(1);
+ }
+
+ template.sendBody("direct:start", "Hello World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ RouteDefinition route = from("direct:start");
+
+ for (int i = 0; i < 1000; i++) {
+ route = route.to("mock:" + i);
+ }
+ }
+ };
+ }
+}
Propchange:
camel/trunk/camel-core/src/test/java/org/apache/camel/management/BigRouteTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
camel/trunk/camel-core/src/test/java/org/apache/camel/management/BigRouteTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date