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 2009/10/09 10:08:33 UTC

svn commit: r823448 - in /camel/trunk/camel-core/src/main/java/org/apache/camel: impl/ management/ management/mbean/ model/

Author: davsclaus
Date: Fri Oct  9 08:08:33 2009
New Revision: 823448

URL: http://svn.apache.org/viewvc?rev=823448&view=rev
Log:
MANAGEMENT-178: Avoid using ManagedPerformanceCounter as performance counter holders. Introduced delegate performance counter as its a more clean solution. And less confusing for people extending the camel management api.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/DelegatePerformanceCounter.java   (with props)
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/PerformanceCounter.java   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RouteService.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationInterceptStrategy.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationProcessor.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCounter.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedPerformanceCounter.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessDefinition.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RouteService.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RouteService.java?rev=823448&r1=823447&r2=823448&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RouteService.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RouteService.java Fri Oct  9 08:08:33 2009
@@ -114,6 +114,9 @@
                 LOG.trace("Starting route services: " + route);
             }
 
+            // TODO: We should also consider processors which are not services then we can manage all processors as well
+            // otherwise its only the processors which is a Service
+
             List<Service> services = route.getServices();
 
             // callback that we are staring these services

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java?rev=823448&r1=823447&r2=823448&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java Fri Oct  9 08:08:33 2009
@@ -313,7 +313,7 @@
         // only manage if we have a name for it as otherwise we do not want to manage it anyway
         if (managedObject != null) {
             // is it a performance counter then we need to set our counter
-            if (managedObject instanceof ManagedPerformanceCounter) {
+            if (managedObject instanceof PerformanceCounter) {
                 InstrumentationProcessor counter = holder.getValue();
                 if (counter != null) {
                     // change counter to us
@@ -427,14 +427,12 @@
 
         // Create a map (ProcessorType -> PerformanceCounter)
         // to be passed to InstrumentationInterceptStrategy.
-        Map<ProcessorDefinition, ManagedPerformanceCounter> registeredCounters =
-                new HashMap<ProcessorDefinition, ManagedPerformanceCounter>();
+        Map<ProcessorDefinition, PerformanceCounter> registeredCounters =
+                new HashMap<ProcessorDefinition, PerformanceCounter>();
 
-        // Each processor in a route will have its own performance counter
-        // The performance counter are MBeans that we register with MBeanServer.
-        // These performance counter will be embedded
-        // to InstrumentationProcessor and wrap the appropriate processor
-        // by InstrumentationInterceptStrategy.
+        // Each processor in a route will have its own performance counter.
+        // These performance counter will be embedded to InstrumentationProcessor
+        // and wrap the appropriate processor by InstrumentationInterceptStrategy.
         RouteDefinition route = routeContext.getRoute();
 
         // register performance counters for all processors and its children
@@ -449,7 +447,7 @@
 
     @SuppressWarnings("unchecked")
     private void registerPerformanceCounters(RouteContext routeContext, ProcessorDefinition processor,
-                                             Map<ProcessorDefinition, ManagedPerformanceCounter> registeredCounters) {
+                                             Map<ProcessorDefinition, PerformanceCounter> registeredCounters) {
 
         // traverse children if any exists
         List<ProcessorDefinition> children = processor.getOutputs();
@@ -463,9 +461,9 @@
         }
 
         // okay this is a processor we would like to manage so create the
-        // performance counter that is the base for processors
-        ManagedPerformanceCounter pc = new ManagedPerformanceCounter();
-        pc.init(getManagementStrategy());
+        // a delegate performance counter that acts as the placeholder in the interceptor
+        // that then delegates to the real mbean which we register later in the onServiceAdd method
+        DelegatePerformanceCounter pc = new DelegatePerformanceCounter();
         // set statistics enabled depending on the option
         boolean enabled = context.getManagementStrategy().getStatisticsLevel() == ManagementStatisticsLevel.All;
         pc.setStatisticsEnabled(enabled);

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/management/DelegatePerformanceCounter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/DelegatePerformanceCounter.java?rev=823448&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/DelegatePerformanceCounter.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/DelegatePerformanceCounter.java Fri Oct  9 08:08:33 2009
@@ -0,0 +1,68 @@
+/**
+ * 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;
+
+/**
+ * Delegates to another {@link org.apache.camel.management.PerformanceCounter}.
+ * <p/>
+ * This is used to allow Camel to pre initialize these delegate performance counters
+ * when Camel creates the actual route from the model. Then later as the various
+ * processors, routes etc. is created and registered in the {@link org.apache.camel.spi.LifecycleStrategy}
+ * then we link this to the real {@link org.apache.camel.management.mbean.ManagedPerformanceCounter} mbean
+ * so the mbean can gather statistics.
+ * <p/>
+ * This delegation is needed as how Camel is designed to register services in the
+ * {@link org.apache.camel.spi.LifecycleStrategy} in various stages.
+ *
+ * @version $Revision$
+ */
+public class DelegatePerformanceCounter implements PerformanceCounter {
+
+    private PerformanceCounter counter;
+    private boolean statisticsEnabled;
+
+    public DelegatePerformanceCounter() {
+    }
+
+    public void setCounter(PerformanceCounter counter) {
+        this.counter = counter;
+        // init statistics based on the real counter based on how we got initialized
+        this.counter.setStatisticsEnabled(statisticsEnabled);
+    }
+
+    public void completedExchange(long time) {
+        counter.completedExchange(time);
+    }
+
+    public void failedExchange() {
+        counter.failedExchange();
+    }
+
+    public boolean isStatisticsEnabled() {
+        // statistics is only considered enabled if we have a counter to delegate to
+        // otherwise we do not want to gather statistics (we are just a delegate with noone to delegate to)
+        return counter != null && counter.isStatisticsEnabled();
+    }
+
+    public void setStatisticsEnabled(boolean statisticsEnabled) {
+        if (counter != null) {
+            counter.setStatisticsEnabled(statisticsEnabled);
+        } else {
+            this.statisticsEnabled = statisticsEnabled;
+        }
+    }
+}

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/management/DelegatePerformanceCounter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/management/DelegatePerformanceCounter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationInterceptStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationInterceptStrategy.java?rev=823448&r1=823447&r2=823448&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationInterceptStrategy.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationInterceptStrategy.java Fri Oct  9 08:08:33 2009
@@ -37,10 +37,10 @@
  */
 public class InstrumentationInterceptStrategy implements InterceptStrategy {
 
-    private Map<ProcessorDefinition, ManagedPerformanceCounter> registeredCounters;
+    private Map<ProcessorDefinition, PerformanceCounter> registeredCounters;
     private final Map<Processor, KeyValueHolder<ProcessorDefinition, InstrumentationProcessor>> wrappedProcessors;
 
-    public InstrumentationInterceptStrategy(Map<ProcessorDefinition, ManagedPerformanceCounter> registeredCounters,
+    public InstrumentationInterceptStrategy(Map<ProcessorDefinition, PerformanceCounter> registeredCounters,
                                             Map<Processor, KeyValueHolder<ProcessorDefinition, InstrumentationProcessor>> wrappedProcessors) {
         this.registeredCounters = registeredCounters;
         this.wrappedProcessors = wrappedProcessors;
@@ -48,13 +48,13 @@
 
     public Processor wrapProcessorInInterceptors(CamelContext context, ProcessorDefinition definition,
                                                  Processor target, Processor nextTarget) throws Exception {
-        // dont double wrap it
+        // do not double wrap it
         if (target instanceof InstrumentationProcessor) {
             return target;
         }
 
         // only wrap a performance counter if we have it registered in JMX by the jmx agent
-        ManagedPerformanceCounter counter = registeredCounters.get(definition);
+        PerformanceCounter counter = registeredCounters.get(definition);
         if (counter != null) {
             InstrumentationProcessor wrapper = new InstrumentationProcessor(counter);
             wrapper.setProcessor(target);

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationProcessor.java?rev=823448&r1=823447&r2=823448&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationProcessor.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationProcessor.java Fri Oct  9 08:08:33 2009
@@ -31,13 +31,13 @@
 public class InstrumentationProcessor extends DelegateProcessor {
 
     private static final transient Log LOG = LogFactory.getLog(InstrumentationProcessor.class);
-    private ManagedPerformanceCounter counter;
+    private PerformanceCounter counter;
     private String type;
 
     public InstrumentationProcessor() {
     }
 
-    public InstrumentationProcessor(ManagedPerformanceCounter counter) {
+    public InstrumentationProcessor(PerformanceCounter counter) {
         this.counter = counter;
     }
 
@@ -47,11 +47,11 @@
     }
 
     public void setCounter(ManagedPerformanceCounter counter) {
-        this.counter = counter;
-    }
-
-    public ManagedPerformanceCounter getCounter() {
-        return counter;
+        if (this.counter instanceof DelegatePerformanceCounter) {
+            ((DelegatePerformanceCounter) this.counter).setCounter(counter);
+        } else {
+            this.counter = counter;
+        }
     }
 
     public void process(Exchange exchange) throws Exception {

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/management/PerformanceCounter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/PerformanceCounter.java?rev=823448&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/PerformanceCounter.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/PerformanceCounter.java Fri Oct  9 08:08:33 2009
@@ -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;
+
+/**
+ * A counter that gathers performance metrics when {@link org.apache.camel.Exchange} are routed in Camel.
+ *
+ * @version $Revision$
+ */
+public interface PerformanceCounter {
+
+    /**
+     * Executed when an {@link org.apache.camel.Exchange} is complete.
+     *
+     * @param time  the time it took in millis to complete it
+     */
+    void completedExchange(long time);
+
+    /**
+     * Executed when an {@link org.apache.camel.Exchange} failed.
+     */
+    void failedExchange();
+
+    /**
+     * Is statistics enabled.
+     * <p/>
+     * They can be enabled and disabled at runtime
+     *
+     * @return whether statistics is enabled or not
+     */
+    boolean isStatisticsEnabled();
+
+    /**
+     * Sets whether statistics is enabled.
+     * <p/>
+     * They can be enabled and disabled at runtime
+     *
+     * @param statisticsEnabled whether statistics is enabled or not
+     */
+    void setStatisticsEnabled(boolean statisticsEnabled);
+
+}

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/management/PerformanceCounter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/management/PerformanceCounter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCounter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCounter.java?rev=823448&r1=823447&r2=823448&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCounter.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCounter.java Fri Oct  9 08:08:33 2009
@@ -23,7 +23,7 @@
 import org.springframework.jmx.export.annotation.ManagedResource;
 
 @ManagedResource(description = "Managed Counter")
-public class ManagedCounter {
+public abstract class ManagedCounter {
 
     protected Statistic exchangesTotal;
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedPerformanceCounter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedPerformanceCounter.java?rev=823448&r1=823447&r2=823448&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedPerformanceCounter.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedPerformanceCounter.java Fri Oct  9 08:08:33 2009
@@ -18,6 +18,7 @@
 
 import java.util.Date;
 
+import org.apache.camel.management.PerformanceCounter;
 import org.apache.camel.spi.ManagementStrategy;
 import org.fusesource.commons.management.Statistic;
 import org.springframework.jmx.export.annotation.ManagedAttribute;
@@ -25,7 +26,7 @@
 import org.springframework.jmx.export.annotation.ManagedResource;
 
 @ManagedResource(description = "PerformanceCounter")
-public class ManagedPerformanceCounter extends ManagedCounter {
+public abstract class ManagedPerformanceCounter extends ManagedCounter implements PerformanceCounter {
 
     private Statistic exchangesCompleted;
     private Statistic exchangesFailed;

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessDefinition.java?rev=823448&r1=823447&r2=823448&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessDefinition.java Fri Oct  9 08:08:33 2009
@@ -23,6 +23,7 @@
 import javax.xml.bind.annotation.XmlTransient;
 
 import org.apache.camel.Processor;
+import org.apache.camel.Service;
 import org.apache.camel.spi.RouteContext;
 import org.apache.camel.util.ObjectHelper;
 
@@ -86,4 +87,5 @@
         }
         return processor;
     }
+
 }