camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r808866 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/impl/ main/java/org/apache/camel/management/ main/java/org/apache/camel/management/mbean/ main/java/org/apache/camel/model/ main/java/org/apach...
Date Fri, 28 Aug 2009 12:51:34 GMT
Author: davsclaus
Date: Fri Aug 28 12:51:32 2009
New Revision: 808866

URL: http://svn.apache.org/viewvc?rev=808866&view=rev
Log:
CAMEL-1933: Overhaul of JMX. Tracing can now be managed. And can enabled on the fly even if
tracing was not defined when started. So you can start tracing running platforms. Need to
expose more attributes/operations though so you can adjust trace formatting etc.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedTracer.java
  (with props)
    camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedCamelContextTracerTest.java
      - copied, changed from r808754, camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRegisterCamelContextTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteContextTracerTest.java
  (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultRouteContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagedLifecycleStrategy.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementNamingStrategy.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/ManagedManagementStrategy.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DefaultChannel.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceInterceptor.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Tracer.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ManagementNamingStrategy.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RouteContext.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceableUnitOfWorkTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/TraceInterceptorDestinationTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java?rev=808866&r1=808865&r2=808866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java Fri Aug 28 12:51:32
2009
@@ -535,4 +535,11 @@
      */
     void setManagementStrategy(ManagementStrategy strategy);
 
+    /**
+     * Gets the default tracer
+     *
+     * @return the default tracer
+     */
+    InterceptStrategy getDefaultTracer();
+
 }

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=808866&r1=808865&r2=808866&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 Fri
Aug 28 12:51:32 2009
@@ -127,6 +127,7 @@
     // so if we have 6 endpoints in the pool, we have 6 x 100 producers in total
     private ServicePool<Endpoint, Producer> producerServicePool = new DefaultProducerServicePool(100);
     private NodeIdFactory nodeIdFactory = new DefaultNodeIdFactory();
+    private Tracer defaultTracer;
 
     public DefaultCamelContext() {
         super();
@@ -888,12 +889,8 @@
         }
 
         if (isTracing()) {
-            // only add a new tracer if not already configured
-            if (Tracer.getTracer(this) == null) {
-                Tracer tracer = Tracer.createTracer(this);
-                LOG.debug("Tracing is enabled");
-                addInterceptStrategy(tracer);
-            }
+            // tracing is added in the DefaultChannel so we can enable it on the fly
+            LOG.debug("Tracing is enabled");
         }
 
         if (isHandleFault()) {
@@ -1191,6 +1188,13 @@
         this.managementStrategy = managementStrategy;
     }
 
+    public InterceptStrategy getDefaultTracer() {
+        if (defaultTracer == null) {
+            defaultTracer = new Tracer();
+        }
+        return defaultTracer;
+    }
+
     protected synchronized String getEndpointKey(String uri, Endpoint endpoint) {
         if (endpoint.isSingleton()) {
             return uri;

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultRouteContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultRouteContext.java?rev=808866&r1=808865&r2=808866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultRouteContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultRouteContext.java Fri
Aug 28 12:51:32 2009
@@ -49,6 +49,7 @@
     private final List<Processor> eventDrivenProcessors = new ArrayList<Processor>();
     private CamelContext camelContext;
     private List<InterceptStrategy> interceptStrategies = new ArrayList<InterceptStrategy>();
+    private InterceptStrategy managedInterceptStrategy;
     private boolean routeAdded;
     private Boolean trace;
     private Boolean stramCache;
@@ -172,6 +173,14 @@
         getInterceptStrategies().add(interceptStrategy);
     }
 
+    public void setManagedInterceptStrategy(InterceptStrategy interceptStrategy) {
+        this.managedInterceptStrategy = interceptStrategy;
+    }
+
+    public InterceptStrategy getManagedInterceptStrategy() {
+        return managedInterceptStrategy;
+    }
+
     public boolean isRouteAdded() {
         return routeAdded;
     }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagedLifecycleStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagedLifecycleStrategy.java?rev=808866&r1=808865&r2=808866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagedLifecycleStrategy.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagedLifecycleStrategy.java
Fri Aug 28 12:51:32 2009
@@ -45,6 +45,7 @@
 import org.apache.camel.management.mbean.ManagedRoute;
 import org.apache.camel.management.mbean.ManagedSendProcessor;
 import org.apache.camel.management.mbean.ManagedThrottler;
+import org.apache.camel.management.mbean.ManagedTracer;
 import org.apache.camel.model.AOPDefinition;
 import org.apache.camel.model.InterceptDefinition;
 import org.apache.camel.model.OnCompletionDefinition;
@@ -54,6 +55,7 @@
 import org.apache.camel.processor.Delayer;
 import org.apache.camel.processor.SendProcessor;
 import org.apache.camel.processor.Throttler;
+import org.apache.camel.processor.interceptor.Tracer;
 import org.apache.camel.spi.BrowsableEndpoint;
 import org.apache.camel.spi.ClassResolver;
 import org.apache.camel.spi.LifecycleStrategy;
@@ -241,7 +243,10 @@
         }
 
         Object managedObject;
-        if (service instanceof Processor) {
+        if (service instanceof Tracer) {
+            // special for tracer
+            managedObject = new ManagedTracer(context, (Tracer) service);
+        } else if (service instanceof Processor) {
             // special for processors
             managedObject = getManagedObjectForProcessor(context, (Processor) service);
         } else {
@@ -271,7 +276,11 @@
         }
 
         Object managedObject;
-        if (service instanceof Processor) {
+        if (service instanceof Tracer) {
+            // special for tracer
+            managedObject = new ManagedTracer(context, (Tracer) service);
+        } else if (service instanceof Processor) {
+            // special for processors
             managedObject = getManagedObjectForProcessor(context, (Processor) service);
         } else {
             // regular for services
@@ -403,9 +412,9 @@
             registerPerformanceCounters(routeContext, processor, registeredCounters);
         }
 
-        // add intercept strategy that executes the JMX instrumentation for performance metrics
+        // set this managed intercept strategy that executes the JMX instrumentation for
performance metrics
         // so our registered counters can be used for fine grained performance instrumentation
-        routeContext.addInterceptStrategy(new InstrumentationInterceptStrategy(registeredCounters,
wrappedProcessors));
+        routeContext.setManagedInterceptStrategy(new InstrumentationInterceptStrategy(registeredCounters,
wrappedProcessors));
     }
 
     @SuppressWarnings("unchecked")

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementNamingStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementNamingStrategy.java?rev=808866&r1=808865&r2=808866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementNamingStrategy.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementNamingStrategy.java
Fri Aug 28 12:51:32 2009
@@ -32,6 +32,7 @@
 import org.apache.camel.management.mbean.ManagedProcessor;
 import org.apache.camel.management.mbean.ManagedRoute;
 import org.apache.camel.management.mbean.ManagedService;
+import org.apache.camel.management.mbean.ManagedTracer;
 import org.apache.camel.model.ProcessorDefinition;
 import org.apache.camel.spi.ManagementNamingStrategy;
 import org.apache.camel.spi.RouteContext;
@@ -50,6 +51,7 @@
     public static final String TYPE_CONSUMER = "consumers";
     public static final String TYPE_ROUTE = "routes";
     public static final String TYPE_COMPONENT = "components";
+    public static final String TYPE_TRACER = "tracer";
 
     protected String domainName;
     protected String hostName = "localhost";
@@ -138,6 +140,17 @@
         return null;
     }
 
+    public ObjectName getObjectName(ManagedTracer mbean) throws MalformedObjectNameException
{
+        StringBuffer buffer = new StringBuffer();
+        buffer.append(domainName).append(":");
+        buffer.append(KEY_CONTEXT + "=").append(getContextId(mbean.getCamelContext())).append(",");
+        buffer.append(KEY_TYPE + "=" + TYPE_TRACER + ",");
+        buffer.append(KEY_NAME + "=")
+            .append("Tracer")
+            .append("(").append(getIdentityHashCode(mbean.getTracer())).append(")");
+        return createObjectName(buffer);
+    }
+
     public ObjectName getObjectName(ManagedRoute mbean) throws MalformedObjectNameException
{
         Route route = mbean.getRoute();
         Endpoint ep = route.getEndpoint();

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/ManagedManagementStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/ManagedManagementStrategy.java?rev=808866&r1=808865&r2=808866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/ManagedManagementStrategy.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/ManagedManagementStrategy.java
Fri Aug 28 12:51:32 2009
@@ -24,6 +24,7 @@
 import org.apache.camel.management.mbean.ManagedEndpoint;
 import org.apache.camel.management.mbean.ManagedProcessor;
 import org.apache.camel.management.mbean.ManagedRoute;
+import org.apache.camel.management.mbean.ManagedTracer;
 import org.apache.camel.model.ProcessorDefinition;
 import org.apache.camel.spi.ManagementAgent;
 import org.apache.commons.logging.Log;
@@ -98,6 +99,11 @@
             objectName = getManagementNamingStrategy().getObjectName(ms);
         }
 
+        if (managedObject instanceof ManagedTracer) {
+            ManagedTracer mt = (ManagedTracer) managedObject;
+            objectName = getManagementNamingStrategy().getObjectName(mt);
+        }
+
         return nameType.cast(objectName);
     }
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java?rev=808866&r1=808865&r2=808866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
Fri Aug 28 12:51:32 2009
@@ -66,6 +66,16 @@
         return context.getProperties();
     }
 
+    @ManagedAttribute(description = "Tracing")
+    public Boolean getTracing() {
+        return context.isTracing();
+    }
+
+    @ManagedAttribute(description = "Tracing")
+    public void setTracing(Boolean tracing) {
+        context.setTracing(tracing);
+    }
+
     @ManagedOperation(description = "Start Camel")
     public void start() throws Exception {
         context.start();

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java?rev=808866&r1=808865&r2=808866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
Fri Aug 28 12:51:32 2009
@@ -84,6 +84,16 @@
         return context.getName();
     }
 
+    @ManagedAttribute(description = "Tracing")
+    public Boolean getTracing() {
+        return route.getRouteContext().isTracing();
+    }
+
+    @ManagedAttribute(description = "Tracing")
+    public void setTracing(Boolean tracing) {
+        route.getRouteContext().setTracing(tracing);
+    }
+
     @ManagedOperation(description = "Start Route")
     public void start() throws Exception {
         context.startRoute(getRouteId());

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedTracer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedTracer.java?rev=808866&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedTracer.java
(added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedTracer.java
Fri Aug 28 12:51:32 2009
@@ -0,0 +1,60 @@
+/**
+ * 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.mbean;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.processor.interceptor.Tracer;
+import org.springframework.jmx.export.annotation.ManagedAttribute;
+import org.springframework.jmx.export.annotation.ManagedResource;
+
+/**
+ * @version $Revision$
+ */
+@ManagedResource(description = "Managed Tracer")
+public class ManagedTracer extends ManagedPerformanceCounter {
+
+    private CamelContext camelContext;
+    private Tracer tracer;
+
+    public ManagedTracer(CamelContext camelContext, Tracer tracer) {
+        super(camelContext.getManagementStrategy());
+        this.camelContext = camelContext;
+        this.tracer = tracer;
+    }
+
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    public Tracer getTracer() {
+        return tracer;
+    }
+
+    @ManagedAttribute(description = "Enabled")
+    public boolean getEnabled() {
+        return tracer.isEnabled();
+    }
+
+    @ManagedAttribute(description = "Enabled")
+    public void setEnabled(boolean enabled) {
+        tracer.setEnabled(enabled);
+    }
+
+    // TODO: add management for formatter etc.
+
+
+}

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

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

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java?rev=808866&r1=808865&r2=808866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java Fri
Aug 28 12:51:32 2009
@@ -166,6 +166,9 @@
         // add interceptor strategies to the channel must be in this order: camel context,
route context, local
         addInterceptStrategies(routeContext, channel, routeContext.getCamelContext().getInterceptStrategies());
         addInterceptStrategies(routeContext, channel, routeContext.getInterceptStrategies());
+        if (routeContext.getManagedInterceptStrategy() != null) {
+            channel.addInterceptStrategy(routeContext.getManagedInterceptStrategy());
+        }
         addInterceptStrategies(routeContext, channel, this.getInterceptStrategies());
         
         // init the channel
@@ -193,10 +196,6 @@
      */
     protected void addInterceptStrategies(RouteContext routeContext, Channel channel, List<InterceptStrategy>
strategies) {
         for (InterceptStrategy strategy : strategies) {
-            if (!routeContext.isTracing() && strategy instanceof Tracer) {
-                // trace is disabled so we should not add it
-                continue;
-            }
             if (!routeContext.isStreamCaching() && strategy instanceof StreamCaching)
{
                 // stream cache is disabled so we should not add it
                 continue;

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java?rev=808866&r1=808865&r2=808866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java Fri Aug
28 12:51:32 2009
@@ -373,11 +373,7 @@
                 if (log.isDebugEnabled()) {
                     log.debug("Tracing is enabled on route: " + this);
                 }
-                // only add a new tracer if not already a global configured on camel context
-                if (Tracer.getTracer(camelContext) == null) {
-                    Tracer tracer = Tracer.createTracer(camelContext);
-                    addInterceptStrategy(tracer);
-                }
+                // tracing is added in the DefaultChannel so we can enable it on the fly
             }
         }
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DefaultChannel.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DefaultChannel.java?rev=808866&r1=808865&r2=808866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DefaultChannel.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DefaultChannel.java Fri
Aug 28 12:51:32 2009
@@ -18,13 +18,19 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
+import org.apache.camel.CamelContext;
 import org.apache.camel.Channel;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
+import org.apache.camel.Service;
 import org.apache.camel.impl.ServiceSupport;
 import org.apache.camel.model.ProcessorDefinition;
+import org.apache.camel.processor.interceptor.TraceInterceptor;
+import org.apache.camel.processor.interceptor.Tracer;
 import org.apache.camel.spi.InterceptStrategy;
+import org.apache.camel.spi.LifecycleStrategy;
 import org.apache.camel.spi.RouteContext;
 import org.apache.camel.util.ServiceHelper;
 import org.apache.commons.logging.Log;
@@ -53,6 +59,8 @@
     // the real output to invoke that has been wrapped
     private Processor output;
     private ProcessorDefinition definition;
+    private RouteContext routeContext;
+    private CamelContext camelContext;
 
     public List<Processor> next() {
         List<Processor> answer = new ArrayList<Processor>(1);
@@ -128,13 +136,32 @@
 
     public void initChannel(ProcessorDefinition outputDefinition, RouteContext routeContext)
throws Exception {
         this.definition = outputDefinition;
+        this.routeContext = routeContext;
+        this.camelContext = routeContext.getCamelContext();
 
-        // TODO: Support ordering of interceptors
-
-        // wrap the output with the interceptors
         Processor target = nextProcessor;
+        Processor next;
+
+        // first wrap the output with the managed strategy if any
+        InterceptStrategy managed = routeContext.getManagedInterceptStrategy();
+        if (managed != null) {
+            next = target == nextProcessor ? null : nextProcessor;
+            target = managed.wrapProcessorInInterceptors(routeContext.getCamelContext(),
outputDefinition, target, next);
+        }
+
+        // then wrap the output with the tracer
+        TraceInterceptor trace = (TraceInterceptor) getOrCreateTracer().wrapProcessorInInterceptors(routeContext.getCamelContext(),
outputDefinition, target, null);
+        // trace interceptor need to have a reference to route context so we at runtime can
enable/disable tracing on-the-fly
+        trace.setRouteContext(routeContext);
+        target = trace;
+
+        // wrap the output with the configured interceptors
         for (InterceptStrategy strategy : interceptors) {
-            Processor next = target == nextProcessor ? null : nextProcessor;
+            next = target == nextProcessor ? null : nextProcessor;
+            // skip tracer as we did the specially beforehand and it could potentially be
added as an interceptor strategy
+            if (strategy instanceof Tracer) {
+                continue;
+            }
             target = strategy.wrapProcessorInInterceptors(routeContext.getCamelContext(),
outputDefinition, target, next);
         }
 
@@ -142,6 +169,29 @@
         output = target;
     }
 
+    private InterceptStrategy getOrCreateTracer() {
+        InterceptStrategy tracer = Tracer.getTracer(camelContext);
+        if (tracer == null) {
+            // lookup in registry
+            Map<String, Tracer> map = camelContext.getRegistry().lookupByType(Tracer.class);
+            if (map.size() == 1) {
+                tracer = map.values().iterator().next();
+            } else {
+                // fallback to use the default tracer
+                tracer = camelContext.getDefaultTracer();
+            }
+        }
+
+        // which we must manage as well
+        for (LifecycleStrategy strategy : camelContext.getLifecycleStrategies()) {
+            if (tracer instanceof Service) {
+                strategy.onServiceAdd(camelContext, (Service) tracer);
+            }
+        }
+
+        return tracer;
+    }
+
     public void process(Exchange exchange) throws Exception {
         Processor processor = getOutput();
         if (processor != null && continueProcessing(exchange)) {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceInterceptor.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceInterceptor.java?rev=808866&r1=808865&r2=808866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceInterceptor.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/TraceInterceptor.java
Fri Aug 28 12:51:32 2009
@@ -35,6 +35,7 @@
 import org.apache.camel.processor.Logger;
 import org.apache.camel.spi.ExchangeFormatter;
 import org.apache.camel.spi.InterceptStrategy;
+import org.apache.camel.spi.RouteContext;
 import org.apache.camel.spi.TraceableUnitOfWork;
 import org.apache.camel.util.IntrospectionSupport;
 import org.apache.camel.util.ObjectHelper;
@@ -56,6 +57,7 @@
     private final Tracer tracer;
     private TraceFormatter formatter;
     private Class jpaTraceEventMessageClass;
+    private RouteContext routeContext;
 
     public TraceInterceptor(ProcessorDefinition node, Processor target, TraceFormatter formatter,
Tracer tracer) {
         super(target);
@@ -90,7 +92,18 @@
         return "TraceInterceptor[" + node + "]";
     }
 
+    public void setRouteContext(RouteContext routeContext) {
+        this.routeContext = routeContext;
+    }
+
     public void process(final Exchange exchange) throws Exception {
+        // do not trace if tracing is disabled
+        if (!tracer.isEnabled() || (routeContext != null && !routeContext.isTracing()))
{
+            super.proceed(exchange);
+            return;
+        }
+
+
         // interceptor will also trace routes supposed only for TraceEvents so we need to
skip
         // logging TraceEvents to avoid infinite looping
         if (exchange.getProperty(Exchange.TRACE_EVENT, Boolean.class) != null) {
@@ -178,7 +191,10 @@
         return formatter;
     }
 
-    
+    public Tracer getTracer() {
+        return tracer;
+    }
+
     // Implementation methods
     //-------------------------------------------------------------------------
     protected boolean beforeOnException(OnExceptionDefinition onException, TraceableUnitOfWork
tuow, Exchange exchange) throws Exception {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Tracer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Tracer.java?rev=808866&r1=808865&r2=808866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Tracer.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Tracer.java
Fri Aug 28 12:51:32 2009
@@ -23,6 +23,7 @@
 import org.apache.camel.LoggingLevel;
 import org.apache.camel.Predicate;
 import org.apache.camel.Processor;
+import org.apache.camel.Service;
 import org.apache.camel.model.ProcessorDefinition;
 import org.apache.camel.spi.InterceptStrategy;
 
@@ -31,7 +32,7 @@
  *
  * @version $Revision$
  */
-public class Tracer implements InterceptStrategy {
+public class Tracer implements InterceptStrategy, Service {
 
     private TraceFormatter formatter = new DefaultTraceFormatter();
     private boolean enabled = true;
@@ -231,4 +232,10 @@
     public String toString() {
         return "Tracer";
     }
+
+    public void start() throws Exception {
+    }
+
+    public void stop() throws Exception {
+    }
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ManagementNamingStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ManagementNamingStrategy.java?rev=808866&r1=808865&r2=808866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ManagementNamingStrategy.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ManagementNamingStrategy.java
Fri Aug 28 12:51:32 2009
@@ -26,6 +26,7 @@
 import org.apache.camel.management.mbean.ManagedProcessor;
 import org.apache.camel.management.mbean.ManagedRoute;
 import org.apache.camel.management.mbean.ManagedService;
+import org.apache.camel.management.mbean.ManagedTracer;
 import org.apache.camel.model.ProcessorDefinition;
 
 /**
@@ -45,6 +46,8 @@
 
     ObjectName getObjectName(ManagedConsumer mbean) throws MalformedObjectNameException;
 
+    ObjectName getObjectName(ManagedTracer mbean) throws MalformedObjectNameException;
+
     /**
      * @deprecated
      */

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RouteContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RouteContext.java?rev=808866&r1=808865&r2=808866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RouteContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RouteContext.java Fri Aug 28
12:51:32 2009
@@ -141,6 +141,23 @@
     void addInterceptStrategy(InterceptStrategy interceptStrategy);
 
     /**
+     * Sets a special intercept strategy for management.
+     * <p/>
+     * Is by default used to correlate managed performance counters with processors
+     * when the runtime route is being constructed
+     *
+     * @param interceptStrategy the managed intercept strategy
+     */
+    void setManagedInterceptStrategy(InterceptStrategy interceptStrategy);
+
+    /**
+     * Gets the special managed intercept strategy if any
+     *
+     * @return the managed intercept strategy, or <tt>null</tt> if not managed
+     */
+    InterceptStrategy getManagedInterceptStrategy();
+
+    /**
      * If this flag is true, {@link ProcessorDefinition#addRoutes(RouteContext, java.util.Collection)}
      * will not add processor to addEventDrivenProcessor to the RouteContext and it
      * will prevent from adding an EventDrivenRoute.

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedCamelContextTracerTest.java
(from r808754, camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRegisterCamelContextTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedCamelContextTracerTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedCamelContextTracerTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRegisterCamelContextTest.java&r1=808754&r2=808866&rev=808866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRegisterCamelContextTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedCamelContextTracerTest.java
Fri Aug 28 12:51:32 2009
@@ -16,18 +16,21 @@
  */
 package org.apache.camel.management;
 
+import javax.management.Attribute;
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.ContextTestSupport;
-import org.apache.camel.ServiceStatus;
+import org.apache.camel.LoggingLevel;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.processor.interceptor.Tracer;
 
 /**
  * @version $Revision$
  */
-public class ManagedRegisterCamelContextTest extends ContextTestSupport {
+public class ManagedCamelContextTracerTest extends ContextTestSupport {
 
     @Override
     protected CamelContext createCamelContext() throws Exception {
@@ -39,15 +42,37 @@
     }
 
     @SuppressWarnings("unchecked")
-    public void testRegisterCamelContext() throws Exception {
+    public void testCamelContextTracing() throws Exception {
         MBeanServer mbeanServer = context.getManagementStrategy().getManagementAgent().getMBeanServer();
 
         ObjectName on = ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=context,name=\"camel-1\"");
-        String name = (String) mbeanServer.getAttribute(on, "CamelId");
-        assertEquals("camel-1", name);
 
-        String state = (String) mbeanServer.getAttribute(on, "State");
-        assertEquals(ServiceStatus.Started.name(), state);
+        // with tracing
+        MockEndpoint traced = getMockEndpoint("mock:traced");
+        traced.setExpectedMessageCount(2);
+        MockEndpoint result = getMockEndpoint("mock:result");
+        result.setExpectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+
+        // should be enabled
+        Boolean tracing = (Boolean) mbeanServer.getAttribute(on, "Tracing");
+        assertEquals("Tracing should be enabled", true, tracing.booleanValue());
+
+        // now disable tracing
+        mbeanServer.setAttribute(on, new Attribute("Tracing", Boolean.FALSE));
+
+        // without tracing
+        traced.reset();
+        traced.setExpectedMessageCount(0);
+        result.reset();
+        result.setExpectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
     }
 
     @Override
@@ -55,9 +80,14 @@
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
+                Tracer tracer = new Tracer();
+                tracer.setDestinationUri("mock:traced");
+                tracer.setLogLevel(LoggingLevel.OFF);
+                context.addInterceptStrategy(tracer);
+
                 from("direct:start").to("log:foo").to("mock:result");
             }
         };
     }
 
-}
+}
\ No newline at end of file

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteContextTracerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteContextTracerTest.java?rev=808866&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteContextTracerTest.java
(added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteContextTracerTest.java
Fri Aug 28 12:51:32 2009
@@ -0,0 +1,105 @@
+/**
+ * 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 javax.management.Attribute;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.LoggingLevel;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.processor.interceptor.Tracer;
+
+/**
+ * @version $Revision$
+ */
+public class ManagedRouteContextTracerTest extends ContextTestSupport {
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        DefaultManagementNamingStrategy naming = (DefaultManagementNamingStrategy) context.getManagementStrategy().getManagementNamingStrategy();
+        naming.setHostName("localhost");
+        naming.setDomainName("org.apache.camel");
+        return context;
+    }
+
+    @SuppressWarnings("unchecked")
+    public void testRouteTracing() throws Exception {
+        MBeanServer mbeanServer = context.getManagementStrategy().getManagementAgent().getMBeanServer();
+
+        ObjectName on1 = ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=routes,name=\"route1\"");
+        ObjectName on2 = ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=routes,name=\"route2\"");
+
+        // with tracing
+        MockEndpoint traced = getMockEndpoint("mock:traced");
+        traced.setExpectedMessageCount(2);
+        MockEndpoint result = getMockEndpoint("mock:result");
+        result.setExpectedMessageCount(1);
+        MockEndpoint foo = getMockEndpoint("mock:foo");
+        foo.setExpectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+        template.sendBody("direct:foo", "Hello World");
+
+        assertMockEndpointsSatisfied();
+
+        // should be enabled for route 1
+        Boolean tracing = (Boolean) mbeanServer.getAttribute(on1, "Tracing");
+        assertEquals("Tracing should be enabled for route 1", true, tracing.booleanValue());
+
+        // should be disabled for route 2
+        Boolean tracing2 = (Boolean) mbeanServer.getAttribute(on2, "Tracing");
+        assertEquals("Tracing should be disabled for route 2", false, tracing2.booleanValue());
+
+        // now enable tracing on route 2
+        mbeanServer.setAttribute(on2, new Attribute("Tracing", Boolean.TRUE));
+
+        // with tracing
+        traced.reset();
+        traced.setExpectedMessageCount(1);
+        result.reset();
+        result.setExpectedMessageCount(0);
+        foo.reset();
+        foo.setExpectedMessageCount(1);
+
+        template.sendBody("direct:foo", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                Tracer tracer = new Tracer();
+                tracer.setDestinationUri("mock:traced");
+                tracer.setLogLevel(LoggingLevel.OFF);
+                context.addInterceptStrategy(tracer);
+
+                from("direct:start").to("log:foo").to("mock:result");
+
+                from("direct:foo").noTracing().to("mock:foo");
+            }
+        };
+    }
+
+}

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

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

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceableUnitOfWorkTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceableUnitOfWorkTest.java?rev=808866&r1=808865&r2=808866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceableUnitOfWorkTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/TraceableUnitOfWorkTest.java
Fri Aug 28 12:51:32 2009
@@ -24,6 +24,7 @@
 import org.apache.camel.RouteNode;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.management.InstrumentationProcessor;
 import org.apache.camel.spi.TraceableUnitOfWork;
 
 /**
@@ -76,6 +77,12 @@
             // get the 3rd last as its the bean
             Processor last = list.get(list.size() - 3).getProcessor();
 
+            // wrapped by JMX
+            if (last instanceof InstrumentationProcessor) {
+                InstrumentationProcessor ip = (InstrumentationProcessor) last;
+                last = ip.getProcessor();
+            }
+
             // set error message
             exchange.getOut().setFault(true);
             exchange.getOut().setBody("Failed at: " + last.toString());

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/TraceInterceptorDestinationTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/TraceInterceptorDestinationTest.java?rev=808866&r1=808865&r2=808866&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/TraceInterceptorDestinationTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/TraceInterceptorDestinationTest.java
Fri Aug 28 12:51:32 2009
@@ -91,7 +91,7 @@
                 // we disable regular trace logging in the log file. You can omit this and
                 // have both.
                 tracer.setLogLevel(LoggingLevel.OFF);
-                // and we must remeber to add the tracer to Camel
+                // and we must remember to add the tracer to Camel
                 getContext().addInterceptStrategy(tracer);
                 // END SNIPPET: e1
 



Mime
View raw message