diff --git a/spring-all/pom.xml b/spring-all/pom.xml index 5fabeefd67..03e105dc69 100644 --- a/spring-all/pom.xml +++ b/spring-all/pom.xml @@ -33,7 +33,7 @@ - + org.hibernate hibernate-core @@ -118,7 +118,6 @@ ${mockito.version} test - diff --git a/spring-all/src/main/java/org/baeldung/scheduling/ScheduledAnnotationExample.java b/spring-all/src/main/java/org/baeldung/scheduling/ScheduledAnnotationExample.java new file mode 100644 index 0000000000..aa4c8c1466 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/scheduling/ScheduledAnnotationExample.java @@ -0,0 +1,52 @@ +package org.baeldung.scheduling; + +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +@Component +public class ScheduledAnnotationExample { + + @Scheduled(fixedDelay = 1000) + public void scheduleFixedDelayTask() { + System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(fixedDelayString = "${fixedDelay.in.milliseconds}") + public void scheduleFixedDelayTaskUsingExpression() { + System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(fixedDelay = 1000, initialDelay = 2000) + public void scheduleFixedDelayWithInitialDelayTask() { + System.out.println("Fixed delay task with one second initial delay - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(fixedRate = 1000) + public void scheduleFixedRateTask() { + System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(fixedRateString = "${fixedRate.in.milliseconds}") + public void scheduleFixedRateTaskUsingExpression() { + System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(fixedDelay = 1000, initialDelay = 100) + public void scheduleFixedRateWithInitialDelayTask() { + System.out.println("Fixed delay task with one second initial delay - " + System.currentTimeMillis() / 1000); + } + + /** + * Scheduled task is executed at 10:15 AM on the 15th day of every month + */ + @Scheduled(cron = "0 15 10 15 * ?") + public void scheduleTaskUsingCronExpression() { + System.out.println("schedule tasks using cron expressions - " + System.currentTimeMillis() / 1000); + } + + @Scheduled(cron = "${cron.expression}") + public void scheduleTaskUsingExternalizedCronExpression() { + System.out.println("schedule tasks using externalized cron expressions - " + System.currentTimeMillis() / 1000); + } + +} diff --git a/spring-all/src/main/java/org/baeldung/scheduling/SpringSchedulingConfig.java b/spring-all/src/main/java/org/baeldung/scheduling/SpringSchedulingConfig.java new file mode 100644 index 0000000000..b765001bf5 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/scheduling/SpringSchedulingConfig.java @@ -0,0 +1,20 @@ +package org.baeldung.scheduling; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; +import org.springframework.scheduling.annotation.EnableScheduling; + +@Configuration +@EnableScheduling +@ComponentScan("com.baeldung.spring.integration") +@PropertySource("classpath:springIntegration.properties") +public class SpringSchedulingConfig { + + @Bean + public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } +} \ No newline at end of file diff --git a/spring-all/src/main/resources/springScheduled-config.xml b/spring-all/src/main/resources/springScheduled-config.xml new file mode 100644 index 0000000000..78240c02e4 --- /dev/null +++ b/spring-all/src/main/resources/springScheduled-config.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-all/src/main/resources/springScheduled.properties b/spring-all/src/main/resources/springScheduled.properties new file mode 100644 index 0000000000..9bd0713ecd --- /dev/null +++ b/spring-all/src/main/resources/springScheduled.properties @@ -0,0 +1,3 @@ +cron.expression=0 15 10 15 * ? +fixedRate.in.millisecons=1000 +fixedDelay.in.millisecons=1000 \ No newline at end of file diff --git a/spring-all/src/test/java/org/baeldung/scheduling/ScheduledAnnotationExampleTest.java b/spring-all/src/test/java/org/baeldung/scheduling/ScheduledAnnotationExampleTest.java new file mode 100644 index 0000000000..97807200cf --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/scheduling/ScheduledAnnotationExampleTest.java @@ -0,0 +1,18 @@ +package org.baeldung.scheduling; + +import org.baeldung.scheduling.SpringSchedulingConfig; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SpringSchedulingConfig.class }, loader = AnnotationConfigContextLoader.class) +public class ScheduledAnnotationExampleTest { + + @Test + public void testScheduledAnnotation() throws InterruptedException { + Thread.sleep(20000); + } +}