Announcement Announcement Module
Collapse
No announcement yet.
The cron expression "0 0 8-10 1,2,3 9 ?" makes unexpected schedules in Spring 3.0 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • The cron expression "0 0 8-10 1,2,3 9 ?" makes unexpected schedules in Spring 3.0

    Hello,

    I would like to test the expression of "0 0 8-10 1,2,3 9 ?", and I
    expected that the next schedule of "2010/09/01 09:00:00" should be "2010/09/01 10:00:00", but I got "2010/10/01 08:00:00".


    I tested "org.springframework.scheduling.support.CronTrigge r" as following test code.

    Code:
    import static org.hamcrest.CoreMatchers.*;
    import static org.junit.Assert.*;
    import java.text.ParseException;
    import java.util.Arrays;
    import java.util.Date;
    import java.util.List;
    
    import org.apache.commons.lang.time.DateFormatUtils;
    import org.apache.commons.lang.time.DateUtils;
    import org.junit.Test;
    import org.springframework.scheduling.support.CronTrigger;
    import org.springframework.scheduling.support.SimpleTriggerContext;
    
    public class SpringCronExpressionTest {
    	
    	private static final String DATE_PATTERN = "yyyy/MM/dd hh:mm:ss";
    
    	@Test
    	public void everyMorningSchedule() throws ParseException {
    		String cronExpression = "0 0 8-10 1,2,3 9 ?";
    		String intialTime = "2010/09/01 09:00:00";
    		List<String> expectedTimeList = Arrays.asList(
    				"2010/09/01 10:00:00",
    				"2010/09/02 08:00:00",
    				"2010/09/02 09:00:00",
    				"2010/09/02 10:00:00",
    				"2010/09/03 08:00:00",
    				"2010/09/03 09:00:00", 
    				"2010/09/03 10:00:00");
    
    		assertSchedule(cronExpression, intialTime, expectedTimeList);
    	}
    
    	private void assertSchedule(String cronExpression, String intialTime,
    		List<String> expectedTimeList) throws ParseException {
    		CronTrigger trigger = new CronTrigger(cronExpression);
    		Date startTime = DateUtils.parseDate(intialTime,	new String[]{DATE_PATTERN});
    		SimpleTriggerContext context = new SimpleTriggerContext();
    		context.update(startTime, startTime, startTime);
    		
    		for(String exptectedTime : expectedTimeList){
    			Date nextExecutionTime = trigger.nextExecutionTime(context);
    			String actualTime = DateFormatUtils.format(nextExecutionTime, DATE_PATTERN);
    			System.out.printf("expected %s, actual %s \r\n",exptectedTime, actualTime);
    			assertThat("executed on expected time", actualTime, is(exptectedTime));
    			context.update(nextExecutionTime, nextExecutionTime, nextExecutionTime);
    		}
    	}
    }
    Code:
    java.lang.AssertionError: executed on expected time
    Expected: is "2010/09/01 10:00:00"
         got: "2010/10/01 08:00:00"
    I did a same test by the CronTrigger class of Quartz, then I got expected result.
    Code:
    package edu.batch;
    
    import static org.hamcrest.CoreMatchers.*;
    import static org.junit.Assert.*;
    
    import java.text.ParseException;
    import java.util.Arrays;
    import java.util.Date;
    import java.util.List;
    
    import org.apache.commons.lang.time.DateFormatUtils;
    import org.apache.commons.lang.time.DateUtils;
    import org.junit.Test;
    import org.quartz.CronTrigger;
    
    
    public class QuartzCronExpressionTest {
    	private static final String DATE_PATTERN = "yyyy/MM/dd hh:mm:ss";
    
    	@Test
    	public void everyMorningSchedule() throws ParseException {
    		String cronExpression = "0 0 8-10 1,2,3 9 ?";
    		String intialTime = "2010/09/01 09:00:00";
    		List<String> expectedTimeList = Arrays.asList(
    				"2010/09/01 10:00:00",
    				"2010/09/02 08:00:00",
    				"2010/09/02 09:00:00",
    				"2010/09/02 10:00:00",
    				"2010/09/03 08:00:00",
    				"2010/09/03 09:00:00", 
    				"2010/09/03 10:00:00");
    
    		assertSchedule(cronExpression, intialTime, expectedTimeList);
    	}
    
    	private void assertSchedule(String cronExpression, String intialTime,
    		List<String> expectedTimeList) throws ParseException {
    		CronTrigger trigger = new CronTrigger();
    		trigger.setCronExpression(cronExpression);
    		Date previousStartTime = DateUtils.parseDate(intialTime,	new String[]{DATE_PATTERN});
    		
    		for(String exptectedTime : expectedTimeList){
    			trigger.setStartTime(previousStartTime);
    			Date nextExecutionTime =  trigger.getFireTimeAfter(previousStartTime);
    			String actualTime = DateFormatUtils.format(nextExecutionTime, DATE_PATTERN);
    			System.out.printf("expected %s, actual %s \r\n",exptectedTime, actualTime);
    			assertThat("executed on expected time", actualTime, is(exptectedTime));
    			previousStartTime = nextExecutionTime;
    		}		
    	}
    }

    I need help to fix failure of my SpringCronExpressionTest.
    Thank you in advance.

  • #2
    The CronTrigger class interpertes month '9' as October

    I tested it again as following
    Code:
    import java.util.Date;
    import org.apache.commons.lang.time.DateFormatUtils;
    import org.junit.Test;
    import org.springframework.scheduling.support.CronTrigger;
    import org.springframework.scheduling.support.SimpleTriggerContext;
    import static org.junit.Assert.*;
    
    public class CronTriggerTest {
    	
    	@Test
    	public void septemberSchedule() {
    		CronTrigger trigger = new CronTrigger("0 0 8 1 9 ?");
    		SimpleTriggerContext context = new SimpleTriggerContext();
    		Date executionTime = trigger.nextExecutionTime(context);
    		String monthOfExecution = DateFormatUtils.format(executionTime, "MM");
    		assertThat(monthOfExecution, is("09"));
    	}
    }
    The test failed with following messages.
    Code:
    java.lang.AssertionError: 
    Expected: is "09"
         got: "10"
    As I know, '9' should be Setepmber in cron expression, as Quartz CronTrigger do.

    Comment


    • #3
      The bug fixed in Spring 3.0.4

      I found this issue in the jira, and upgrade spring version to 3.0.4, got no problem.

      https://jira.springframework.org/browse/SPR-7384

      Comment

      Working...
      X