camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject svn commit: r1179153 - in /camel/trunk/camel-core/src/main/java/org/apache/camel: management/ management/mbean/ processor/interceptor/
Date Wed, 05 Oct 2011 10:46:45 GMT
Author: cschneider
Date: Wed Oct  5 10:46:45 2011
New Revision: 1179153

URL: http://svn.apache.org/viewvc?rev=1179153&view=rev
Log:
CAMEL-4500 Adding a TraceEventHandler that supports JMX Notifications. Moving the standard
trace logic to a new TraceEventHandler

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/JMXNotificationTraceEventHandler.java
  (with props)
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceEventHandler.java
  (with props)
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DispatchingTraceEventHandler.java
  (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedTracer.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

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=1179153&r1=1179152&r2=1179153&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
Wed Oct  5 10:46:45 2011
@@ -101,6 +101,7 @@ public class DefaultManagementLifecycleS
     private CamelContext camelContext;
     private volatile boolean initialized;
     private final Set<String> knowRouteIds = new HashSet<String>();
+    private Map<Object, ManagedTracer> managedTracers = new HashMap<Object, ManagedTracer>();
 
     public DefaultManagementLifecycleStrategy() {
     }
@@ -395,9 +396,14 @@ public class DefaultManagementLifecycleS
             return ((ManagementAware) service).getManagedObject(service);
         } else if (service instanceof Tracer) {
             // special for tracer
-            ManagedTracer mt = new ManagedTracer(context, (Tracer) service);
-            mt.init(getManagementStrategy());
-            return mt;
+            Object mo = this.managedTracers.get(service);
+            if (mo == null) {
+                ManagedTracer mt = new ManagedTracer(context, (Tracer) service);
+                mt.init(getManagementStrategy());
+                this.managedTracers.put(service, mt);
+                mo = mt;
+            }
+            return mo;
         } else if (service instanceof EventNotifier) {
             answer = getManagementObjectStrategy().getManagedObjectForEventNotifier(context,
(EventNotifier) service);
         } else if (service instanceof Producer) {

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/JMXNotificationTraceEventHandler.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/JMXNotificationTraceEventHandler.java?rev=1179153&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/JMXNotificationTraceEventHandler.java
(added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/JMXNotificationTraceEventHandler.java
Wed Oct  5 10:46:45 2011
@@ -0,0 +1,108 @@
+/**
+ * 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 java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.management.Notification;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.Traceable;
+import org.apache.camel.api.management.NotificationSender;
+import org.apache.camel.api.management.NotificationSenderAware;
+import org.apache.camel.model.ProcessorDefinition;
+import org.apache.camel.processor.interceptor.TraceEventHandler;
+import org.apache.camel.processor.interceptor.TraceInterceptor;
+
+public final class JMXNotificationTraceEventHandler implements TraceEventHandler, NotificationSenderAware
{
+    private long num;
+    private NotificationSender notificationSender;
+
+    public JMXNotificationTraceEventHandler() {
+    }
+
+    @SuppressWarnings("rawtypes")
+    public void traceExchangeOut(ProcessorDefinition node, Processor target, TraceInterceptor
traceInterceptor, Exchange exchange, Object traceState) throws Exception {
+    }
+
+    @SuppressWarnings("rawtypes")
+    public Object traceExchangeIn(ProcessorDefinition node, Processor target, TraceInterceptor
traceInterceptor, Exchange exchange) throws Exception {
+        return null;
+    }
+
+    @SuppressWarnings("rawtypes")
+    public void traceExchange(ProcessorDefinition node, Processor target, TraceInterceptor
traceInterceptor, Exchange exchange) throws Exception {
+        if (notificationSender != null) {
+            String body = exchange.getIn().getBody(String.class);
+            if (body == null) {
+                body = "";
+            }
+            String message = body.substring(0, Math.min(body.length(), 60));
+            Map tm = createTraceMessage(node, exchange, body);
+
+            Notification notification = new Notification("TraceNotification", exchange.toString(),
num++, System.currentTimeMillis(), message);
+            notification.setUserData(tm);
+
+            notificationSender.sendNotification(notification);
+        }
+
+    }
+
+    private Map<String, Object> createTraceMessage(ProcessorDefinition<?> node,
Exchange exchange, String body) {
+        Map<String, Object> mi = new HashMap<String, Object>();
+        mi.put("ExchangeId", exchange.getExchangeId());
+        mi.put("EndpointURI", getEndpointUri(node));
+        mi.put("TimeStamp", new Date(System.currentTimeMillis()));
+        mi.put("Body", body);
+
+        Message message = exchange.getIn();
+        Map<String, Object> sHeaders = message.getHeaders();
+        Map<String, Object> sProperties = exchange.getProperties();
+
+        Map<String, String> headers = new HashMap<String, String>();
+        for (String key : sHeaders.keySet()) {
+            headers.put(key, message.getHeader(key, String.class));
+        }
+        mi.put("Headers", headers);
+
+        Map<String, String> properties = new HashMap<String, String>();
+        for (String key : sProperties.keySet()) {
+            properties.put(key, exchange.getProperty(key, String.class));
+        }
+        mi.put("Properties", properties);
+        return mi;
+    }
+
+    private String getEndpointUri(ProcessorDefinition<?> node) {
+        if (node instanceof Traceable) {
+            Traceable tr = (Traceable)node;
+            return tr.getTraceLabel();
+        } else {
+            return node.getLabel();
+        }
+    }
+
+    @Override
+    public void setNotificationSender(NotificationSender sender) {
+        this.notificationSender = sender;
+    }
+
+}

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/JMXNotificationTraceEventHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 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=1179153&r1=1179152&r2=1179153&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedTracer.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedTracer.java
Wed Oct  5 10:46:45 2011
@@ -19,7 +19,13 @@ package org.apache.camel.management.mbea
 import org.apache.camel.CamelContext;
 import org.apache.camel.LoggingLevel;
 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.ManagedResource;
+import org.apache.camel.api.management.NotificationSender;
+import org.apache.camel.api.management.NotificationSenderAware;
+import org.apache.camel.processor.interceptor.DispatchingTraceEventHandler;
+import org.apache.camel.processor.interceptor.TraceEventHandler;
 import org.apache.camel.processor.interceptor.Tracer;
 import org.apache.camel.spi.ManagementStrategy;
 import org.apache.camel.util.ObjectHelper;
@@ -28,13 +34,27 @@ import org.apache.camel.util.ObjectHelpe
  * @version 
  */
 @ManagedResource(description = "Managed Tracer")
-public class ManagedTracer {
+@ManagedNotifications(@ManagedNotification(name = "javax.management.Notification", 
+    description = "Fine grained trace events", 
+    notificationTypes = {"TraceNotification"}))
+public class ManagedTracer implements NotificationSenderAware {
     private final CamelContext camelContext;
     private final Tracer tracer;
+    private JMXNotificationTraceEventHandler jmxTraceHandler;
 
     public ManagedTracer(CamelContext camelContext, Tracer tracer) {
         this.camelContext = camelContext;
         this.tracer = tracer;
+        TraceEventHandler oldHandler = tracer.getTraceHandler();
+        jmxTraceHandler = new JMXNotificationTraceEventHandler();
+        if (oldHandler != null) {
+            DispatchingTraceEventHandler teh = new DispatchingTraceEventHandler();
+            teh.addHandler(oldHandler);
+            teh.addHandler(jmxTraceHandler);
+            tracer.setTraceHandler(teh);
+        } else {
+            tracer.setTraceHandler(jmxTraceHandler);
+        }
     }
 
     public void init(ManagementStrategy strategy) {
@@ -410,4 +430,9 @@ public class ManagedTracer {
         tracer.getDefaultTraceFormatter().setMaxChars(maxChars);
     }
 
+    @Override
+    public void setNotificationSender(NotificationSender sender) {
+        jmxTraceHandler.setNotificationSender(sender);
+    }
+
 }

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceEventHandler.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceEventHandler.java?rev=1179153&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceEventHandler.java
(added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceEventHandler.java
Wed Oct  5 10:46:45 2011
@@ -0,0 +1,148 @@
+/**
+ * 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.processor.interceptor;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.Service;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.model.ProcessorDefinition;
+import org.apache.camel.util.IntrospectionSupport;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.ServiceHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DefaultTraceEventHandler implements TraceEventHandler, Service {
+    private static final transient Logger LOG = LoggerFactory.getLogger(DefaultTraceEventHandler.class);
+    
+    private Producer traceEventProducer;
+    private Class<?> jpaTraceEventMessageClass;
+    private String jpaTraceEventMessageClassName;
+
+    private final Tracer tracer;
+    
+    public DefaultTraceEventHandler(Tracer tracer) {
+        this.tracer = tracer;
+    }
+
+    private synchronized void loadJpaTraceEventMessageClass(Exchange exchange) {
+        if (jpaTraceEventMessageClass == null) {
+            jpaTraceEventMessageClassName = tracer.getJpaTraceEventMessageClassName();
+        }
+        if (jpaTraceEventMessageClass == null) {
+            jpaTraceEventMessageClass = exchange.getContext().getClassResolver().resolveClass(jpaTraceEventMessageClassName);
+            if (jpaTraceEventMessageClass == null) {
+                throw new IllegalArgumentException("Cannot find class: " + jpaTraceEventMessageClassName
+                        + ". Make sure camel-jpa.jar is in the classpath.");
+            }
+        }
+    }
+
+    private synchronized Producer getTraceEventProducer(Exchange exchange) throws Exception
{
+        if (traceEventProducer == null) {
+            // create producer when we have access the the camel context (we dont in doStart)
+            Endpoint endpoint = tracer.getDestination() != null ? tracer.getDestination()
: exchange.getContext().getEndpoint(tracer.getDestinationUri());
+            traceEventProducer = endpoint.createProducer();
+            ServiceHelper.startService(traceEventProducer);
+        }
+        return traceEventProducer;
+    }
+
+    @Override
+    public void traceExchange(ProcessorDefinition node, Processor target, TraceInterceptor
traceInterceptor, Exchange exchange) throws Exception {
+        if (tracer.getDestination() != null || tracer.getDestinationUri() != null) {
+
+            // create event exchange and add event information
+            Date timestamp = new Date();
+            Exchange event = new DefaultExchange(exchange);
+            event.setProperty(Exchange.TRACE_EVENT_NODE_ID, node.getId());
+            event.setProperty(Exchange.TRACE_EVENT_TIMESTAMP, timestamp);
+            // keep a reference to the original exchange in case its needed
+            event.setProperty(Exchange.TRACE_EVENT_EXCHANGE, exchange);
+
+            // create event message to sent as in body containing event information such
as
+            // from node, to node, etc.
+            TraceEventMessage msg = new DefaultTraceEventMessage(timestamp, node, exchange);
+
+            // should we use ordinary or jpa objects
+            if (tracer.isUseJpa()) {
+                if (LOG.isTraceEnabled()) {
+                    LOG.trace("Using class: " + this.jpaTraceEventMessageClassName + " for
tracing event messages");
+                }
+
+                // load the jpa event message class
+                loadJpaTraceEventMessageClass(exchange);
+                // create a new instance of the event message class
+                Object jpa = ObjectHelper.newInstance(jpaTraceEventMessageClass);
+
+                // copy options from event to jpa
+                Map<String, Object> options = new HashMap<String, Object>();
+                IntrospectionSupport.getProperties(msg, options, null);
+                IntrospectionSupport.setProperties(jpa, options);
+                // and set the timestamp as its not a String type
+                IntrospectionSupport.setProperty(jpa, "timestamp", msg.getTimestamp());
+
+                event.getIn().setBody(jpa);
+            } else {
+                event.getIn().setBody(msg);
+            }
+
+            // marker property to indicate its a tracing event being routed in case
+            // new Exchange instances is created during trace routing so we can check
+            // for this marker when interceptor also kick in during routing of trace events
+            event.setProperty(Exchange.TRACE_EVENT, Boolean.TRUE);
+            try {
+                // process the trace route
+                getTraceEventProducer(exchange).process(event);
+            } catch (Exception e) {
+                // log and ignore this as the original Exchange should be allowed to continue
+                LOG.error("Error processing trace event (original Exchange will continue):
" + event, e);
+            }
+        }
+    }
+
+    @Override
+    public Object traceExchangeIn(ProcessorDefinition node, Processor target, TraceInterceptor
traceInterceptor, Exchange exchange) throws Exception {
+        traceExchange(node, target, traceInterceptor, exchange);
+        return null;
+    }
+
+    @Override
+    public void traceExchangeOut(ProcessorDefinition node, Processor target, TraceInterceptor
traceInterceptor, Exchange exchange, Object traceState) throws Exception {
+        traceExchange(node, target, traceInterceptor, exchange);
+    }
+
+    @Override
+    public void start() throws Exception {
+        traceEventProducer = null;
+    }
+
+    @Override
+    public void stop() throws Exception {
+        if (traceEventProducer != null) {
+            ServiceHelper.stopService(traceEventProducer);
+        }
+    }
+
+}

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultTraceEventHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DispatchingTraceEventHandler.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DispatchingTraceEventHandler.java?rev=1179153&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DispatchingTraceEventHandler.java
(added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DispatchingTraceEventHandler.java
Wed Oct  5 10:46:45 2011
@@ -0,0 +1,61 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor.interceptor;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.model.ProcessorDefinition;
+
+public class DispatchingTraceEventHandler implements TraceEventHandler {
+    
+    private Set<TraceEventHandler> handlers = new HashSet<TraceEventHandler>();
+    
+    public void addHandler(TraceEventHandler handler) {
+        handlers.add(handler);
+    }
+    
+    public void removeHandler(TraceEventHandler handler) {
+        handlers.remove(handler);
+    }
+
+    @Override
+    public void traceExchange(ProcessorDefinition node, Processor target, TraceInterceptor
traceInterceptor, Exchange exchange) throws Exception {
+        for (TraceEventHandler handler : handlers) {
+            handler.traceExchange(node, target, traceInterceptor, exchange);
+        }
+    }
+
+    @Override
+    public Object traceExchangeIn(ProcessorDefinition node, Processor target, TraceInterceptor
traceInterceptor, Exchange exchange) throws Exception {
+        Object result = null;
+        for (TraceEventHandler handler : handlers) {
+            result = handler.traceExchangeIn(node, target, traceInterceptor, exchange);
+        }
+        return result;
+    }
+
+    @Override
+    public void traceExchangeOut(ProcessorDefinition node, Processor target, TraceInterceptor
traceInterceptor, Exchange exchange, Object traceState) throws Exception {
+        for (TraceEventHandler handler : handlers) {
+            handler.traceExchangeOut(node, target, traceInterceptor, exchange, traceState);
+        }
+    }
+
+}

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/DispatchingTraceEventHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

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=1179153&r1=1179152&r2=1179153&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
Wed Oct  5 10:46:45 2011
@@ -16,17 +16,10 @@
  */
 package org.apache.camel.processor.interceptor;
 
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
-
 import org.apache.camel.AsyncCallback;
-import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
-import org.apache.camel.Producer;
 import org.apache.camel.impl.AggregateRouteNode;
-import org.apache.camel.impl.DefaultExchange;
 import org.apache.camel.impl.DefaultRouteNode;
 import org.apache.camel.impl.DoCatchRouteNode;
 import org.apache.camel.impl.DoFinallyRouteNode;
@@ -46,8 +39,6 @@ import org.apache.camel.spi.ExchangeForm
 import org.apache.camel.spi.InterceptStrategy;
 import org.apache.camel.spi.RouteContext;
 import org.apache.camel.spi.TracedRouteNodes;
-import org.apache.camel.util.IntrospectionSupport;
-import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.ServiceHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -61,16 +52,15 @@ public class TraceInterceptor extends De
     private static final transient Logger LOG = LoggerFactory.getLogger(TraceInterceptor.class);
 
     private CamelLogProcessor logger;
-    private Producer traceEventProducer;
-    private final ProcessorDefinition node;
+
+    private final ProcessorDefinition<?> node;
     private final Tracer tracer;
     private TraceFormatter formatter;
-    private Class<?> jpaTraceEventMessageClass;
+
     private RouteContext routeContext;
     private TraceEventHandler traceHandler;
-    private String jpaTraceEventMessageClassName;
 
-    public TraceInterceptor(ProcessorDefinition node, Processor target, TraceFormatter formatter,
Tracer tracer) {
+    public TraceInterceptor(ProcessorDefinition<?> node, Processor target, TraceFormatter
formatter, Tracer tracer) {
         super(target);
         this.tracer = tracer;
         this.node = node;
@@ -80,7 +70,6 @@ public class TraceInterceptor extends De
             this.formatter = tracer.getFormatter();
         }
         this.traceHandler = tracer.getTraceHandler();
-        this.jpaTraceEventMessageClassName = tracer.getJpaTraceEventMessageClassName();
     }
 
     @Override
@@ -307,64 +296,6 @@ public class TraceInterceptor extends De
     protected void traceExchange(Exchange exchange) throws Exception {
         if (traceHandler != null) {
             traceHandler.traceExchange(node, processor, this, exchange);
-        } else if (tracer.getDestination() != null || tracer.getDestinationUri() != null)
{
-
-            // create event exchange and add event information
-            Date timestamp = new Date();
-            Exchange event = new DefaultExchange(exchange);
-            event.setProperty(Exchange.TRACE_EVENT_NODE_ID, node.getId());
-            event.setProperty(Exchange.TRACE_EVENT_TIMESTAMP, timestamp);
-            // keep a reference to the original exchange in case its needed
-            event.setProperty(Exchange.TRACE_EVENT_EXCHANGE, exchange);
-
-            // create event message to sent as in body containing event information such
as
-            // from node, to node, etc.
-            TraceEventMessage msg = new DefaultTraceEventMessage(timestamp, node, exchange);
-
-            // should we use ordinary or jpa objects
-            if (tracer.isUseJpa()) {
-                if (LOG.isTraceEnabled()) {
-                    LOG.trace("Using class: " + this.jpaTraceEventMessageClassName + " for
tracing event messages");
-                }
-
-                // load the jpa event message class
-                loadJpaTraceEventMessageClass(exchange);
-                // create a new instance of the event message class
-                Object jpa = ObjectHelper.newInstance(jpaTraceEventMessageClass);
-
-                // copy options from event to jpa
-                Map<String, Object> options = new HashMap<String, Object>();
-                IntrospectionSupport.getProperties(msg, options, null);
-                IntrospectionSupport.setProperties(jpa, options);
-                // and set the timestamp as its not a String type
-                IntrospectionSupport.setProperty(jpa, "timestamp", msg.getTimestamp());
-
-                event.getIn().setBody(jpa);
-            } else {
-                event.getIn().setBody(msg);
-            }
-
-            // marker property to indicate its a tracing event being routed in case
-            // new Exchange instances is created during trace routing so we can check
-            // for this marker when interceptor also kick in during routing of trace events
-            event.setProperty(Exchange.TRACE_EVENT, Boolean.TRUE);
-            try {
-                // process the trace route
-                getTraceEventProducer(exchange).process(event);
-            } catch (Exception e) {
-                // log and ignore this as the original Exchange should be allowed to continue
-                LOG.error("Error processing trace event (original Exchange will continue):
" + event, e);
-            }
-        }
-    }
-
-    private synchronized void loadJpaTraceEventMessageClass(Exchange exchange) {
-        if (jpaTraceEventMessageClass == null) {
-            jpaTraceEventMessageClass = exchange.getContext().getClassResolver().resolveClass(jpaTraceEventMessageClassName);
-            if (jpaTraceEventMessageClass == null) {
-                throw new IllegalArgumentException("Cannot find class: " + jpaTraceEventMessageClassName
-                        + ". Make sure camel-jpa.jar is in the classpath.");
-            }
         }
     }
 
@@ -372,16 +303,13 @@ public class TraceInterceptor extends De
         if (traceHandler != null) {
             return traceHandler.traceExchangeIn(node, processor, this, exchange);
         } else {
-            traceExchange(exchange);
+            return null;
         }
-        return null;
     }
 
     protected void traceExchangeOut(Exchange exchange, Object traceState) throws Exception
{
         if (traceHandler != null) {
             traceHandler.traceExchangeOut(node, processor, this, exchange, traceState);
-        } else {
-            traceExchange(exchange);
         }
     }
 
@@ -429,27 +357,15 @@ public class TraceInterceptor extends De
         return true;
     }
 
-    private synchronized Producer getTraceEventProducer(Exchange exchange) throws Exception
{
-        if (traceEventProducer == null) {
-            // create producer when we have access the the camel context (we dont in doStart)
-            Endpoint endpoint = tracer.getDestination() != null ? tracer.getDestination()
: exchange.getContext().getEndpoint(tracer.getDestinationUri());
-            traceEventProducer = endpoint.createProducer();
-            ServiceHelper.startService(traceEventProducer);
-        }
-        return traceEventProducer;
-    }
-
     @Override
     protected void doStart() throws Exception {
         super.doStart();
-        traceEventProducer = null;
+        ServiceHelper.startService(traceHandler);
     }
 
     @Override
     protected void doStop() throws Exception {
         super.doStop();
-        if (traceEventProducer != null) {
-            ServiceHelper.stopService(traceEventProducer);
-        }
+        ServiceHelper.stopService(traceHandler);
     }
 }

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=1179153&r1=1179152&r2=1179153&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
Wed Oct  5 10:46:45 2011
@@ -55,6 +55,10 @@ public class Tracer implements Intercept
     private TraceInterceptorFactory traceInterceptorFactory = new DefaultTraceInterceptorFactory();
     private TraceEventHandler traceHandler;
     private String jpaTraceEventMessageClassName = JPA_TRACE_EVENT_MESSAGE;
+    
+    public Tracer() {
+        traceHandler = new DefaultTraceEventHandler(this);
+    }
 
     /**
      * Creates a new tracer.



Mime
View raw message