CAMEL-9633 quartz2 - Add support for specifying custom calendar

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

Branch: refs/heads/master
Commit: 3533c011db3305f4effb580510c15638f846bcbc
Parents: dc9144e
Author: Andrea Cosentino <anco...@gmail.com>
Authored: Wed Feb 24 13:31:22 2016 +0100
Committer: Andrea Cosentino <anco...@gmail.com>
Committed: Wed Feb 24 13:45:40 2016 +0100

----------------------------------------------------------------------
 .../component/quartz2/QuartzConstants.java      |  2 +
 .../camel/component/quartz2/QuartzEndpoint.java | 56 +++++++++++++--
 .../quartz2/QuartzCustomCalendarFireTest.java   | 74 ++++++++++++++++++++
 .../quartz2/QuartzCustomCalendarNoFireTest.java | 71 +++++++++++++++++++
 4 files changed, 198 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/3533c011/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzConstants.java
----------------------------------------------------------------------
diff --git 
a/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzConstants.java
 
b/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzConstants.java
index 3386a71..7b51b08 100644
--- 
a/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzConstants.java
+++ 
b/components/camel-quartz2/src/main/java/org/apache/camel/component/quartz2/QuartzConstants.java
@@ -34,6 +34,8 @@ public final class QuartzConstants {
     public static final String QUARTZ_TRIGGER_CRON_TIMEZONE = 
"CamelQuartzTriggerCronTimeZone";
     public static final String QUARTZ_TRIGGER_SIMPLE_REPEAT_COUNTER = 
"CamelQuartzTriggerSimpleRepeatCounter";
     public static final String QUARTZ_TRIGGER_SIMPLE_REPEAT_INTERVAL = 
"CamelQuartzTriggerSimpleRepeatInterval";
+    
+    public static final String QUARTZ_CAMEL_CUSTOM_CALENDAR = 
"CamelQuartzCustomCalendar";
 
     private QuartzConstants() {
         // Utility class

http://git-wip-us.apache.org/repos/asf/camel/blob/3533c011/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 6bd8fb2..7a6cd38 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
@@ -34,6 +34,8 @@ import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriPath;
 import org.apache.camel.util.EndpointHelper;
+import org.apache.camel.util.ObjectHelper;
+import org.quartz.Calendar;
 import org.quartz.CronTrigger;
 import org.quartz.Job;
 import org.quartz.JobBuilder;
@@ -94,6 +96,8 @@ public class QuartzEndpoint extends DefaultEndpoint {
     private Map<String, Object> triggerParameters;
     @UriParam(prefix = "job.", multiValue = true)
     private Map<String, Object> jobParameters;
+    @UriParam
+    private Calendar customCalendar;
 
     public QuartzEndpoint(String uri, QuartzComponent quartzComponent) {
         super(uri, quartzComponent);
@@ -296,8 +300,19 @@ public class QuartzEndpoint extends DefaultEndpoint {
     public void setTriggerKey(TriggerKey triggerKey) {
         this.triggerKey = triggerKey;
     }
+    
+       public Calendar getCustomCalendar() {
+               return customCalendar;
+       }
 
-    @Override
+    /**
+     * Specifies a custom calendar to avoid specific range of date
+     */
+       public void setCustomCalendar(Calendar customCalendar) {
+               this.customCalendar = customCalendar;
+       }
+
+       @Override
     public Producer createProducer() throws Exception {
         throw new UnsupportedOperationException("Quartz producer is not 
supported.");
     }
@@ -319,7 +334,9 @@ public class QuartzEndpoint extends DefaultEndpoint {
         if (isDeleteJob() && isPauseJob()) {
             throw new IllegalArgumentException("Cannot have both options 
deleteJob and pauseJob enabled");
         }
-
+        if (ObjectHelper.isNotEmpty(customCalendar)) {
+            
getComponent().getScheduler().addCalendar(QuartzConstants.QUARTZ_CAMEL_CUSTOM_CALENDAR,
 customCalendar, true, false);
+        }
         addJobInScheduler();
     }
 
@@ -442,21 +459,42 @@ public class QuartzEndpoint extends DefaultEndpoint {
             LOG.debug("Creating CronTrigger: {}", cron);
             String timeZone = (String)triggerParameters.get("timeZone");
             if (timeZone != null) {
+               if (ObjectHelper.isNotEmpty(customCalendar)) {
                 result = TriggerBuilder.newTrigger()
                         .withIdentity(triggerKey)
                         .startAt(startTime)
                         .withSchedule(cronSchedule(cron)
                         .withMisfireHandlingInstructionFireAndProceed()
                         .inTimeZone(TimeZone.getTimeZone(timeZone)))
+                        
.modifiedByCalendar(QuartzConstants.QUARTZ_CAMEL_CUSTOM_CALENDAR)
                         .build();
+               } else {
+                    result = TriggerBuilder.newTrigger()
+                            .withIdentity(triggerKey)
+                            .startAt(startTime)
+                            .withSchedule(cronSchedule(cron)
+                            .withMisfireHandlingInstructionFireAndProceed()
+                            .inTimeZone(TimeZone.getTimeZone(timeZone)))
+                            .build();
+               }
                 
jobDetail.getJobDataMap().put(QuartzConstants.QUARTZ_TRIGGER_CRON_TIMEZONE, 
timeZone);
             } else {
+               if (ObjectHelper.isNotEmpty(customCalendar)) {
                 result = TriggerBuilder.newTrigger()
                         .withIdentity(triggerKey)
                         .startAt(startTime)
                         .withSchedule(cronSchedule(cron)
                         .withMisfireHandlingInstructionFireAndProceed())
+                        
.modifiedByCalendar(QuartzConstants.QUARTZ_CAMEL_CUSTOM_CALENDAR)
                         .build();
+               } else {
+                    result = TriggerBuilder.newTrigger()
+                            .withIdentity(triggerKey)
+                            .startAt(startTime)
+                            .withSchedule(cronSchedule(cron)
+                            .withMisfireHandlingInstructionFireAndProceed())
+                            .build();
+               }
             }
 
             // enrich job map with details
@@ -480,12 +518,20 @@ public class QuartzEndpoint extends DefaultEndpoint {
                 // need to update the parameters
                 triggerParameters.put("repeatInterval", interval);
             }
-
-            TriggerBuilder<SimpleTrigger> triggerBuilder = 
TriggerBuilder.newTrigger()
+            TriggerBuilder<SimpleTrigger> triggerBuilder;
+               if (ObjectHelper.isNotEmpty(customCalendar)) {
+            triggerBuilder = TriggerBuilder.newTrigger()
                     .withIdentity(triggerKey)
                     .startAt(startTime)
                     
.withSchedule(simpleSchedule().withMisfireHandlingInstructionFireNow()
-                            
.withRepeatCount(repeat).withIntervalInMilliseconds(interval));
+                            
.withRepeatCount(repeat).withIntervalInMilliseconds(interval)).modifiedByCalendar(QuartzConstants.QUARTZ_CAMEL_CUSTOM_CALENDAR);
+               } else {
+                triggerBuilder = TriggerBuilder.newTrigger()
+                        .withIdentity(triggerKey)
+                        .startAt(startTime)
+                        
.withSchedule(simpleSchedule().withMisfireHandlingInstructionFireNow()
+                                
.withRepeatCount(repeat).withIntervalInMilliseconds(interval));
+               }
 
             if (fireNow) {
                 triggerBuilder = triggerBuilder.startNow();

http://git-wip-us.apache.org/repos/asf/camel/blob/3533c011/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzCustomCalendarFireTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzCustomCalendarFireTest.java
 
b/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzCustomCalendarFireTest.java
new file mode 100644
index 0000000..e78d49e
--- /dev/null
+++ 
b/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzCustomCalendarFireTest.java
@@ -0,0 +1,74 @@
+/**
+ * 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.quartz2;
+
+import java.util.Date;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+import org.junit.Test;
+import org.quartz.Calendar;
+import org.quartz.Scheduler;
+import org.quartz.impl.calendar.HolidayCalendar;
+
+/**
+ * This test a timer endpoint in a route with Custom calendar.
+ */
+public class QuartzCustomCalendarFireTest extends BaseQuartzTest {
+
+    @Test
+    public void testQuartzCustomCronRouteNoFire() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMinimumMessageCount(0);
+
+        QuartzComponent component = context.getComponent("quartz2", 
QuartzComponent.class);
+        Scheduler scheduler = component.getScheduler();
+        
+        Calendar c = 
scheduler.getCalendar(QuartzConstants.QUARTZ_CAMEL_CUSTOM_CALENDAR);
+        Date now = new Date();
+        java.util.Calendar tomorrow = java.util.Calendar.getInstance();
+        tomorrow.setTime(now);
+        tomorrow.add(java.util.Calendar.DAY_OF_MONTH, 1);
+        assertEquals(false, c.isTimeIncluded(tomorrow.getTimeInMillis()));
+        assertEquals(true, c.isTimeIncluded(now.getTime()));
+        assertMockEndpointsSatisfied();
+    }
+    
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+
+        HolidayCalendar cal = new HolidayCalendar();
+        java.util.Calendar tomorrow = java.util.Calendar.getInstance();
+        tomorrow.setTime(new Date());
+        tomorrow.add(java.util.Calendar.DAY_OF_MONTH, 1);
+        cal.addExcludedDate(tomorrow.getTime());
+
+        jndi.bind("calendar", cal);
+        return jndi;
+    }
+    
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                
from("quartz2://MyTimer?customCalendar=#calendar&cron=05+00+00+*+*+?").to("mock:result");
+            }
+        };
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/3533c011/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzCustomCalendarNoFireTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzCustomCalendarNoFireTest.java
 
b/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzCustomCalendarNoFireTest.java
new file mode 100644
index 0000000..fb50e41
--- /dev/null
+++ 
b/components/camel-quartz2/src/test/java/org/apache/camel/component/quartz2/QuartzCustomCalendarNoFireTest.java
@@ -0,0 +1,71 @@
+/**
+ * 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.quartz2;
+
+import java.util.Date;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+import org.junit.Test;
+import org.quartz.Calendar;
+import org.quartz.Scheduler;
+import org.quartz.impl.calendar.HolidayCalendar;
+
+/**
+ * This test a timer endpoint in a route with Custom calendar.
+ */
+public class QuartzCustomCalendarNoFireTest extends BaseQuartzTest {
+
+    @Test
+    public void testQuartzCustomCronRouteNoFire() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMinimumMessageCount(0);
+
+        QuartzComponent component = context.getComponent("quartz2", 
QuartzComponent.class);
+        Scheduler scheduler = component.getScheduler();
+        
+        Calendar c = 
scheduler.getCalendar(QuartzConstants.QUARTZ_CAMEL_CUSTOM_CALENDAR);
+        Date now = new Date();
+        java.util.Calendar tomorrow = java.util.Calendar.getInstance();
+        tomorrow.setTime(now);
+        tomorrow.add(java.util.Calendar.DAY_OF_MONTH, 1);
+        assertEquals(true, c.isTimeIncluded(tomorrow.getTimeInMillis()));
+        assertEquals(false, c.isTimeIncluded(now.getTime()));
+        assertMockEndpointsSatisfied();
+    }
+    
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+
+        HolidayCalendar cal = new HolidayCalendar();
+        cal.addExcludedDate(new Date());
+
+        jndi.bind("calendar", cal);
+        return jndi;
+    }
+    
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                
from("quartz2://MyTimer?customCalendar=#calendar&cron=05+00+00+*+*+?").to("mock:result");
+            }
+        };
+    }
+}
\ No newline at end of file

Reply via email to