Author: davsclaus
Date: Thu Mar 11 12:05:34 2010
New Revision: 921816

URL: http://svn.apache.org/viewvc?rev=921816&view=rev
Log:
CAMEL-2539: Added onInit to RoutePolicy to cater for other kind of use cases 
such as a scheduled based policy to start a route in the future.

Added:
    
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CustomScheduledRoutePolicyTest.java
      - copied, changed from r921794, 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CustomRoutePolicyTest.java
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultRouteContext.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RoutePolicySupport.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RoutePolicyProcessor.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RoutePolicy.java

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=921816&r1=921815&r2=921816&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
 Thu Mar 11 12:05:34 2010
@@ -186,6 +186,11 @@ public class DefaultRouteContext impleme
                 routePolicyProcessor.setRoute(edcr);
             }
 
+            // invoke init on route policy
+            if (policy != null) {
+                policy.onInit(edcr);
+            }
+
             routes.add(edcr);
         }
     }

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RoutePolicySupport.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RoutePolicySupport.java?rev=921816&r1=921815&r2=921816&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RoutePolicySupport.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RoutePolicySupport.java
 Thu Mar 11 12:05:34 2010
@@ -35,10 +35,16 @@ public abstract class RoutePolicySupport
     protected final transient Log log = LogFactory.getLog(getClass());
     private ExceptionHandler exceptionHandler;
 
+    public void onInit(Route route) {
+        // noop
+    }
+
     public void onExchangeBegin(Route route, Exchange exchange) {
+        // noop
     }
 
     public void onExchangeDone(Route route, Exchange exchange) {
+        // noop
     }
 
     protected boolean startConsumer(Consumer consumer) throws Exception {
@@ -57,6 +63,14 @@ public abstract class RoutePolicySupport
         return suspended;
     }
 
+    protected void startRoute(Route route) throws Exception {
+        route.getRouteContext().getCamelContext().startRoute(route.getId());
+    }
+
+    protected void stopRoute(Route route) throws Exception {
+        route.getRouteContext().getCamelContext().stopRoute(route.getId());
+    }
+
     /**
      * Handles the given exception using the {...@link #getExceptionHandler()}
      *
@@ -68,10 +82,12 @@ public abstract class RoutePolicySupport
 
     @Override
     protected void doStart() throws Exception {
+        // noop
     }
 
     @Override
     protected void doStop() throws Exception {
+        // noop
     }
 
     public ExceptionHandler getExceptionHandler() {

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RoutePolicyProcessor.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RoutePolicyProcessor.java?rev=921816&r1=921815&r2=921816&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RoutePolicyProcessor.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/RoutePolicyProcessor.java
 Thu Mar 11 12:05:34 2010
@@ -63,6 +63,11 @@ public class RoutePolicyProcessor extend
                     }
                     routePolicy.onExchangeDone(route, exchange);
                 }
+
+                @Override
+                public String toString() {
+                    return "RoutePolicy";
+                }
             });
         }
 

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RoutePolicy.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RoutePolicy.java?rev=921816&r1=921815&r2=921816&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RoutePolicy.java 
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RoutePolicy.java 
Thu Mar 11 12:05:34 2010
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.spi;
 
+import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.Route;
 
@@ -30,7 +31,14 @@ import org.apache.camel.Route;
 public interface RoutePolicy {
 
     /**
-     * Callback invokes when an {...@link Exchange} is started being routed on 
the given {...@link Route}
+     * Callback invoked when the {...@link Route} is being initialized
+     *
+     * @param route     the route being initialized
+     */
+    void onInit(Route route);
+
+    /**
+     * Callback invoked when an {...@link Exchange} is started being routed on 
the given {...@link Route}
      *
      * @param route     the route where the exchange started from
      * @param exchange  the created exchange
@@ -38,7 +46,7 @@ public interface RoutePolicy {
     void onExchangeBegin(Route route, Exchange exchange);
 
     /**
-     * Callback invokes when an {...@link Exchange} is done being routed, 
where it started from the given {...@link Route}
+     * Callback invoked when an {...@link Exchange} is done being routed, 
where it started from the given {...@link Route}
      * <p/>
      * Notice this callback is invoked when the <b>Exchange</b> is done and 
the {...@link Route} is the route where
      * the {...@link Exchange} was started. Most often its also the route 
where the exchange is done. However its

Copied: 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CustomScheduledRoutePolicyTest.java
 (from r921794, 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CustomRoutePolicyTest.java)
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CustomScheduledRoutePolicyTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CustomScheduledRoutePolicyTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CustomRoutePolicyTest.java&r1=921794&r2=921816&rev=921816&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CustomRoutePolicyTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CustomScheduledRoutePolicyTest.java
 Thu Mar 11 12:05:34 2010
@@ -16,10 +16,7 @@
  */
 package org.apache.camel.processor;
 
-import java.util.concurrent.atomic.AtomicBoolean;
-
 import org.apache.camel.ContextTestSupport;
-import org.apache.camel.Exchange;
 import org.apache.camel.Route;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
@@ -28,52 +25,40 @@ import org.apache.camel.impl.RoutePolicy
 /**
  * @version $Revision$
  */
-public class CustomRoutePolicyTest extends ContextTestSupport {
+public class CustomScheduledRoutePolicyTest extends ContextTestSupport {
 
     private final MyCustomRoutePolicy policy = new MyCustomRoutePolicy();
 
     private class MyCustomRoutePolicy extends RoutePolicySupport {
 
-        private volatile AtomicBoolean stopped = new AtomicBoolean();
+        private Route route;
 
         @Override
-        public void onExchangeDone(Route route, Exchange exchange) {
-            String body = exchange.getIn().getBody(String.class);
-            if ("stop".equals(body)) {
-                try {
-                    stopped.set(true);
-                    stopConsumer(route.getConsumer());
-                } catch (Exception e) {
-                    handleException(e);
-                }
-            }
+        public void onInit(Route route) {
+            this.route = route;
         }
 
-        public boolean isStopped() {
-            return stopped.get();
+        public void startRoute() throws Exception {
+            startRoute(route);
         }
+
     }
 
     public void testCustomPolicy() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedBodiesReceived("Hello World");
+        mock.setResultWaitTime(2000);
 
         template.sendBody("seda:foo", "Hello World");
 
-        assertMockEndpointsSatisfied();
-
-        mock.reset();
-        mock.expectedBodiesReceived("stop");
+        // wait 2 sec but the route is not started
+        mock.assertIsNotSatisfied();
 
-        // we send stop command so we should only get 1 message
-        template.sendBody("seda:foo", "stop");
+        // now start it using our policy
+        policy.startRoute();
 
-        assertMockEndpointsSatisfied();
-
-        // give time for slow boxes
-        Thread.sleep(500);
-
-        assertTrue("Should be stopped", policy.isStopped());
+        // now the message should be routed
+        mock.assertIsSatisfied();
     }
 
     @Override
@@ -81,9 +66,8 @@ public class CustomRoutePolicyTest exten
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                
from("seda:foo").routeId("foo").routePolicy(policy).to("mock:result");
+                
from("seda:foo").noAutoStartup().routePolicy(policy).to("mock:result");
             }
         };
     }
-}
-
+}
\ No newline at end of file


Reply via email to