Search in sources :

Example 1 with TriggerContext

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);
}
Also used : CronTrigger(org.springframework.scheduling.support.CronTrigger) CronTask(org.springframework.scheduling.config.CronTask) Calendar(java.util.Calendar) Method(java.lang.reflect.Method) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) SimpleTriggerContext(org.springframework.scheduling.support.SimpleTriggerContext) Date(java.util.Date) ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable) Trigger(org.springframework.scheduling.Trigger) CronTrigger(org.springframework.scheduling.support.CronTrigger) ScheduledTaskRegistrar(org.springframework.scheduling.config.ScheduledTaskRegistrar) SimpleTriggerContext(org.springframework.scheduling.support.SimpleTriggerContext) TriggerContext(org.springframework.scheduling.TriggerContext) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) List(java.util.List) Test(org.junit.Test)

Example 2 with TriggerContext

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);
}
Also used : TriggerContext(org.springframework.scheduling.TriggerContext) Date(java.util.Date) Test(org.junit.Test)

Example 3 with TriggerContext

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));
}
Also used : TriggerContext(org.springframework.scheduling.TriggerContext) Date(java.util.Date) Test(org.junit.Test)

Example 4 with TriggerContext

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));
}
Also used : TriggerContext(org.springframework.scheduling.TriggerContext) Date(java.util.Date) Test(org.junit.Test)

Example 5 with TriggerContext

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));
}
Also used : TriggerContext(org.springframework.scheduling.TriggerContext) Date(java.util.Date) Test(org.junit.Test)

Aggregations

TriggerContext (org.springframework.scheduling.TriggerContext)66 Date (java.util.Date)61 Test (org.junit.Test)33 Trigger (org.springframework.scheduling.Trigger)5 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ScheduledFuture (java.util.concurrent.ScheduledFuture)2 Advice (org.aopalliance.aop.Advice)2 AbstractMessageSourceAdvice (org.springframework.integration.aop.AbstractMessageSourceAdvice)2 CompoundTriggerAdvice (org.springframework.integration.aop.CompoundTriggerAdvice)2 SimpleActiveIdleMessageSourceAdvice (org.springframework.integration.aop.SimpleActiveIdleMessageSourceAdvice)2 PollSkipAdvice (org.springframework.integration.scheduling.PollSkipAdvice)2 OnlyOnceTrigger (org.springframework.integration.test.util.OnlyOnceTrigger)2 CompoundTrigger (org.springframework.integration.util.CompoundTrigger)2 DynamicPeriodicTrigger (org.springframework.integration.util.DynamicPeriodicTrigger)2 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)2 ScheduledMethodRunnable (org.springframework.scheduling.support.ScheduledMethodRunnable)2 Method (java.lang.reflect.Method)1 Calendar (java.util.Calendar)1 List (java.util.List)1