You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2010/06/25 10:51:24 UTC

svn commit: r957850 - in /camel/trunk/components/camel-quartz/src: main/java/org/apache/camel/component/quartz/QuartzComponent.java test/java/org/apache/camel/component/quartz/QuartzAutoStartTest.java

Author: davsclaus
Date: Fri Jun 25 08:51:23 2010
New Revision: 957850

URL: http://svn.apache.org/viewvc?rev=957850&view=rev
Log:
CAMEL-2856: camel-quartz working better in clustered mode.

Added:
    camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzAutoStartTest.java   (with props)
Modified:
    camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java

Modified: camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java?rev=957850&r1=957849&r2=957850&view=diff
==============================================================================
--- camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java (original)
+++ camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java Fri Jun 25 08:51:23 2010
@@ -57,6 +57,7 @@ public class QuartzComponent extends Def
     private Properties properties;
     private String propertiesFile;
     private int startDelayedSeconds;
+    private boolean autoStartScheduler = true;
 
     public QuartzComponent() {
     }
@@ -124,16 +125,14 @@ public class QuartzComponent extends Def
     }
 
     public void onCamelContextStarted(CamelContext camelContext) throws Exception {
-        // only start scheduler when CamelContext have finished starting
-        if (!scheduler.isStarted()) {
-            if (getStartDelayedSeconds() > 0) {
-                LOG.info("Starting Quartz scheduler: " + scheduler.getSchedulerName() + " delayed: " + getStartDelayedSeconds() + " seconds.");
-                scheduler.startDelayed(getStartDelayedSeconds());
-            } else {
-                LOG.info("Starting Quartz scheduler: " + scheduler.getSchedulerName());
-                scheduler.start();
-            }
+        // if not configure to auto start then don't start it
+        if (!isAutoStartScheduler()) {
+            LOG.info("QuartzComponent configured to not auto start Quartz scheduler.");
+            return;
         }
+
+        // only start scheduler when CamelContext have finished starting
+        startScheduler();
     }
 
     @Override
@@ -146,19 +145,19 @@ public class QuartzComponent extends Def
 
     @Override
     protected void doStop() throws Exception {
+        super.doStop();
+
         if (scheduler != null) {
             int number = JOBS.get();
             if (number > 0) {
                 LOG.info("Cannot shutdown Quartz scheduler: " + scheduler.getSchedulerName() + " as there are still " + number + " jobs registered.");
-            }
-            if (number == 0) {
+            } else {
                 // no more jobs then shutdown the scheduler
                 LOG.info("There are no more jobs registered, so shutting down Quartz scheduler: " + scheduler.getSchedulerName());
                 scheduler.shutdown();
                 scheduler = null;
             }
         }
-        super.doStop();
     }
 
     public void addJob(JobDetail job, Trigger trigger) throws SchedulerException {
@@ -180,15 +179,22 @@ public class QuartzComponent extends Def
     public void removeJob(JobDetail job, Trigger trigger) throws SchedulerException {
         JOBS.decrementAndGet();
 
-        // only un schedule volatile jobs
+        if (isClustered()) {
+            // do not remove jobs which are clustered, as we want the jobs to continue running on the other nodes
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Cannot removing job using trigger: " + trigger.getGroup() + "/" + trigger.getName() + " as the JobStore is clustered.");
+            }
+            return;
+        }
+
+        // only unschedule volatile jobs
         if (job.isVolatile()) {
             if (LOG.isDebugEnabled()) {
                 LOG.debug("Removing job using trigger: " + trigger.getGroup() + "/" + trigger.getName());
             }
             getScheduler().unscheduleJob(trigger.getName(), trigger.getGroup());
         } else {
-            // but pause jobs so they wont trigger in case an application is being stopped or re-started
-            // while this component is still running (eg as it can do in OSGi)
+            // but pause jobs so we can resume them if the application restarts
             if (LOG.isDebugEnabled()) {
                 LOG.debug("Pausing job using trigger: " + trigger.getGroup() + "/" + trigger.getName());
             }
@@ -196,6 +202,43 @@ public class QuartzComponent extends Def
         }
     }
 
+    /**
+     * To force shutdown the quartz scheduler
+     *
+     * @throws SchedulerException can be thrown if error shutting down
+     */
+    public void shutdownScheduler() throws SchedulerException {
+        if (scheduler != null) {
+            LOG.info("Forcing shutdown of Quartz scheduler: " + scheduler.getSchedulerName());
+            scheduler.shutdown();
+            scheduler = null;
+        }
+    }
+
+    /**
+     * Is the quartz scheduler clustered?
+     */
+    public boolean isClustered() throws SchedulerException {
+        return getScheduler().getMetaData().isJobStoreClustered();
+    }
+
+    /**
+     * To force starting the quartz scheduler
+     *
+     * @throws SchedulerException can be thrown if error starting
+     */
+    public void startScheduler() throws SchedulerException {
+        if (!scheduler.isStarted()) {
+            if (getStartDelayedSeconds() > 0) {
+                LOG.info("Starting Quartz scheduler: " + scheduler.getSchedulerName() + " delayed: " + getStartDelayedSeconds() + " seconds.");
+                scheduler.startDelayed(getStartDelayedSeconds());
+            } else {
+                LOG.info("Starting Quartz scheduler: " + scheduler.getSchedulerName());
+                scheduler.start();
+            }
+        }
+    }
+
     // Properties
     // -------------------------------------------------------------------------
 
@@ -245,6 +288,14 @@ public class QuartzComponent extends Def
         this.startDelayedSeconds = startDelayedSeconds;
     }
 
+    public boolean isAutoStartScheduler() {
+        return autoStartScheduler;
+    }
+
+    public void setAutoStartScheduler(boolean autoStartScheduler) {
+        this.autoStartScheduler = autoStartScheduler;
+    }
+
     // Implementation methods
     // -------------------------------------------------------------------------
 

Added: camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzAutoStartTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzAutoStartTest.java?rev=957850&view=auto
==============================================================================
--- camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzAutoStartTest.java (added)
+++ camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzAutoStartTest.java Fri Jun 25 08:51:23 2010
@@ -0,0 +1,64 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.quartz;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * @version $Revision$
+ */
+public class QuartzAutoStartTest extends CamelTestSupport {
+
+    @Test
+    public void testQuartzAutoStart() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:one");
+        mock.expectedMessageCount(0);
+
+        QuartzComponent quartz = context.getComponent("quartz", QuartzComponent.class);
+        assertFalse("Should not have started scheduler", quartz.getScheduler().isStarted());
+
+        Thread.sleep(2000);
+
+        assertMockEndpointsSatisfied();
+
+        mock.reset();
+        mock.expectedMinimumMessageCount(1);
+
+        // start scheduler
+
+        quartz.startScheduler();
+
+        assertMockEndpointsSatisfied();
+    }
+
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                QuartzComponent quartz = context.getComponent("quartz", QuartzComponent.class);
+                quartz.setAutoStartScheduler(false);
+
+                from("quartz://myGroup/myTimerName?cron=0/1+*+*+*+*+?").to("mock:one");
+            }
+        };
+    }
+}

Propchange: camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzAutoStartTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzAutoStartTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date