use of org.springframework.scheduling.TriggerContext in project spring-framework by spring-projects.
the class ScheduledAnnotationBeanPostProcessorTests method cronTaskWithZone.
@Test
public void cronTaskWithZone() throws InterruptedException {
Assume.group(TestGroup.LONG_RUNNING);
BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
BeanDefinition targetDefinition = new RootBeanDefinition(CronWithTimezoneTestBean.class);
context.registerBeanDefinition("postProcessor", processorDefinition);
context.registerBeanDefinition("target", targetDefinition);
context.refresh();
Object postProcessor = context.getBean("postProcessor");
Object target = context.getBean("target");
ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar) new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
@SuppressWarnings("unchecked") List<CronTask> cronTasks = (List<CronTask>) new DirectFieldAccessor(registrar).getPropertyValue("cronTasks");
assertEquals(1, cronTasks.size());
CronTask task = cronTasks.get(0);
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
Object targetObject = runnable.getTarget();
Method targetMethod = runnable.getMethod();
assertEquals(target, targetObject);
assertEquals("cron", targetMethod.getName());
assertEquals("0 0 0-4,6-23 * * ?", task.getExpression());
Trigger trigger = task.getTrigger();
assertNotNull(trigger);
assertTrue(trigger instanceof CronTrigger);
CronTrigger cronTrigger = (CronTrigger) trigger;
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+10"));
cal.clear();
// 15-04-2013 4:00 GMT+10
cal.set(2013, 3, 15, 4, 0);
Date lastScheduledExecutionTime = cal.getTime();
Date lastActualExecutionTime = cal.getTime();
// 4:30
cal.add(Calendar.MINUTE, 30);
Date lastCompletionTime = cal.getTime();
TriggerContext triggerContext = new SimpleTriggerContext(lastScheduledExecutionTime, lastActualExecutionTime, lastCompletionTime);
cal.add(Calendar.MINUTE, 30);
// 6:00
cal.add(Calendar.HOUR_OF_DAY, 1);
Date nextExecutionTime = cronTrigger.nextExecutionTime(triggerContext);
// assert that 6:00 is next execution time
assertEquals(cal.getTime(), nextExecutionTime);
Thread.sleep(10000);
}
use of org.springframework.scheduling.TriggerContext in project spring-framework by spring-projects.
the class CronTriggerTests method testNonExistentSpecificDate.
@Test(expected = IllegalArgumentException.class)
public void testNonExistentSpecificDate() throws Exception {
// TODO: maybe try and detect this as a special case in parser?
CronTrigger trigger = new CronTrigger("0 0 0 31 6 *", timeZone);
calendar.set(Calendar.DAY_OF_MONTH, 10);
calendar.set(Calendar.MONTH, 2);
Date date = calendar.getTime();
TriggerContext context1 = getTriggerContext(date);
trigger.nextExecutionTime(context1);
// new CronTrigger("0 0 0 30 1 ?", timeZone);
}
use of org.springframework.scheduling.TriggerContext in project spring-framework by spring-projects.
the class CronTriggerTests method testMonthSequence.
@Test
public void testMonthSequence() throws Exception {
CronTrigger trigger = new CronTrigger("0 30 23 30 1/3 ?", timeZone);
calendar.set(2010, 11, 30);
Date date = calendar.getTime();
// set expected next trigger time
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
calendar.add(Calendar.MONTH, 1);
TriggerContext context1 = getTriggerContext(date);
assertEquals(calendar.getTime(), date = trigger.nextExecutionTime(context1));
// Next trigger is 3 months latter
calendar.add(Calendar.MONTH, 3);
TriggerContext context2 = getTriggerContext(date);
assertEquals(calendar.getTime(), date = trigger.nextExecutionTime(context2));
// Next trigger is 3 months latter
calendar.add(Calendar.MONTH, 3);
TriggerContext context3 = getTriggerContext(date);
assertEquals(calendar.getTime(), date = trigger.nextExecutionTime(context3));
}
use of org.springframework.scheduling.TriggerContext in project spring-framework by spring-projects.
the class CronTriggerTests method testMonthlyTriggerInShortMonth.
@Test
public void testMonthlyTriggerInShortMonth() throws Exception {
CronTrigger trigger = new CronTrigger("0 0 0 1 * *", timeZone);
calendar.set(Calendar.MONTH, 9);
calendar.set(Calendar.DAY_OF_MONTH, 30);
Date date = calendar.getTime();
calendar.set(Calendar.MONTH, 10);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
TriggerContext context = getTriggerContext(date);
assertEquals(calendar.getTime(), trigger.nextExecutionTime(context));
}
use of org.springframework.scheduling.TriggerContext in project spring-framework by spring-projects.
the class CronTriggerTests method testIncrementDayOfMonth.
@Test
public void testIncrementDayOfMonth() throws Exception {
CronTrigger trigger = new CronTrigger("0 0 0 * * *", timeZone);
calendar.set(Calendar.DAY_OF_MONTH, 1);
Date date = calendar.getTime();
calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
TriggerContext context1 = getTriggerContext(date);
assertEquals(calendar.getTime(), date = trigger.nextExecutionTime(context1));
assertEquals(2, calendar.get(Calendar.DAY_OF_MONTH));
calendar.add(Calendar.DAY_OF_MONTH, 1);
TriggerContext context2 = getTriggerContext(date);
assertEquals(calendar.getTime(), date = trigger.nextExecutionTime(context2));
assertEquals(3, calendar.get(Calendar.DAY_OF_MONTH));
}
Aggregations