activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gtu...@apache.org
Subject svn commit: r926646 - in /activemq/trunk/activemq-core/src: main/java/org/apache/activemq/broker/scheduler/CronParser.java test/java/org/apache/activemq/broker/scheduler/CronParserTest.java
Date Tue, 23 Mar 2010 16:20:40 GMT
Author: gtully
Date: Tue Mar 23 16:20:40 2010
New Revision: 926646

URL: http://svn.apache.org/viewvc?rev=926646&view=rev
Log:
resolve issue with roll over and cron parser, hudson builds in different TZ has repeated faiure
on trunk, getNext may still need some work

Modified:
    activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/scheduler/CronParser.java
    activemq/trunk/activemq-core/src/test/java/org/apache/activemq/broker/scheduler/CronParserTest.java

Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/scheduler/CronParser.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/scheduler/CronParser.java?rev=926646&r1=926645&r2=926646&view=diff
==============================================================================
--- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/scheduler/CronParser.java
(original)
+++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/scheduler/CronParser.java
Tue Mar 23 16:20:40 2010
@@ -36,49 +36,46 @@ public class CronParser {
         if (cronEntry != null && cronEntry.length() > 0) {
             List<String> list = tokenize(cronEntry);
             List<CronEntry> entries = buildCronEntries(list);
-            Calendar calendar = Calendar.getInstance();
-            calendar.setTimeInMillis(currentTime);
-            int currentMinutes = calendar.get(Calendar.MINUTE);
-            int currentHours = calendar.get(Calendar.HOUR_OF_DAY);
-            int currentDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
-            int currentMonth = calendar.get(Calendar.MONTH) + 1;
-            int currentDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
+            Calendar working = Calendar.getInstance();
+            working.setTimeInMillis(currentTime);
 
             CronEntry minutes = entries.get(MINUTES);
             CronEntry hours = entries.get(HOURS);
             CronEntry dayOfMonth = entries.get(DAY_OF_MONTH);
             CronEntry month = entries.get(MONTH);
             CronEntry dayOfWeek = entries.get(DAY_OF_MONTH);
-            if (!isCurrent(month, currentMonth)) {
-                int nextMonth = getNext(month, currentMonth);
-                Calendar working = (Calendar) calendar.clone();
-                working.add(Calendar.MONTH, nextMonth);
-                result += working.getTimeInMillis();
+            
+            int currentMinutes = working.get(Calendar.MINUTE);
+            if (!isCurrent(minutes, currentMinutes)) {
+                int nextMinutes = getNext(minutes, currentMinutes);
+                working.add(Calendar.MINUTE, nextMinutes);
+                result = working.getTimeInMillis();
             }
-            if (!isCurrent(dayOfMonth, currentDayOfMonth)) {
-                int nextDay = getNext(dayOfMonth, currentMonth);
-                Calendar working = (Calendar) calendar.clone();
-                working.add(Calendar.DAY_OF_MONTH, nextDay);
-                result += working.getTimeInMillis();
+            int currentHours = working.get(Calendar.HOUR_OF_DAY);
+            if (!isCurrent(hours, currentHours)) {
+                int nextHour = getNext(hours, currentHours);
+                working.add(Calendar.HOUR_OF_DAY, nextHour);
+                result = working.getTimeInMillis();
             }
+            int currentDayOfWeek = working.get(Calendar.DAY_OF_WEEK) - 1;
             if (!isCurrent(dayOfWeek, currentDayOfWeek)) {
                 int nextDay = getNext(dayOfWeek, currentDayOfWeek);
-                Calendar working = (Calendar) calendar.clone();
                 working.add(Calendar.DAY_OF_WEEK, nextDay);
-                result += working.getTimeInMillis();
+                result = working.getTimeInMillis();
             }
-            if (!isCurrent(hours, currentHours)) {
-                int nextHour = getNext(hours, currentHours);
-                Calendar working = (Calendar) calendar.clone();
-                working.add(Calendar.HOUR_OF_DAY, nextHour);
-                result += working.getTimeInMillis();
+            int currentMonth = working.get(Calendar.MONTH) + 1;
+            if (!isCurrent(month, currentMonth)) {
+                int nextMonth = getNext(month, currentMonth);
+                working.add(Calendar.MONTH, nextMonth);
+                result = working.getTimeInMillis();
             }
-            if (!isCurrent(minutes, currentMinutes)) {
-                int nextMinutes = getNext(minutes, currentMinutes);
-                Calendar working = (Calendar) calendar.clone();
-                working.add(Calendar.MINUTE, nextMinutes);
-                result += working.getTimeInMillis();
+            int currentDayOfMonth = working.get(Calendar.DAY_OF_MONTH);
+            if (!isCurrent(dayOfMonth, currentDayOfMonth)) {
+                int nextDay = getNext(dayOfMonth, currentMonth);
+                working.add(Calendar.DAY_OF_MONTH, nextDay);
+                result = working.getTimeInMillis();
             }
+            
             if (result == 0) {
                 // this can occur for "* * * * *"
                 result = currentTime + 60 * 1000;
@@ -148,7 +145,7 @@ public class CronParser {
     protected static List<Integer> calculateValues(CronEntry entry) {
         List<Integer> result = new ArrayList<Integer>();
         if (isAll(entry.token)) {
-            for (int i = entry.start; i < entry.end; i++) {
+            for (int i = entry.start; i <= entry.end; i++) {
                 result.add(i);
             }
         } else if (isAStep(entry.token)) {
@@ -215,9 +212,9 @@ public class CronParser {
 
     static List<CronEntry> buildCronEntries(List<String> tokens) {
         List<CronEntry> result = new ArrayList<CronEntry>();
-        CronEntry minutes = new CronEntry("Minutes", tokens.get(MINUTES), 0, 59);
+        CronEntry minutes = new CronEntry("Minutes", tokens.get(MINUTES), 0, 60);
         result.add(minutes);
-        CronEntry hours = new CronEntry("Hours", tokens.get(HOURS), 0, 23);
+        CronEntry hours = new CronEntry("Hours", tokens.get(HOURS), 0, 24);
         result.add(hours);
         CronEntry dayOfMonth = new CronEntry("DayOfMonth", tokens.get(DAY_OF_MONTH), 1, 31);
         result.add(dayOfMonth);

Modified: activemq/trunk/activemq-core/src/test/java/org/apache/activemq/broker/scheduler/CronParserTest.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/broker/scheduler/CronParserTest.java?rev=926646&r1=926645&r2=926646&view=diff
==============================================================================
--- activemq/trunk/activemq-core/src/test/java/org/apache/activemq/broker/scheduler/CronParserTest.java
(original)
+++ activemq/trunk/activemq-core/src/test/java/org/apache/activemq/broker/scheduler/CronParserTest.java
Tue Mar 23 16:20:40 2010
@@ -18,6 +18,8 @@ package org.apache.activemq.broker.sched
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
+
+import java.util.Calendar;
 import java.util.List;
 import javax.jms.MessageFormatException;
 import org.junit.Test;
@@ -28,7 +30,13 @@ public class CronParserTest {
     public void testgetNextTimeMinutes() throws MessageFormatException {
         String test = "30 * * * *";
         long current = 20*60*1000;
+        Calendar calender = Calendar.getInstance();
+        calender.setTimeInMillis(current);
+        System.out.println("start:" + calender.getTime());
         long next = CronParser.getNextScheduledTime(test, current);
+        
+        calender.setTimeInMillis(next);
+        System.out.println("next:" + calender.getTime());
         long result = next - current;
         assertEquals(60*10*1000,result);
     }
@@ -36,13 +44,33 @@ public class CronParserTest {
     @Test
     public void testgetNextTimeHours() throws MessageFormatException {
         String test = "* 1 * * *";
-        long current = 60*1000*60*5;
+        
+        Calendar calender = Calendar.getInstance();
+        calender.set(1972, 2, 2, 17, 10, 0);
+        long current = calender.getTimeInMillis();
         long next = CronParser.getNextScheduledTime(test, current);
+        
+        calender.setTimeInMillis(next);
         long result = next - current;
-        long expected = 60*1000*60*18;
+        long expected = 60*1000*60*8;
         assertEquals(expected,result);
     }
-    
+
+    @Test
+    public void testgetNextTimeHoursZeroMin() throws MessageFormatException {
+        String test = "0 1 * * *";
+        
+        Calendar calender = Calendar.getInstance();
+        calender.set(1972, 2, 2, 17, 10, 0);
+        long current = calender.getTimeInMillis();
+        long next = CronParser.getNextScheduledTime(test, current);
+        
+        calender.setTimeInMillis(next);
+        long result = next - current;
+        long expected = 60*1000*60*7 + 60*1000*50;
+        assertEquals(expected,result);
+    }
+
     @Test
     public void testValidate() {
         try {



Mime
View raw message