Author: ningjiang Date: Tue Sep 4 00:47:14 2012 New Revision: 1380412 URL: http://svn.apache.org/viewvc?rev=1380412&view=rev Log: Merged revisions 1380237 via svnmerge from https://svn.apache.org/repos/asf/camel/branches/camel-2.10.x
........ r1380237 | ningjiang | 2012-09-03 22:08:51 +0800 (Mon, 03 Sep 2012) | 4 lines Merged revisions 1380232 via svnmerge from https://svn.apache.org/repos/asf/camel/trunk ........ Modified: camel/branches/camel-2.9.x/ (props changed) camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/util/ServiceHelper.java camel/branches/camel-2.9.x/components/camel-quartz/src/main/java/org/apache/camel/routepolicy/quartz/ScheduledRoutePolicy.java camel/branches/camel-2.9.x/components/camel-quartz/src/test/java/org/apache/camel/routepolicy/quartz/SimpleScheduledRoutePolicyTest.java Propchange: camel/branches/camel-2.9.x/ ------------------------------------------------------------------------------ Merged /camel/trunk:r1380232 Merged /camel/branches/camel-2.10.x:r1380237 Propchange: camel/branches/camel-2.9.x/ ------------------------------------------------------------------------------ Binary property 'svnmerge-integrated' - no diff available. Modified: camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/util/ServiceHelper.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/util/ServiceHelper.java?rev=1380412&r1=1380411&r2=1380412&view=diff ============================================================================== --- camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/util/ServiceHelper.java (original) +++ camel/branches/camel-2.9.x/camel-core/src/main/java/org/apache/camel/util/ServiceHelper.java Tue Sep 4 00:47:14 2012 @@ -340,5 +340,20 @@ 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; + } } Modified: camel/branches/camel-2.9.x/components/camel-quartz/src/main/java/org/apache/camel/routepolicy/quartz/ScheduledRoutePolicy.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-quartz/src/main/java/org/apache/camel/routepolicy/quartz/ScheduledRoutePolicy.java?rev=1380412&r1=1380411&r2=1380412&view=diff ============================================================================== --- camel/branches/camel-2.9.x/components/camel-quartz/src/main/java/org/apache/camel/routepolicy/quartz/ScheduledRoutePolicy.java (original) +++ camel/branches/camel-2.9.x/components/camel-quartz/src/main/java/org/apache/camel/routepolicy/quartz/ScheduledRoutePolicy.java Tue Sep 4 00:47:14 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) { @@ -61,7 +63,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/branches/camel-2.9.x/components/camel-quartz/src/test/java/org/apache/camel/routepolicy/quartz/SimpleScheduledRoutePolicyTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-quartz/src/test/java/org/apache/camel/routepolicy/quartz/SimpleScheduledRoutePolicyTest.java?rev=1380412&r1=1380411&r2=1380412&view=diff ============================================================================== --- camel/branches/camel-2.9.x/components/camel-quartz/src/test/java/org/apache/camel/routepolicy/quartz/SimpleScheduledRoutePolicyTest.java (original) +++ camel/branches/camel-2.9.x/components/camel-quartz/src/test/java/org/apache/camel/routepolicy/quartz/SimpleScheduledRoutePolicyTest.java Tue Sep 4 00:47:14 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(); + } }