camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r564644 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/builder/ camel-core/src/main/java/org/apache/camel/model/ camel-core/src/main/java/org/apache/camel/processor/ components/camel-spring/
Date Fri, 10 Aug 2007 16:13:21 GMT
Author: jstrachan
Date: Fri Aug 10 09:13:20 2007
New Revision: 564644

URL: http://svn.apache.org/viewvc?view=rev&rev=564644
Log:
added some early refactorings for CAMEL-97

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderSupport.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ExceptionType.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ErrorHandlerSupport.java
    activemq/camel/trunk/components/camel-spring/pom.xml

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilder.java?view=diff&rev=564644&r1=564643&r2=564644
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilder.java
Fri Aug 10 09:13:20 2007
@@ -18,6 +18,7 @@
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
+import org.apache.camel.model.ExceptionType;
 
 import java.util.List;
 
@@ -35,5 +36,5 @@
      */
     Processor createErrorHandler(Processor processor) throws Exception;
 
-    void addErrorHandlers(List<Class> exceptionClasses, Processor errorHandler);
+    void addErrorHandlers(ExceptionType exception);
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderSupport.java?view=diff&rev=564644&r1=564643&r2=564644
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderSupport.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderSupport.java
Fri Aug 10 09:13:20 2007
@@ -17,34 +17,25 @@
  */
 package org.apache.camel.builder;
 
-import org.apache.camel.Processor;
+import org.apache.camel.model.ExceptionType;
 import org.apache.camel.processor.ErrorHandlerSupport;
 
-import java.util.IdentityHashMap;
+import java.util.ArrayList;
 import java.util.List;
-import java.util.Map;
-import java.util.Set;
 
 /**
  * @version $Revision: 1.1 $
  */
 public abstract class ErrorHandlerBuilderSupport implements ErrorHandlerBuilder {
-    private Map<List<Class>, Processor> exceptionHandlers = new IdentityHashMap<List<Class>,
Processor>();
+    private List<ExceptionType> exceptions = new ArrayList<ExceptionType>();
 
-    public void addErrorHandlers(List<Class> exceptionClasses, Processor errorHandler)
{
-        exceptionHandlers.put(exceptionClasses, errorHandler);
+    public void addErrorHandlers(ExceptionType exception) {
+        exceptions.add(exception);
     }
 
     protected void configure(ErrorHandlerSupport handler) {
-        Set<Map.Entry<List<Class>, Processor>> entries = exceptionHandlers.entrySet();
-        for (Map.Entry<List<Class>, Processor> entry : entries) {
-            configure(handler, entry.getKey(), entry.getValue());
-        }
-    }
-
-    protected void configure(ErrorHandlerSupport handler, List<Class> exceptionTypes,
Processor exceptionProcessor) {
-        for (Class exceptionType : exceptionTypes) {
-            handler.addCustomProcessor(exceptionType, exceptionProcessor);
+        for (ExceptionType exception : exceptions) {
+            handler.addExceptionPolicy(exception);
         }
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ExceptionType.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ExceptionType.java?view=diff&rev=564644&r1=564643&r2=564644
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ExceptionType.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ExceptionType.java
Fri Aug 10 09:13:20 2007
@@ -31,12 +31,13 @@
 import org.apache.camel.builder.ErrorHandlerBuilder;
 import org.apache.camel.impl.RouteContext;
 import org.apache.camel.processor.CatchProcessor;
+import org.apache.camel.processor.RedeliveryPolicy;
 import org.apache.camel.util.ObjectHelper;
 
 /**
  * @version $Revision: 1.1 $
  */
-@XmlRootElement(name = "error")
+@XmlRootElement(name = "onException")
 @XmlAccessorType(XmlAccessType.FIELD)
 public class ExceptionType extends ProcessorType {
     @XmlElementRef
@@ -47,6 +48,9 @@
     private List<ProcessorType> outputs = new ArrayList<ProcessorType>();
     @XmlTransient
     private List<Class> exceptionClasses;
+    @XmlTransient
+    private Processor errorHandler;
+    private Integer retry;
 
     public ExceptionType() {
     }
@@ -62,14 +66,24 @@
 
     @Override
     public String toString() {
-        return "Error[ " + getExceptionClasses() + " -> " + getOutputs() + "]";
+        return "Exception[ " + getExceptionClasses() + " -> " + getOutputs() + "]";
+    }
+
+    /**
+     * Allows an exception handler to create a new redelivery policy for this exception type
+     * @param redeliveryPolicy the current redelivery policy
+     * @return a newly created redelivery policy, or return the original policy if no customization
is required
+     * for this exception handler.
+     */
+    public RedeliveryPolicy createRedeliveryPolicy(RedeliveryPolicy redeliveryPolicy) {
+        return redeliveryPolicy;
     }
 
     public void addRoutes(RouteContext routeContext, Collection<Route> routes) throws
Exception {
         // lets attach a processor to an error handler
-        Processor errorHandler = routeContext.createProcessor(this);
+        errorHandler = routeContext.createProcessor(this);
         ErrorHandlerBuilder builder = routeContext.getRoute().getErrorHandlerBuilder();
-        builder.addErrorHandlers(getExceptionClasses(), errorHandler);
+        builder.addErrorHandlers(this);
     }
 
     @Override
@@ -111,6 +125,10 @@
 
     public void setExceptions(List<String> exceptions) {
         this.exceptions = exceptions;
+    }
+
+    public Processor getErrorHandler() {
+        return errorHandler;
     }
 
     protected List<Class> createExceptionClasses() {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java?view=diff&rev=564644&r1=564643&r2=564644
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java
Fri Aug 10 09:13:20 2007
@@ -19,6 +19,7 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.Processor;
+import org.apache.camel.model.ExceptionType;
 import org.apache.camel.impl.ServiceSupport;
 import org.apache.camel.util.ServiceHelper;
 import org.apache.commons.logging.Log;
@@ -67,10 +68,14 @@
         int redeliveryCounter = 0;
         long redeliveryDelay = 0;
 
+        // default behaviour which can be overloaded on a per exception basis
+        RedeliveryPolicy currentRedeliveryPolicy = redeliveryPolicy;
+        Processor failureProcessor = deadLetter;
+
         do {
             if (redeliveryCounter > 0) {
                 // Figure out how long we should wait to resend this message.
-                redeliveryDelay = redeliveryPolicy.getRedeliveryDelay(redeliveryDelay);
+                redeliveryDelay = currentRedeliveryPolicy.getRedeliveryDelay(redeliveryDelay);
                 sleep(redeliveryDelay);
             }
 
@@ -78,16 +83,23 @@
                 output.process(exchange);
                 return;
             } catch (Throwable e) {
-                if (customProcessorForException(exchange, e)) {
-                    return;
-                }
                 logger.log("On delivery attempt: " + redeliveryCounter + " caught: " + e,
e);
+                redeliveryCounter = incrementRedeliveryCounter(exchange, e);
+
+
+                ExceptionType exceptionPolicy = getExceptionPolicy(exchange, e);
+                if (exceptionPolicy != null) {
+                    currentRedeliveryPolicy = exceptionPolicy.createRedeliveryPolicy(currentRedeliveryPolicy);
+                    Processor processor = exceptionPolicy.getErrorHandler();
+                    if (processor != null) {
+                        failureProcessor = processor;
+                    }
+                }
             }
-            redeliveryCounter = incrementRedeliveryCounter(exchange);
-        } while (redeliveryPolicy.shouldRedeliver(redeliveryCounter));
+        } while (currentRedeliveryPolicy.shouldRedeliver(redeliveryCounter));
 
         // now lets send to the dead letter queue
-        deadLetter.process(exchange);
+        failureProcessor.process(exchange);
     }
 
     // Properties
@@ -138,7 +150,7 @@
      * Increments the redelivery counter and adds the redelivered flag if the
      * message has been redelivered
      */
-    protected int incrementRedeliveryCounter(Exchange exchange) {
+    protected int incrementRedeliveryCounter(Exchange exchange, Throwable e) {
         Message in = exchange.getIn();
         Integer counter = in.getHeader(REDELIVERY_COUNTER, Integer.class);
         int next = 1;
@@ -147,6 +159,7 @@
         }
         in.setHeader(REDELIVERY_COUNTER, next);
         in.setHeader(REDELIVERED, true);
+        exchange.setException(e);
         return next;
     }
 

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ErrorHandlerSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ErrorHandlerSupport.java?view=diff&rev=564644&r1=564643&r2=564644
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ErrorHandlerSupport.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ErrorHandlerSupport.java
Fri Aug 10 09:13:20 2007
@@ -20,8 +20,10 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.impl.ServiceSupport;
+import org.apache.camel.model.ExceptionType;
 
 import java.util.IdentityHashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -29,33 +31,39 @@
  * @version $Revision: 1.1 $
  */
 public abstract class ErrorHandlerSupport extends ServiceSupport implements ErrorHandler
{
-    private Map<Class, Processor> exceptionHandlers = new IdentityHashMap<Class,
Processor>();
+    private Map<Class, ExceptionType> exceptionPolicices = new IdentityHashMap<Class,
ExceptionType>();
 
-    public void addCustomProcessor(Class exceptionType, Processor processor) {
+    public void addExceptionPolicy(ExceptionType exception) {
+        Processor processor = exception.getErrorHandler();
         addChildService(processor);
-        exceptionHandlers.put(exceptionType, processor);
 
+        List<Class> list = exception.getExceptionClasses();
+
+        for (Class exceptionType : list) {
+            exceptionPolicices.put(exceptionType, exception);
+        }
     }
 
     /**
      * Attempts to invoke the handler for this particular exception if one is available
+     *
      * @param exchange
      * @param exception
      * @return
      */
     protected boolean customProcessorForException(Exchange exchange, Throwable exception)
throws Exception {
-        Processor processor = getProcessorForException(exchange, exception);
+        ExceptionType policy = getExceptionPolicy(exchange, exception);
+        Processor processor = policy.getErrorHandler();
         if (processor != null) {
-            exchange.setException(exception);
             processor.process(exchange);
             return true;
         }
         return false;
     }
 
-    protected Processor getProcessorForException(Exchange exchange, Throwable exception)
{
-        Set<Map.Entry<Class,Processor>> entries = exceptionHandlers.entrySet();
-        for (Map.Entry<Class, Processor> entry : entries) {
+    protected ExceptionType getExceptionPolicy(Exchange exchange, Throwable exception) {
+        Set<Map.Entry<Class, ExceptionType>> entries = exceptionPolicices.entrySet();
+        for (Map.Entry<Class, ExceptionType> entry : entries) {
             Class type = entry.getKey();
             if (type.isInstance(exception)) {
                 return entry.getValue();

Modified: activemq/camel/trunk/components/camel-spring/pom.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/pom.xml?view=diff&rev=564644&r1=564643&r2=564644
==============================================================================
--- activemq/camel/trunk/components/camel-spring/pom.xml (original)
+++ activemq/camel/trunk/components/camel-spring/pom.xml Fri Aug 10 09:13:20 2007
@@ -45,7 +45,7 @@
     <dependency>
       <groupId>javax.xml</groupId>
       <artifactId>jaxb-api</artifactId>
-      <optional>true</optional>
+      <optional>false</optional>
     </dependency>
 
     <!-- for parsing the XML -->



Mime
View raw message