camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r950883 - in /camel/trunk/components/camel-quartz: ./ src/main/java/org/apache/camel/component/quartz/ src/test/java/org/apache/camel/component/quartz/ src/test/resources/
Date Thu, 03 Jun 2010 05:53:23 GMT
Author: davsclaus
Date: Thu Jun  3 05:53:22 2010
New Revision: 950883

URL: http://svn.apache.org/viewvc?rev=950883&view=rev
Log:
CAMEL-2784: Fixed camel-quartz to work in osgi and have bundles being started/stopped using
quartz-component without sideeffects on other camel apps using quartz as well.

Added:
    camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzTwoCamelContextTest.java
  (with props)
Removed:
    camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzEndpointTest.java
Modified:
    camel/trunk/components/camel-quartz/pom.xml
    camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzComponent.java
    camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzEndpoint.java
    camel/trunk/components/camel-quartz/src/test/resources/log4j.properties

Modified: camel/trunk/components/camel-quartz/pom.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-quartz/pom.xml?rev=950883&r1=950882&r2=950883&view=diff
==============================================================================
--- camel/trunk/components/camel-quartz/pom.xml (original)
+++ camel/trunk/components/camel-quartz/pom.xml Thu Jun  3 05:53:22 2010
@@ -101,6 +101,7 @@
             <plugin>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <configuration>
+                    <forkMode>pertest</forkMode>
                     <childDelegation>false</childDelegation>
                     <useFile>true</useFile>
                     <excludes>

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=950883&r1=950882&r2=950883&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
Thu Jun  3 05:53:22 2010
@@ -20,6 +20,7 @@ import java.net.URI;
 import java.text.ParseException;
 import java.util.Date;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.impl.DefaultComponent;
@@ -28,6 +29,7 @@ import org.apache.camel.util.ObjectHelpe
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.quartz.CronTrigger;
+import org.quartz.JobDetail;
 import org.quartz.Scheduler;
 import org.quartz.SchedulerException;
 import org.quartz.SchedulerFactory;
@@ -38,15 +40,16 @@ import org.quartz.impl.StdSchedulerFacto
 /**
  * A <a href="http://camel.apache.org/quartz.html">Quartz Component</a>
  * <p/>
- * For a bried tutorial on setting cron expression see
+ * For a brief tutorial on setting cron expression see
  * <a href="http://www.opensymphony.com/quartz/wikidocs/CronTriggers%20Tutorial.html">Quartz
cron tutorial</a>.
  * 
  * @version $Revision:520964 $
  */
 public class QuartzComponent extends DefaultComponent {
     private static final transient Log LOG = LogFactory.getLog(QuartzComponent.class);
+    private static final AtomicInteger JOBS = new AtomicInteger();
+    private static Scheduler scheduler;
     private SchedulerFactory factory;
-    private Scheduler scheduler;
 
     public QuartzComponent() {
     }
@@ -57,7 +60,7 @@ public class QuartzComponent extends Def
 
     @Override
     protected QuartzEndpoint createEndpoint(final String uri, final String remaining, final
Map<String, Object> parameters) throws Exception {
-        QuartzEndpoint answer = new QuartzEndpoint(uri, this, getScheduler());
+        QuartzEndpoint answer = new QuartzEndpoint(uri, this);
 
         // lets split the remaining into a group/name
         URI u = new URI(uri);
@@ -119,23 +122,46 @@ public class QuartzComponent extends Def
         if (scheduler == null) {
             scheduler = getScheduler();
         }
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("Starting Quartz scheduler: " + scheduler.getSchedulerName());
+        if (!scheduler.isStarted()) {
+            LOG.info("Starting Quartz scheduler: " + scheduler.getSchedulerName());
+            scheduler.start();
         }
-        scheduler.start();
     }
 
     @Override
     protected void doStop() throws Exception {
         if (scheduler != null) {
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Shutting down Quartz scheduler: " + scheduler.getSchedulerName());
+            int number = JOBS.get();
+            if (number > 0) {
+                LOG.info("There are still " + number + " jobs registered in the Quartz scheduler:
" + scheduler.getSchedulerName());
+            }
+            if (number == 0) {
+                // 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.shutdown();
         }
         super.doStop();
     }
 
+    public void addJob(JobDetail job, Trigger trigger) throws SchedulerException {
+        JOBS.incrementAndGet();
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Adding job using trigger: " + trigger.getGroup() + "/" + trigger.getName());
+        }
+        getScheduler().scheduleJob(job, trigger);
+    }
+
+    public void removeJob(Trigger trigger) throws SchedulerException {
+        JOBS.decrementAndGet();
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Removing job using trigger: " + trigger.getGroup() + "/" + trigger.getName());
+        }
+        getScheduler().unscheduleJob(trigger.getName(), trigger.getGroup());
+    }
+
     // Properties
     // -------------------------------------------------------------------------
     public SchedulerFactory getFactory() {
@@ -149,7 +175,7 @@ public class QuartzComponent extends Def
         this.factory = factory;
     }
 
-    public Scheduler getScheduler() throws SchedulerException {
+    public synchronized Scheduler getScheduler() throws SchedulerException {
         if (scheduler == null) {
             scheduler = createScheduler();
         }
@@ -157,7 +183,7 @@ public class QuartzComponent extends Def
     }
 
     public void setScheduler(final Scheduler scheduler) {
-        this.scheduler = scheduler;
+        QuartzComponent.scheduler = scheduler;
     }
 
     // Implementation methods

Modified: camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzEndpoint.java?rev=950883&r1=950882&r2=950883&view=diff
==============================================================================
--- camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzEndpoint.java
(original)
+++ camel/trunk/components/camel-quartz/src/main/java/org/apache/camel/component/quartz/QuartzEndpoint.java
Thu Jun  3 05:53:22 2010
@@ -21,17 +21,18 @@ import java.util.Date;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
+import org.apache.camel.Service;
 import org.apache.camel.impl.DefaultEndpoint;
 import org.apache.camel.processor.loadbalancer.LoadBalancer;
 import org.apache.camel.processor.loadbalancer.RoundRobinLoadBalancer;
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.ServiceHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.quartz.JobDetail;
 import org.quartz.JobExecutionContext;
 import org.quartz.JobExecutionException;
-import org.quartz.Scheduler;
 import org.quartz.SchedulerException;
 import org.quartz.Trigger;
 
@@ -40,27 +41,17 @@ import org.quartz.Trigger;
  * 
  * @version $Revision:520964 $
  */
-public class QuartzEndpoint extends DefaultEndpoint {
+public class QuartzEndpoint extends DefaultEndpoint implements Service {
     private static final transient Log LOG = LogFactory.getLog(QuartzEndpoint.class);
 
-    private Scheduler scheduler;
     private LoadBalancer loadBalancer;
     private Trigger trigger;
     private JobDetail jobDetail;
     private boolean started;
     private boolean stateful;
 
-    public QuartzEndpoint() {
-    }
-
-    public QuartzEndpoint(final String endpointUri, final QuartzComponent component, final
Scheduler scheduler) {
+    public QuartzEndpoint(final String endpointUri, final QuartzComponent component) {
         super(endpointUri, component);
-        this.scheduler = scheduler;
-    }
-
-    public QuartzEndpoint(final String endpointUri, final Scheduler scheduler) {
-        super(endpointUri);
-        this.scheduler = scheduler;
     }
 
     public void addTrigger(final Trigger trigger, final JobDetail detail) throws SchedulerException
{
@@ -83,11 +74,11 @@ public class QuartzEndpoint extends Defa
         if (detail.getName() == null) {
             detail.setName(getEndpointUri());
         }
-        getScheduler().scheduleJob(detail, trigger);
+        getComponent().addJob(detail, trigger);
     }
 
-    public void removeTrigger(final Trigger trigger, final JobDetail jobDetail) throws SchedulerException
{
-        getScheduler().unscheduleJob(trigger.getName(), trigger.getGroup());
+    public void removeTrigger(final Trigger trigger) throws SchedulerException {
+        getComponent().removeJob(trigger);
     }
 
     /**
@@ -150,10 +141,6 @@ public class QuartzEndpoint extends Defa
         return true;
     }
 
-    public Scheduler getScheduler() {
-        return scheduler;
-    }
-
     public LoadBalancer getLoadBalancer() {
         if (loadBalancer == null) {
             loadBalancer = createLoadBalancer();
@@ -192,10 +179,6 @@ public class QuartzEndpoint extends Defa
         this.stateful = stateful;
     }
 
-    public void setScheduler(Scheduler scheduler) {
-        this.scheduler = scheduler;
-    }
-
     // Implementation methods
     // -------------------------------------------------------------------------
     public synchronized void consumerStarted(final QuartzConsumer consumer) throws SchedulerException
{
@@ -213,7 +196,7 @@ public class QuartzEndpoint extends Defa
         ObjectHelper.notNull(trigger, "trigger");
         getLoadBalancer().removeProcessor(consumer.getProcessor());
         if (getLoadBalancer().getProcessors().isEmpty() && started) {
-            removeTrigger(getTrigger(), getJobDetail());
+            removeTrigger(getTrigger());
             started = false;
         }
     }
@@ -226,4 +209,13 @@ public class QuartzEndpoint extends Defa
         return new JobDetail();
     }
 
+    public void start() throws Exception {
+        ObjectHelper.notNull(getComponent(), "QuartzComponent", this);
+        ServiceHelper.startService(loadBalancer);
+    }
+
+    public void stop() throws Exception {
+        ServiceHelper.stopService(loadBalancer);
+    }
+
 }

Added: camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzTwoCamelContextTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzTwoCamelContextTest.java?rev=950883&view=auto
==============================================================================
--- camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzTwoCamelContextTest.java
(added)
+++ camel/trunk/components/camel-quartz/src/test/java/org/apache/camel/component/quartz/QuartzTwoCamelContextTest.java
Thu Jun  3 05:53:22 2010
@@ -0,0 +1,77 @@
+/**
+ * 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 junit.framework.TestCase;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+
+/**
+ * @version $Revision$
+ */
+public class QuartzTwoCamelContextTest extends TestCase {
+
+    private DefaultCamelContext camel1;
+    private DefaultCamelContext camel2;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        camel1 = new DefaultCamelContext();
+        camel1.setName("camel-1");
+        camel1.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("quartz://myGroup/myTimerName?cron=0/1+*+*+*+*+?").to("mock:one");
+            }
+        });
+        camel1.start();
+
+        camel2 = new DefaultCamelContext();
+        camel2.setName("camel-2");
+        camel2.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("quartz://myOtherGroup/myOtherTimerName?cron=0/1+*+*+*+*+?").to("mock:two");
+            }
+        });
+        camel2.start();
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        camel1.stop();
+        camel2.stop();
+        super.tearDown();
+    }
+
+    public void testTwoCamelContext() throws Exception {
+        MockEndpoint mock1 = camel1.getEndpoint("mock:one", MockEndpoint.class);
+        mock1.expectedMinimumMessageCount(2);
+
+        MockEndpoint mock2 = camel2.getEndpoint("mock:two", MockEndpoint.class);
+        mock2.expectedMinimumMessageCount(6);
+
+        camel1.stop();
+
+        mock2.assertIsSatisfied();
+
+        camel2.stop();
+    }
+
+}

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

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

Modified: camel/trunk/components/camel-quartz/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-quartz/src/test/resources/log4j.properties?rev=950883&r1=950882&r2=950883&view=diff
==============================================================================
--- camel/trunk/components/camel-quartz/src/test/resources/log4j.properties (original)
+++ camel/trunk/components/camel-quartz/src/test/resources/log4j.properties Thu Jun  3 05:53:22
2010
@@ -22,6 +22,7 @@ log4j.rootLogger=INFO, file
 
 # uncomment the following to enable camel debugging
 #log4j.logger.org.apache.camel=DEBUG
+#log4j.logger.org.apache.camel.component.quartz=DEBUG
 
 # CONSOLE appender not used by default
 log4j.appender.out=org.apache.log4j.ConsoleAppender



Mime
View raw message