Search in sources :

Example 6 with ScheduledTaskRegistrar

use of org.springframework.scheduling.config.ScheduledTaskRegistrar in project spring-framework by spring-projects.

the class ScheduledAnnotationBeanPostProcessorTests method fixedDelayTask.

@Test
public void fixedDelayTask() {
    BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
    BeanDefinition targetDefinition = new RootBeanDefinition(FixedDelayTestBean.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<IntervalTask> fixedDelayTasks = (List<IntervalTask>) new DirectFieldAccessor(registrar).getPropertyValue("fixedDelayTasks");
    assertEquals(1, fixedDelayTasks.size());
    IntervalTask task = fixedDelayTasks.get(0);
    ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
    Object targetObject = runnable.getTarget();
    Method targetMethod = runnable.getMethod();
    assertEquals(target, targetObject);
    assertEquals("fixedDelay", targetMethod.getName());
    assertEquals(0L, task.getInitialDelay());
    assertEquals(5000L, task.getInterval());
}
Also used : ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable) IntervalTask(org.springframework.scheduling.config.IntervalTask) ScheduledTaskRegistrar(org.springframework.scheduling.config.ScheduledTaskRegistrar) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) List(java.util.List) Method(java.lang.reflect.Method) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 7 with ScheduledTaskRegistrar

use of org.springframework.scheduling.config.ScheduledTaskRegistrar in project spring-framework by spring-projects.

the class ScheduledAnnotationBeanPostProcessorTests method propertyPlaceholderForMetaAnnotation.

@Test
public void propertyPlaceholderForMetaAnnotation() {
    String businessHoursCronExpression = "0 0 9-17 * * MON-FRI";
    BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
    BeanDefinition placeholderDefinition = new RootBeanDefinition(PropertyPlaceholderConfigurer.class);
    Properties properties = new Properties();
    properties.setProperty("schedules.businessHours", businessHoursCronExpression);
    placeholderDefinition.getPropertyValues().addPropertyValue("properties", properties);
    BeanDefinition targetDefinition = new RootBeanDefinition(PropertyPlaceholderMetaAnnotationTestBean.class);
    context.registerBeanDefinition("postProcessor", processorDefinition);
    context.registerBeanDefinition("placeholder", placeholderDefinition);
    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("y", targetMethod.getName());
    assertEquals(businessHoursCronExpression, task.getExpression());
}
Also used : CronTask(org.springframework.scheduling.config.CronTask) Method(java.lang.reflect.Method) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Properties(java.util.Properties) ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable) ScheduledTaskRegistrar(org.springframework.scheduling.config.ScheduledTaskRegistrar) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) List(java.util.List) Test(org.junit.Test)

Example 8 with ScheduledTaskRegistrar

use of org.springframework.scheduling.config.ScheduledTaskRegistrar 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 9 with ScheduledTaskRegistrar

use of org.springframework.scheduling.config.ScheduledTaskRegistrar in project spring-framework by spring-projects.

the class ScheduledAnnotationBeanPostProcessorTests method metaAnnotationWithCronExpression.

@Test
public void metaAnnotationWithCronExpression() {
    BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
    BeanDefinition targetDefinition = new RootBeanDefinition(MetaAnnotationCronTestBean.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("generateReport", targetMethod.getName());
    assertEquals("0 0 * * * ?", task.getExpression());
}
Also used : ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable) ScheduledTaskRegistrar(org.springframework.scheduling.config.ScheduledTaskRegistrar) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) CronTask(org.springframework.scheduling.config.CronTask) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) List(java.util.List) Method(java.lang.reflect.Method) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 10 with ScheduledTaskRegistrar

use of org.springframework.scheduling.config.ScheduledTaskRegistrar in project spring-framework by spring-projects.

the class ScheduledAnnotationBeanPostProcessorTests method fixedRateTask.

@Test
public void fixedRateTask() {
    BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
    BeanDefinition targetDefinition = new RootBeanDefinition(FixedRateTestBean.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<IntervalTask> fixedRateTasks = (List<IntervalTask>) new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
    assertEquals(1, fixedRateTasks.size());
    IntervalTask task = fixedRateTasks.get(0);
    ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
    Object targetObject = runnable.getTarget();
    Method targetMethod = runnable.getMethod();
    assertEquals(target, targetObject);
    assertEquals("fixedRate", targetMethod.getName());
    assertEquals(0L, task.getInitialDelay());
    assertEquals(3000L, task.getInterval());
}
Also used : ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable) IntervalTask(org.springframework.scheduling.config.IntervalTask) ScheduledTaskRegistrar(org.springframework.scheduling.config.ScheduledTaskRegistrar) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) List(java.util.List) Method(java.lang.reflect.Method) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Aggregations

ScheduledTaskRegistrar (org.springframework.scheduling.config.ScheduledTaskRegistrar)18 Test (org.junit.Test)17 Method (java.lang.reflect.Method)15 List (java.util.List)15 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)15 ScheduledMethodRunnable (org.springframework.scheduling.support.ScheduledMethodRunnable)15 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)14 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)14 IntervalTask (org.springframework.scheduling.config.IntervalTask)8 CronTask (org.springframework.scheduling.config.CronTask)7 Properties (java.util.Properties)4 Calendar (java.util.Calendar)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Trigger (org.springframework.scheduling.Trigger)1 TriggerContext (org.springframework.scheduling.TriggerContext)1 CronTrigger (org.springframework.scheduling.support.CronTrigger)1 SimpleTriggerContext (org.springframework.scheduling.support.SimpleTriggerContext)1