Search in sources :

Example 11 with ScheduledMethodRunnable

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

the class ScheduledAnnotationBeanPostProcessor method processScheduled.

protected void processScheduled(Scheduled scheduled, Method method, Object bean) {
    try {
        Assert.isTrue(method.getParameterCount() == 0, "Only no-arg methods may be annotated with @Scheduled");
        Method invocableMethod = AopUtils.selectInvocableMethod(method, bean.getClass());
        Runnable runnable = new ScheduledMethodRunnable(bean, invocableMethod);
        boolean processedSchedule = false;
        String errorMessage = "Exactly one of the 'cron', 'fixedDelay(String)', or 'fixedRate(String)' attributes is required";
        Set<ScheduledTask> tasks = new LinkedHashSet<>(4);
        // Determine initial delay
        long initialDelay = scheduled.initialDelay();
        String initialDelayString = scheduled.initialDelayString();
        if (StringUtils.hasText(initialDelayString)) {
            Assert.isTrue(initialDelay < 0, "Specify 'initialDelay' or 'initialDelayString', not both");
            if (this.embeddedValueResolver != null) {
                initialDelayString = this.embeddedValueResolver.resolveStringValue(initialDelayString);
            }
            try {
                initialDelay = Long.parseLong(initialDelayString);
            } catch (NumberFormatException ex) {
                throw new IllegalArgumentException("Invalid initialDelayString value \"" + initialDelayString + "\" - cannot parse into integer");
            }
        }
        // Check cron expression
        String cron = scheduled.cron();
        if (StringUtils.hasText(cron)) {
            Assert.isTrue(initialDelay == -1, "'initialDelay' not supported for cron triggers");
            processedSchedule = true;
            String zone = scheduled.zone();
            if (this.embeddedValueResolver != null) {
                cron = this.embeddedValueResolver.resolveStringValue(cron);
                zone = this.embeddedValueResolver.resolveStringValue(zone);
            }
            TimeZone timeZone;
            if (StringUtils.hasText(zone)) {
                timeZone = StringUtils.parseTimeZoneString(zone);
            } else {
                timeZone = TimeZone.getDefault();
            }
            tasks.add(this.registrar.scheduleCronTask(new CronTask(runnable, new CronTrigger(cron, timeZone))));
        }
        // At this point we don't need to differentiate between initial delay set or not anymore
        if (initialDelay < 0) {
            initialDelay = 0;
        }
        // Check fixed delay
        long fixedDelay = scheduled.fixedDelay();
        if (fixedDelay >= 0) {
            Assert.isTrue(!processedSchedule, errorMessage);
            processedSchedule = true;
            tasks.add(this.registrar.scheduleFixedDelayTask(new IntervalTask(runnable, fixedDelay, initialDelay)));
        }
        String fixedDelayString = scheduled.fixedDelayString();
        if (StringUtils.hasText(fixedDelayString)) {
            Assert.isTrue(!processedSchedule, errorMessage);
            processedSchedule = true;
            if (this.embeddedValueResolver != null) {
                fixedDelayString = this.embeddedValueResolver.resolveStringValue(fixedDelayString);
            }
            try {
                fixedDelay = Long.parseLong(fixedDelayString);
            } catch (NumberFormatException ex) {
                throw new IllegalArgumentException("Invalid fixedDelayString value \"" + fixedDelayString + "\" - cannot parse into integer");
            }
            tasks.add(this.registrar.scheduleFixedDelayTask(new IntervalTask(runnable, fixedDelay, initialDelay)));
        }
        // Check fixed rate
        long fixedRate = scheduled.fixedRate();
        if (fixedRate >= 0) {
            Assert.isTrue(!processedSchedule, errorMessage);
            processedSchedule = true;
            tasks.add(this.registrar.scheduleFixedRateTask(new IntervalTask(runnable, fixedRate, initialDelay)));
        }
        String fixedRateString = scheduled.fixedRateString();
        if (StringUtils.hasText(fixedRateString)) {
            Assert.isTrue(!processedSchedule, errorMessage);
            processedSchedule = true;
            if (this.embeddedValueResolver != null) {
                fixedRateString = this.embeddedValueResolver.resolveStringValue(fixedRateString);
            }
            try {
                fixedRate = Long.parseLong(fixedRateString);
            } catch (NumberFormatException ex) {
                throw new IllegalArgumentException("Invalid fixedRateString value \"" + fixedRateString + "\" - cannot parse into integer");
            }
            tasks.add(this.registrar.scheduleFixedRateTask(new IntervalTask(runnable, fixedRate, initialDelay)));
        }
        // Check whether we had any attribute set
        Assert.isTrue(processedSchedule, errorMessage);
        // Finally register the scheduled tasks
        synchronized (this.scheduledTasks) {
            Set<ScheduledTask> registeredTasks = this.scheduledTasks.get(bean);
            if (registeredTasks == null) {
                registeredTasks = new LinkedHashSet<>(4);
                this.scheduledTasks.put(bean, registeredTasks);
            }
            registeredTasks.addAll(tasks);
        }
    } catch (IllegalArgumentException ex) {
        throw new IllegalStateException("Encountered invalid @Scheduled method '" + method.getName() + "': " + ex.getMessage());
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CronTrigger(org.springframework.scheduling.support.CronTrigger) CronTask(org.springframework.scheduling.config.CronTask) ScheduledTask(org.springframework.scheduling.config.ScheduledTask) Method(java.lang.reflect.Method) ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable) TimeZone(java.util.TimeZone) IntervalTask(org.springframework.scheduling.config.IntervalTask) ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable)

Example 12 with ScheduledMethodRunnable

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

the class ScheduledTasksBeanDefinitionParserTests method checkTarget.

@Test
public void checkTarget() {
    List<IntervalTask> tasks = (List<IntervalTask>) new DirectFieldAccessor(this.registrar).getPropertyValue("fixedRateTasks");
    Runnable runnable = tasks.get(0).getRunnable();
    assertEquals(ScheduledMethodRunnable.class, runnable.getClass());
    Object targetObject = ((ScheduledMethodRunnable) runnable).getTarget();
    Method targetMethod = ((ScheduledMethodRunnable) runnable).getMethod();
    assertEquals(this.testBean, targetObject);
    assertEquals("test", targetMethod.getName());
}
Also used : ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ScheduledMethodRunnable(org.springframework.scheduling.support.ScheduledMethodRunnable) List(java.util.List) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 13 with ScheduledMethodRunnable

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

the class ScheduledAnnotationBeanPostProcessorTests method composedAnnotationWithInitialDelayAndFixedRate.

@Test
public void composedAnnotationWithInitialDelayAndFixedRate() {
    BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
    BeanDefinition targetDefinition = new RootBeanDefinition(ComposedAnnotationFixedRateTestBean.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());
    assertEquals(1000L, task.getInitialDelay());
}
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 14 with ScheduledMethodRunnable

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

the class ScheduledAnnotationBeanPostProcessorTests method nonVoidReturnType.

@Test
public void nonVoidReturnType() {
    BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
    BeanDefinition targetDefinition = new RootBeanDefinition(NonVoidReturnTypeTestBean.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 9-17 * * MON-FRI", 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 15 with ScheduledMethodRunnable

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

the class ScheduledAnnotationBeanPostProcessorTests method fixedRateTaskWithInitialDelay.

@Test
public void fixedRateTaskWithInitialDelay() {
    BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
    BeanDefinition targetDefinition = new RootBeanDefinition(FixedRateWithInitialDelayTestBean.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(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) Test(org.junit.Test)

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