You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2012/09/03 15:41:20 UTC

svn commit: r1380232 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/util/ components/camel-quartz/src/main/java/org/apache/camel/routepolicy/quartz/ components/camel-quartz/src/test/java/org/apache/camel/routepolicy/quartz/

Author: ningjiang
Date: Mon Sep  3 13:41:19 2012
New Revision: 1380232

URL: http://svn.apache.org/viewvc?rev=1380232&view=rev
Log:
CAMEL-5555 fix the resume issue of ScheduledRoutePolicy

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ServiceHelper.java
    camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/routepolicy/quartz/ScheduledRoutePolicy.java
    camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/routepolicy/quartz/SimpleScheduledRoutePolicyTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ServiceHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ServiceHelper.java?rev=1380232&r1=1380231&r2=1380232&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ServiceHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ServiceHelper.java Mon Sep  3 13:41:19 2012
@@ -343,6 +343,21 @@ public final class ServiceHelper {
         }
         return false;
     }
+    
+    /**
+     * Is the given service suspended
+     *
+     * @return <tt>true</tt> if already suspended, otherwise <tt>false</tt>
+     */
+    public static boolean isSuspended(Object value) {
+        if (value instanceof StatefulService) {
+            StatefulService service = (StatefulService) value;
+            if (service.isSuspended() || service.isSuspending()) {
+                return true;
+            }
+        }
+        return false;
+    }
 
     /**
      * Gather all child services by navigating the service to recursively gather all child services.

Modified: camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/routepolicy/quartz/ScheduledRoutePolicy.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/routepolicy/quartz/ScheduledRoutePolicy.java?rev=1380232&r1=1380231&r2=1380232&view=diff
==============================================================================
--- camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/routepolicy/quartz/ScheduledRoutePolicy.java (original)
+++ camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/routepolicy/quartz/ScheduledRoutePolicy.java Mon Sep  3 13:41:19 2012
@@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit;
 import org.apache.camel.Route;
 import org.apache.camel.ServiceStatus;
 import org.apache.camel.impl.RoutePolicySupport;
+import org.apache.camel.util.ServiceHelper;
 import org.quartz.JobDetail;
 import org.quartz.Scheduler;
 import org.quartz.SchedulerException;
@@ -46,7 +47,8 @@ public abstract class ScheduledRoutePoli
         if (action == Action.START) {
             if (routeStatus == ServiceStatus.Stopped) {
                 startRoute(route);
-            } else if (routeStatus == ServiceStatus.Suspended) {
+                // here we just check the states of the Consumer
+            } else if (ServiceHelper.isSuspended(route.getConsumer())) {
                 startConsumer(route.getConsumer());
             }
         } else if (action == Action.STOP) {
@@ -63,7 +65,11 @@ public abstract class ScheduledRoutePoli
             }
         } else if (action == Action.RESUME) {
             if (routeStatus == ServiceStatus.Started) {
-                startConsumer(route.getConsumer());
+                if (ServiceHelper.isSuspended(route.getConsumer())) {
+                    startConsumer(route.getConsumer());
+                } else {
+                    LOG.warn("The Consumer {} is not suspended and cannot be resumed.", route.getConsumer());
+                }
             } else {
                 LOG.warn("Route is not in a started state and cannot be resumed. The current route state is {}", routeStatus);
             }

Modified: camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/routepolicy/quartz/SimpleScheduledRoutePolicyTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/routepolicy/quartz/SimpleScheduledRoutePolicyTest.java?rev=1380232&r1=1380231&r2=1380232&view=diff
==============================================================================
--- camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/routepolicy/quartz/SimpleScheduledRoutePolicyTest.java (original)
+++ camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/routepolicy/quartz/SimpleScheduledRoutePolicyTest.java Mon Sep  3 13:41:19 2012
@@ -171,6 +171,84 @@ public class SimpleScheduledRoutePolicyT
         
         context.getComponent("quartz", QuartzComponent.class).stop();
         success.assertIsSatisfied();
-    } 
+    }
+    
+    @Test
+    public void testScheduledSuspendAndResumeRoutePolicy() throws Exception {
+        MockEndpoint success = context.getEndpoint("mock:success", MockEndpoint.class);
+        success.expectedMessageCount(1);
+        
+        context.getComponent("quartz", QuartzComponent.class).setPropertiesFile("org/apache/camel/routepolicy/quartz/myquartz.properties");
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                SimpleScheduledRoutePolicy policy = new SimpleScheduledRoutePolicy();
+                long suspendTime = System.currentTimeMillis() + 1000L;
+                policy.setRouteSuspendDate(new Date(suspendTime));
+                policy.setRouteSuspendRepeatCount(0);
+                long resumeTime = System.currentTimeMillis() + 4000L;
+                policy.setRouteResumeDate(new Date(resumeTime));
+                policy.setRouteResumeRepeatCount(1);
+                policy.setRouteResumeRepeatInterval(3000);
+                
+                from("direct:start")
+                    .routeId("test")
+                    .routePolicy(policy)
+                    .to("mock:success");
+            } 
+        });
+        context.start();
+        Thread.sleep(1000);
+        
+        try {
+            template.sendBody("direct:start", "Ready or not, Here, I come");
+        } catch (CamelExecutionException e) {
+            LOG.debug("Consumer successfully suspended");
+        } 
+        
+        Thread.sleep(4000);
+        template.sendBody("direct:start", "Ready or not, Here, I come");
+        
+        context.getComponent("quartz", QuartzComponent.class).stop();
+        success.assertIsSatisfied();
+    }
+    
+    @Test
+    public void testScheduledSuspendAndRestartPolicy() throws Exception {
+        MockEndpoint success = context.getEndpoint("mock:success", MockEndpoint.class);
+        success.expectedMessageCount(1);
+        
+        context.getComponent("quartz", QuartzComponent.class).setPropertiesFile("org/apache/camel/routepolicy/quartz/myquartz.properties");
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                SimpleScheduledRoutePolicy policy = new SimpleScheduledRoutePolicy();
+                long suspendTime = System.currentTimeMillis() + 1000L;
+                policy.setRouteSuspendDate(new Date(suspendTime));
+                policy.setRouteSuspendRepeatCount(0);
+                long startTime = System.currentTimeMillis() + 4000L;
+                policy.setRouteStartDate(new Date(startTime));
+                policy.setRouteResumeRepeatCount(1);
+                policy.setRouteResumeRepeatInterval(3000);
+                
+                from("direct:start")
+                    .routeId("test")
+                    .routePolicy(policy)
+                    .to("mock:success");
+            } 
+        });
+        context.start();
+        Thread.sleep(1000);
+        
+        try {
+            template.sendBody("direct:start", "Ready or not, Here, I come");
+        } catch (CamelExecutionException e) {
+            LOG.debug("Consumer successfully suspended");
+        } 
+        
+        Thread.sleep(4000);
+        template.sendBody("direct:start", "Ready or not, Here, I come");
+        
+        context.getComponent("quartz", QuartzComponent.class).stop();
+        success.assertIsSatisfied();
+    }
     
 }