activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r520939 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/builder/ main/java/org/apache/camel/processor/ test/java/org/apache/camel/ test/java/org/apache/camel/builder/
Date Wed, 21 Mar 2007 16:29:41 GMT
Author: jstrachan
Date: Wed Mar 21 09:29:40 2007
New Revision: 520939

URL: http://svn.apache.org/viewvc?view=rev&rev=520939
Log:
minor refactor so that the BuilderSupport can easily create endpoints and the deadLetterChannel
policy and added some test cases showing how to configure the policy and redelivery policy

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FromBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/LoggingErrorHandlerBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NoErrorHandlerBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RedeliveryPolicy.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerTest.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/RouteBuilderTest.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java?view=diff&rev=520939&r1=520938&r2=520939
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
Wed Mar 21 09:29:40 2007
@@ -18,27 +18,67 @@
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Endpoint;
 import org.apache.camel.processor.LoggingLevel;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import java.util.List;
+import java.util.ArrayList;
+
 /**
  * Base class for implementation inheritance
  *
  * @version $Revision: $
  */
 public abstract class BuilderSupport<E extends Exchange> {
+    private CamelContext context;
     private ErrorHandlerBuilder<E> errorHandlerBuilder;
 
-    protected BuilderSupport() {
+    protected BuilderSupport(CamelContext context) {
+        this.context = context;
     }
 
     protected BuilderSupport(BuilderSupport<E> parent) {
+        this.context = parent.getContext();
         if (parent.errorHandlerBuilder != null) {
             this.errorHandlerBuilder = parent.errorHandlerBuilder.copy();
         }
     }
 
+    // Helper methods
+    //-------------------------------------------------------------------------
+
+    /**
+     * Resolves the given URI to an endpoint
+     */
+    public Endpoint<E> endpoint(String uri) {
+        return getContext().resolveEndpoint(uri);
+    }
+
+    /**
+     * Resolves the list of URIs into a list of {@link Endpoint} instances
+     */
+    public List<Endpoint<E>> endpoints(String... uris) {
+        List<Endpoint<E>> endpoints = new ArrayList<Endpoint<E>>();
+        for (String uri : uris) {
+            endpoints.add(endpoint(uri));
+        }
+        return endpoints;
+    }
+
+    /**
+     * Helper method to create a list of {@link Endpoint} instances
+     */
+    public List<Endpoint<E>> endpoints(Endpoint<E>... endpoints) {
+        List<Endpoint<E>> answer = new ArrayList<Endpoint<E>>();
+        for (Endpoint<E> endpoint : endpoints) {
+            answer.add(endpoint);
+        }
+        return answer;
+    }
+
     // Builder methods
     //-------------------------------------------------------------------------
 
@@ -117,14 +157,27 @@
         return new LoggingErrorHandlerBuilder<E>(log, level);
     }
 
-    /*
     public DeadLetterChannelBuilder<E> deadLetterChannel() {
         return new DeadLetterChannelBuilder<E>();
     }
-    */
+
+    public DeadLetterChannelBuilder<E> deadLetterChannel(String deadLetterUri) {
+        return deadLetterChannel(endpoint(deadLetterUri));
+    }
+
+    public DeadLetterChannelBuilder<E> deadLetterChannel(Endpoint<E> deadLetterEndpoint)
{
+        return new DeadLetterChannelBuilder<E>(deadLetterEndpoint);
+    }
 
     // Properties
     //-------------------------------------------------------------------------
+    public CamelContext getContext() {
+        return context;
+    }
+
+    public void setContext(CamelContext context) {
+        this.context = context;
+    }
 
     public ErrorHandlerBuilder<E> getErrorHandlerBuilder() {
         if (errorHandlerBuilder == null) {
@@ -139,4 +192,5 @@
     public void setErrorHandlerBuilder(ErrorHandlerBuilder<E> errorHandlerBuilder)
{
         this.errorHandlerBuilder = errorHandlerBuilder;
     }
+
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java?view=diff&rev=520939&r1=520938&r2=520939
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java
Wed Mar 21 09:29:40 2007
@@ -18,18 +18,18 @@
 package org.apache.camel.builder;
 
 import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
 import org.apache.camel.Expression;
-import org.apache.camel.processor.RedeliveryPolicy;
+import org.apache.camel.Processor;
 import org.apache.camel.processor.DeadLetterChannel;
 import org.apache.camel.processor.RecipientList;
+import org.apache.camel.processor.RedeliveryPolicy;
 
 /**
  * A builder of a <a href="http://activemq.apache.org/camel/dead-letter-channel.html">Dead
Letter Channel</a>
- * 
+ *
  * @version $Revision$
  */
-public class DeadLetterChannelBuilder<E extends Exchange> extends BuilderSupport<E>
implements ErrorHandlerBuilder<E> {
+public class DeadLetterChannelBuilder<E extends Exchange> implements ErrorHandlerBuilder<E>
{
     private RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
     private ProcessorFactory<E> deadLetterFactory;
     private Processor<E> defaultDeadLetterEndpoint;
@@ -39,6 +39,10 @@
     public DeadLetterChannelBuilder() {
     }
 
+    public DeadLetterChannelBuilder(Processor<E> processor) {
+        this(new ConstantProcessorBuilder<E>(processor));
+    }
+
     public DeadLetterChannelBuilder(ProcessorFactory<E> deadLetterFactory) {
         this.deadLetterFactory = deadLetterFactory;
     }
@@ -54,6 +58,40 @@
         return new DeadLetterChannel<E>(processor, deadLetter, getRedeliveryPolicy());
     }
 
+    // Builder methods
+    //-------------------------------------------------------------------------
+    public DeadLetterChannelBuilder<E> backOffMultiplier(double backOffMultiplier)
{
+        getRedeliveryPolicy().backOffMultiplier(backOffMultiplier);
+        return this;
+    }
+
+    public DeadLetterChannelBuilder<E> collisionAvoidancePercent(short collisionAvoidancePercent)
{
+        getRedeliveryPolicy().collisionAvoidancePercent(collisionAvoidancePercent);
+        return this;
+    }
+
+    public DeadLetterChannelBuilder<E> initialRedeliveryDelay(long initialRedeliveryDelay)
{
+        getRedeliveryPolicy().initialRedeliveryDelay(initialRedeliveryDelay);
+        return this;
+    }
+
+    public DeadLetterChannelBuilder<E> maximumRedeliveries(int maximumRedeliveries)
{
+        getRedeliveryPolicy().maximumRedeliveries(maximumRedeliveries);
+        return this;
+    }
+
+    public DeadLetterChannelBuilder<E> useCollisionAvoidance() {
+        getRedeliveryPolicy().useCollisionAvoidance();
+        return this;
+    }
+
+    public DeadLetterChannelBuilder<E> useExponentialBackOff() {
+        getRedeliveryPolicy().useExponentialBackOff();
+        return this;
+    }
+
+    // Properties
+    //-------------------------------------------------------------------------
     public RedeliveryPolicy getRedeliveryPolicy() {
         return redeliveryPolicy;
     }
@@ -83,19 +121,19 @@
         this.deadLetterFactory = deadLetterFactory;
     }
 
-     public Processor<E> getDefaultDeadLetterEndpoint() {
-         if (defaultDeadLetterEndpoint == null) {
+    public Processor<E> getDefaultDeadLetterEndpoint() {
+        if (defaultDeadLetterEndpoint == null) {
             defaultDeadLetterEndpoint = new RecipientList<E>(getDefaultDeadLetterEndpointExpression());
-         }
-         return defaultDeadLetterEndpoint;
-     }
-
-     /**
-      * Sets the default dead letter endpoint used
-      */
-     public void setDefaultDeadLetterEndpoint(Processor<E> defaultDeadLetterEndpoint)
{
-         this.defaultDeadLetterEndpoint = defaultDeadLetterEndpoint;
-     }
+        }
+        return defaultDeadLetterEndpoint;
+    }
+
+    /**
+     * Sets the default dead letter endpoint used
+     */
+    public void setDefaultDeadLetterEndpoint(Processor<E> defaultDeadLetterEndpoint)
{
+        this.defaultDeadLetterEndpoint = defaultDeadLetterEndpoint;
+    }
 
     public Expression<E> getDefaultDeadLetterEndpointExpression() {
         if (defaultDeadLetterEndpointExpression == null) {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FromBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FromBuilder.java?view=diff&rev=520939&r1=520938&r2=520939
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FromBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FromBuilder.java
Wed Mar 21 09:29:40 2007
@@ -52,29 +52,6 @@
     }
 
     /**
-     * Resolves the given URI to an endpoint
-     */
-    public Endpoint<E> endpoint(String uri) {
-        return getBuilder().endpoint(uri);
-    }
-
-    public List<Endpoint<E>> endpoints(String... uris) {
-        List<Endpoint<E>> endpoints = new ArrayList<Endpoint<E>>();
-        for (String uri : uris) {
-            endpoints.add(endpoint(uri));
-        }
-        return endpoints;
-    }
-
-    public List<Endpoint<E>> endpoints(Endpoint<E>... uris) {
-        List<Endpoint<E>> endpoints = new ArrayList<Endpoint<E>>();
-        for (Endpoint<E> uri : uris) {
-            endpoints.add(uri);
-        }
-        return endpoints;
-    }
-
-    /**
      * Sends the exchange to the given endpoint URI
      */
     public ProcessorFactory<E> to(String uri) {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/LoggingErrorHandlerBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/LoggingErrorHandlerBuilder.java?view=diff&rev=520939&r1=520938&r2=520939
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/LoggingErrorHandlerBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/LoggingErrorHandlerBuilder.java
Wed Mar 21 09:29:40 2007
@@ -30,7 +30,7 @@
  * 
  * @version $Revision$
  */
-public class LoggingErrorHandlerBuilder<E extends Exchange> extends BuilderSupport<E>
implements ErrorHandlerBuilder<E> {
+public class LoggingErrorHandlerBuilder<E extends Exchange> implements ErrorHandlerBuilder<E>
{
     private Log log = LogFactory.getLog(Logger.class);
     private LoggingLevel level = LoggingLevel.INFO;
 

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NoErrorHandlerBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NoErrorHandlerBuilder.java?view=diff&rev=520939&r1=520938&r2=520939
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NoErrorHandlerBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NoErrorHandlerBuilder.java
Wed Mar 21 09:29:40 2007
@@ -29,7 +29,7 @@
  *
  * @version $Revision$
  */
-public class NoErrorHandlerBuilder<E extends Exchange> extends BuilderSupport<E>
implements ErrorHandlerBuilder<E> {
+public class NoErrorHandlerBuilder<E extends Exchange> implements ErrorHandlerBuilder<E>
{
     public ErrorHandlerBuilder<E> copy() {
         return this;
     }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java?view=diff&rev=520939&r1=520938&r2=520939
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
Wed Mar 21 09:29:40 2007
@@ -35,16 +35,16 @@
  * @version $Revision$
  */
 public abstract class RouteBuilder<E extends Exchange> extends BuilderSupport<E>
{
-    private CamelContext<E> context;
     private List<FromBuilder<E>> fromBuilders = new ArrayList<FromBuilder<E>>();
     private AtomicBoolean initalized = new AtomicBoolean(false);
     private Map<Endpoint<E>, Processor<E>> routeMap = new HashMap<Endpoint<E>,
Processor<E>>();
 
     protected RouteBuilder() {
+        this(null);
     }
 
     protected RouteBuilder(CamelContext<E> context) {
-        this.context = context;
+        super(context);
     }
 
     /**
@@ -83,17 +83,15 @@
 
     // Properties
     //-----------------------------------------------------------------------
-
     public CamelContext<E> getContext() {
+        CamelContext<E> context = super.getContext();
         if (context == null) {
             context = createContainer();
+            setContext(context);
         }
         return context;
     }
 
-    public void setContext(CamelContext<E> context) {
-        this.context = context;
-    }
 
     /**
      * Returns the routing map from inbound endpoints to processors

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RedeliveryPolicy.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RedeliveryPolicy.java?view=diff&rev=520939&r1=520938&r2=520939
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RedeliveryPolicy.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RedeliveryPolicy.java
Wed Mar 21 09:29:40 2007
@@ -18,6 +18,7 @@
 package org.apache.camel.processor;
 
 import java.util.Random;
+import java.io.Serializable;
 
 // Code taken from the ActiveMQ codebase
 
@@ -27,7 +28,9 @@
  *
  * @version $Revision$
  */
-public class RedeliveryPolicy {
+public class RedeliveryPolicy implements Cloneable, Serializable {
+    protected static transient Random randomNumberGenerator;
+
     protected int maximumRedeliveries = 6;
     protected long initialRedeliveryDelay = 1000L;
     protected double backOffMultiplier = 2;
@@ -35,7 +38,7 @@
     // +/-15% for a 30% spread -cgs
     protected double collisionAvoidanceFactor = 0.15d;
     protected boolean useCollisionAvoidance = false;
-    protected static Random randomNumberGenerator;
+
 
     public RedeliveryPolicy() {
     }
@@ -186,15 +189,13 @@
         }
 
         if (useCollisionAvoidance) {
-            if (randomNumberGenerator == null) {
-                initRandomNumberGenerator();
-            }
 
             /*
              * First random determines +/-, second random determines how far to
              * go in that direction. -cgs
              */
-            double variance = (randomNumberGenerator.nextBoolean() ? collisionAvoidanceFactor
: -collisionAvoidanceFactor) * randomNumberGenerator.nextDouble();
+            Random random = getRandomNumberGenerator();
+            double variance = (random.nextBoolean() ? collisionAvoidanceFactor : -collisionAvoidanceFactor)
* random.nextDouble();
             redeliveryDelay += redeliveryDelay * variance;
         }
 
@@ -223,9 +224,10 @@
         this.useExponentialBackOff = useExponentialBackOff;
     }
 
-    protected static synchronized void initRandomNumberGenerator() {
+    protected static synchronized Random getRandomNumberGenerator() {
         if (randomNumberGenerator == null) {
             randomNumberGenerator = new Random();
         }
+        return randomNumberGenerator;
     }
 }

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java?view=diff&rev=520939&r1=520938&r2=520939
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java Wed Mar
21 09:29:40 2007
@@ -36,4 +36,8 @@
                 expectedType.isInstance(value));
         return expectedType.cast(value);
     }
+
+    protected void assertEndpointUri(Endpoint<Exchange> endpoint, String uri) {
+        assertEquals("Endoint uri for: " + endpoint, uri, endpoint.getEndpointUri());
+    }
 }

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerTest.java?view=diff&rev=520939&r1=520938&r2=520939
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerTest.java
(original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ErrorHandlerTest.java
Wed Mar 21 09:29:40 2007
@@ -25,6 +25,7 @@
 import org.apache.camel.processor.LoggingErrorHandler;
 import org.apache.camel.processor.SendProcessor;
 import org.apache.camel.processor.DeadLetterChannel;
+import org.apache.camel.processor.RedeliveryPolicy;
 
 import java.util.Map;
 import java.util.Set;
@@ -97,4 +98,62 @@
             }
         }
     }
+
+    public void testConfigureDeadLetterChannel() throws Exception {
+
+        // START SNIPPET: e3
+        RouteBuilder<Exchange> builder1 = new RouteBuilder<Exchange>() {
+            public void configure() {
+                errorHandler(deadLetterChannel("queue:errors"));
+
+                from("queue:a").to("queue:b");
+            }
+        };
+        // END SNIPPET: e3
+        RouteBuilder<Exchange> builder = builder1;
+
+        Map<Endpoint<Exchange>, Processor<Exchange>> routeMap = builder.getRouteMap();
+        Set<Map.Entry<Endpoint<Exchange>, Processor<Exchange>>> routes
= routeMap.entrySet();
+        assertEquals("Number routes created", 1, routes.size());
+        for (Map.Entry<Endpoint<Exchange>, Processor<Exchange>> route :
routes) {
+            Endpoint<Exchange> key = route.getKey();
+            assertEquals("From endpoint", "queue:a", key.getEndpointUri());
+            Processor processor = route.getValue();
+
+            DeadLetterChannel deadLetterChannel = assertIsInstanceOf(DeadLetterChannel.class,
processor);
+            Endpoint deadLetterEndpoint = assertIsInstanceOf(Endpoint.class, deadLetterChannel.getDeadLetter());
+            assertEndpointUri(deadLetterEndpoint, "queue:errors");
+        }
+    }
+
+    public void testConfigureDeadLetterChannelWithCustomRedeliveryPolicy() throws Exception
{
+
+        // START SNIPPET: e4
+        RouteBuilder<Exchange> builder1 = new RouteBuilder<Exchange>() {
+            public void configure() {
+                errorHandler(deadLetterChannel("queue:errors").maximumRedeliveries(2).useExponentialBackOff());
+
+                from("queue:a").to("queue:b");
+            }
+        };
+        // END SNIPPET: e4
+        RouteBuilder<Exchange> builder = builder1;
+
+        Map<Endpoint<Exchange>, Processor<Exchange>> routeMap = builder.getRouteMap();
+        Set<Map.Entry<Endpoint<Exchange>, Processor<Exchange>>> routes
= routeMap.entrySet();
+        assertEquals("Number routes created", 1, routes.size());
+        for (Map.Entry<Endpoint<Exchange>, Processor<Exchange>> route :
routes) {
+            Endpoint<Exchange> key = route.getKey();
+            assertEquals("From endpoint", "queue:a", key.getEndpointUri());
+            Processor processor = route.getValue();
+
+            DeadLetterChannel deadLetterChannel = assertIsInstanceOf(DeadLetterChannel.class,
processor);
+            Endpoint deadLetterEndpoint = assertIsInstanceOf(Endpoint.class, deadLetterChannel.getDeadLetter());
+            assertEndpointUri(deadLetterEndpoint, "queue:errors");
+            RedeliveryPolicy redeliveryPolicy = deadLetterChannel.getRedeliveryPolicy();
+            assertEquals("getMaximumRedeliveries()", 2, redeliveryPolicy.getMaximumRedeliveries());
+            assertEquals("isUseExponentialBackOff()", true, redeliveryPolicy.isUseExponentialBackOff());
+        }
+    }
+
 }

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/RouteBuilderTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/RouteBuilderTest.java?view=diff&rev=520939&r1=520938&r2=520939
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/RouteBuilderTest.java
(original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/RouteBuilderTest.java
Wed Mar 21 09:29:40 2007
@@ -416,8 +416,4 @@
         DeadLetterChannel deadLetter = (DeadLetterChannel) processor;
         return deadLetter.getOutput();
     }
-
-    protected void assertEndpointUri(Endpoint<Exchange> endpoint, String uri) {
-        assertEquals("Endoint uri for: " + endpoint, uri, endpoint.getEndpointUri());
-    }
 }



Mime
View raw message