Search in sources :

Example 6 with ScheduledMethodRunnable

use of org.springframework.scheduling.support.ScheduledMethodRunnable 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 7 with ScheduledMethodRunnable

use of org.springframework.scheduling.support.ScheduledMethodRunnable 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)

Example 8 with ScheduledMethodRunnable

use of org.springframework.scheduling.support.ScheduledMethodRunnable in project spring-framework by spring-projects.

the class ScheduledAnnotationBeanPostProcessorTests method propertyPlaceholderWithFixedRate.

@Test
public void propertyPlaceholderWithFixedRate() {
    BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
    BeanDefinition placeholderDefinition = new RootBeanDefinition(PropertyPlaceholderConfigurer.class);
    Properties properties = new Properties();
    properties.setProperty("fixedRate", "3000");
    properties.setProperty("initialDelay", "1000");
    placeholderDefinition.getPropertyValues().addPropertyValue("properties", properties);
    BeanDefinition targetDefinition = new RootBeanDefinition(PropertyPlaceholderWithFixedRateTestBean.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<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(1000L, 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) Properties(java.util.Properties) Test(org.junit.Test)

Example 9 with ScheduledMethodRunnable

use of org.springframework.scheduling.support.ScheduledMethodRunnable in project spring-framework by spring-projects.

the class ScheduledAnnotationBeanPostProcessorTests method metaAnnotationWithFixedRate.

@Test
public void metaAnnotationWithFixedRate() {
    BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
    BeanDefinition targetDefinition = new RootBeanDefinition(MetaAnnotationFixedRateTestBean.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("checkForUpdates", targetMethod.getName());
    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 10 with ScheduledMethodRunnable

use of org.springframework.scheduling.support.ScheduledMethodRunnable in project gocd by gocd.

the class ScheduledTasksIntegrationTest method assertScheduledTask.

private void assertScheduledTask(String taskType, Object targetObject, String methodName, Object time) {
    Map<Runnable, String> fixedDelayTasks = (Map<Runnable, String>) ReflectionUtil.getField(scheduledTaskRegistrar, taskType);
    boolean matchFound = false;
    for (Runnable runnable : fixedDelayTasks.keySet()) {
        if (runnable instanceof ScheduledMethodRunnable) {
            ScheduledMethodRunnable scheduledMethodRunnable = (ScheduledMethodRunnable) runnable;
            Object target = scheduledMethodRunnable.getTarget();
            if (target.equals(targetObject)) {
                matchFound = true;
                Method method = scheduledMethodRunnable.getMethod();
                assertThat(method.getName(), Is.is(methodName));
                assertThat(fixedDelayTasks.get(runnable), is(time));
            }
        }
    }
    assertThat("Could not find a scheduled job for configRepositoryGCWarningService.checkRepoAndAddWarningIfRequired", matchFound, Is.is(true));
}
Also used : ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable) ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable) Method(java.lang.reflect.Method) Map(java.util.Map)

Aggregations

Method (java.lang.reflect.Method)19 ScheduledMethodRunnable (org.springframework.scheduling.support.ScheduledMethodRunnable)19 List (java.util.List)16 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)16 Test (org.junit.Test)15 ScheduledTaskRegistrar (org.springframework.scheduling.config.ScheduledTaskRegistrar)15 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)14 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)14 IntervalTask (org.springframework.scheduling.config.IntervalTask)9 CronTask (org.springframework.scheduling.config.CronTask)8 Properties (java.util.Properties)4 CronTrigger (org.springframework.scheduling.support.CronTrigger)2 Calendar (java.util.Calendar)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 TimeZone (java.util.TimeZone)1 Trigger (org.springframework.scheduling.Trigger)1 TriggerContext (org.springframework.scheduling.TriggerContext)1