camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [1/2] camel git commit: CAMEL-7978 Added usingFixedCamelContextName option to QuartzEndpoint and catch the ObjectAlreadyExistsException in cluster mode
Date Fri, 21 Nov 2014 08:52:05 GMT
Repository: camel
Updated Branches:
  refs/heads/master 38cf62984 -> 18f5bf453


CAMEL-7978 Added usingFixedCamelContextName option to QuartzEndpoint and catch the ObjectAlreadyExistsException
in cluster mode


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/01d71b31
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/01d71b31
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/01d71b31

Branch: refs/heads/master
Commit: 01d71b31e583e81584ae441cbe199f426e607149
Parents: 38cf629
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Fri Nov 21 16:44:01 2014 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Fri Nov 21 16:44:01 2014 +0800

----------------------------------------------------------------------
 .../camel/component/quartz2/QuartzEndpoint.java | 34 +++++++++++++++++---
 .../camel/component/quartz2/QuartzHelper.java   | 21 ++++++++++--
 2 files changed, 49 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/01d71b31/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzEndpoint.java
b/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzEndpoint.java
index 607fe25..f3a458e 100644
--- a/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzEndpoint.java
+++ b/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzEndpoint.java
@@ -36,14 +36,15 @@ import org.quartz.CronTrigger;
 import org.quartz.Job;
 import org.quartz.JobBuilder;
 import org.quartz.JobDetail;
+import org.quartz.ObjectAlreadyExistsException;
 import org.quartz.Scheduler;
+import org.quartz.SchedulerException;
 import org.quartz.SimpleTrigger;
 import org.quartz.Trigger;
 import org.quartz.TriggerBuilder;
 import org.quartz.TriggerKey;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-
 import static org.quartz.CronScheduleBuilder.cronSchedule;
 import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
 
@@ -77,6 +78,11 @@ public class QuartzEndpoint extends DefaultEndpoint {
      * ensure endpoint is fully started before the job kicks in. */
     @UriParam
     private long triggerStartDelay = 500; // in millis second
+    /** If it is true, the CamelContext name is used,
+     *  if it is false, use the CamelContext management name which could be changed during
the deploy time 
+     **/
+    @UriParam
+    private boolean usingFixedCamelContextName;
 
     // An internal variables to track whether a job has been in scheduler or not, and has
it paused or not.
     private final AtomicBoolean jobAdded = new AtomicBoolean(false);
@@ -146,6 +152,14 @@ public class QuartzEndpoint extends DefaultEndpoint {
         this.recoverableJob = recoverableJob;
     }
 
+    public boolean isUsingFixedCamelContextName() {
+        return usingFixedCamelContextName;
+    }
+
+    public void setUsingFixedCamelContextName(boolean usingFixedCamelContextName) {
+        this.usingFixedCamelContextName = usingFixedCamelContextName;
+    }
+
     public void setTriggerParameters(Map<String, Object> triggerParameters) {
         this.triggerParameters = triggerParameters;
     }
@@ -246,7 +260,7 @@ public class QuartzEndpoint extends DefaultEndpoint {
         jobDetail = createJobDetail();
         Trigger trigger = createTrigger(jobDetail);
 
-        QuartzHelper.updateJobDataMap(getCamelContext(), jobDetail, getEndpointUri());
+        QuartzHelper.updateJobDataMap(getCamelContext(), jobDetail, getEndpointUri(), isUsingFixedCamelContextName());
 
         if (triggerExisted) {
             // Reschedule job if trigger settings were changed
@@ -254,8 +268,20 @@ public class QuartzEndpoint extends DefaultEndpoint {
                 scheduler.rescheduleJob(triggerKey, trigger);
             }
         } else {
-            // Schedule it now. Remember that scheduler might not be started it, but we can
schedule now.
-            scheduler.scheduleJob(jobDetail, trigger);
+            try {
+                // Schedule it now. Remember that scheduler might not be started it, but
we can schedule now.
+                scheduler.scheduleJob(jobDetail, trigger);
+            } catch (ObjectAlreadyExistsException ex) {
+                // some other VM might may have stored the job & trigger in DB in clustered
mode, in the mean time
+                if (!(getComponent().isClustered())) {  
+                    throw ex;
+                } else { 
+                    trigger = scheduler.getTrigger(triggerKey); 
+                    if (trigger == null) { 
+                        throw new SchedulerException("Trigger could not be found in quartz
scheduler."); 
+                    }
+                } 
+            }
         }
 
         if (LOG.isInfoEnabled()) {

http://git-wip-us.apache.org/repos/asf/camel/blob/01d71b31/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzHelper.java
----------------------------------------------------------------------
diff --git a/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzHelper.java
b/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzHelper.java
index 7263bae..1a78d79 100644
--- a/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzHelper.java
+++ b/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzHelper.java
@@ -38,7 +38,7 @@ public final class QuartzHelper {
             return camelContext.getManagementNameStrategy().getName();
         }
     }
-
+    
     /**
      * Adds the current CamelContext name and endpoint URI to the Job's jobData
      * map.
@@ -48,9 +48,26 @@ public final class QuartzHelper {
      * @param endpointUri URI of the endpoint name, if any. May be {@code null}
      */
     public static void updateJobDataMap(CamelContext camelContext, JobDetail jobDetail, String
endpointUri) {
+        updateJobDataMap(camelContext, jobDetail, endpointUri, false);
+    }
+
+    /**
+     * Adds the current CamelContext name and endpoint URI to the Job's jobData
+     * map.
+     * 
+     * @param camelContext The currently active camelContext
+     * @param jobDetail The job for which the jobData map shall be updated
+     * @param endpointUri URI of the endpoint name, if any. May be {@code null}
+     * @param usingFixedCamelContextName If it is true, jobDataMap uses the CamelContext
name;
+     *  if it is false, jobDataMap uses the CamelContext management name which could be changed
during the deploy time
+     */
+    public static void updateJobDataMap(CamelContext camelContext, JobDetail jobDetail, String
endpointUri, boolean usingFixedCamelContextName) {
         // Store this camelContext name into the job data
         JobDataMap jobDataMap = jobDetail.getJobDataMap();
-        String camelContextName = QuartzHelper.getQuartzContextName(camelContext);
+        String camelContextName = camelContext.getName();
+        if (!usingFixedCamelContextName) {
+            camelContextName = QuartzHelper.getQuartzContextName(camelContext);
+        }
         LOG.debug("Adding camelContextName={}, endpointUri={} into job data map.", camelContextName,
endpointUri);
         jobDataMap.put(QuartzConstants.QUARTZ_CAMEL_CONTEXT_NAME, camelContextName);
         jobDataMap.put(QuartzConstants.QUARTZ_ENDPOINT_URI, endpointUri);


Mime
View raw message