Skip to content

Commit bf8bc8b

Browse files
author
Eugen
committed
Merge pull request eugenp#59 from mgooty/master
Added example code for usage and configuration of @scheduled annotation.
2 parents 9b0dfc4 + fbe55d0 commit bf8bc8b

6 files changed

Lines changed: 114 additions & 2 deletions

File tree

spring-all/pom.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
</dependency>
3434

3535
<!-- persistence -->
36-
36+
3737
<dependency>
3838
<groupId>org.hibernate</groupId>
3939
<artifactId>hibernate-core</artifactId>
@@ -118,7 +118,6 @@
118118
<version>${mockito.version}</version>
119119
<scope>test</scope>
120120
</dependency>
121-
122121
</dependencies>
123122

124123
<build>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.baeldung.scheduling;
2+
3+
import org.springframework.scheduling.annotation.Scheduled;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
public class ScheduledAnnotationExample {
8+
9+
@Scheduled(fixedDelay = 1000)
10+
public void scheduleFixedDelayTask() {
11+
System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000);
12+
}
13+
14+
@Scheduled(fixedDelayString = "${fixedDelay.in.milliseconds}")
15+
public void scheduleFixedDelayTaskUsingExpression() {
16+
System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000);
17+
}
18+
19+
@Scheduled(fixedDelay = 1000, initialDelay = 2000)
20+
public void scheduleFixedDelayWithInitialDelayTask() {
21+
System.out.println("Fixed delay task with one second initial delay - " + System.currentTimeMillis() / 1000);
22+
}
23+
24+
@Scheduled(fixedRate = 1000)
25+
public void scheduleFixedRateTask() {
26+
System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000);
27+
}
28+
29+
@Scheduled(fixedRateString = "${fixedRate.in.milliseconds}")
30+
public void scheduleFixedRateTaskUsingExpression() {
31+
System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000);
32+
}
33+
34+
@Scheduled(fixedDelay = 1000, initialDelay = 100)
35+
public void scheduleFixedRateWithInitialDelayTask() {
36+
System.out.println("Fixed delay task with one second initial delay - " + System.currentTimeMillis() / 1000);
37+
}
38+
39+
/**
40+
* Scheduled task is executed at 10:15 AM on the 15th day of every month
41+
*/
42+
@Scheduled(cron = "0 15 10 15 * ?")
43+
public void scheduleTaskUsingCronExpression() {
44+
System.out.println("schedule tasks using cron expressions - " + System.currentTimeMillis() / 1000);
45+
}
46+
47+
@Scheduled(cron = "${cron.expression}")
48+
public void scheduleTaskUsingExternalizedCronExpression() {
49+
System.out.println("schedule tasks using externalized cron expressions - " + System.currentTimeMillis() / 1000);
50+
}
51+
52+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.baeldung.scheduling;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.PropertySource;
7+
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
8+
import org.springframework.scheduling.annotation.EnableScheduling;
9+
10+
@Configuration
11+
@EnableScheduling
12+
@ComponentScan("com.baeldung.spring.integration")
13+
@PropertySource("classpath:springIntegration.properties")
14+
public class SpringSchedulingConfig {
15+
16+
@Bean
17+
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
18+
return new PropertySourcesPlaceholderConfigurer();
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context"
4+
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
5+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
6+
7+
<!-- Configure the scheduler -->
8+
<task:scheduler id="myScheduler" pool-size="10" />
9+
10+
<bean id="myscheduler" name="myscheduler"
11+
class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
12+
</bean>
13+
14+
<!-- Configure the fixedDealy, fixedRate or cron based schduled tasks -->
15+
<task:scheduled-tasks scheduler="myScheduler">
16+
<task:scheduled ref="beanA" method="methodA" fixed-delay="5000" initial-delay="1000" />
17+
<task:scheduled ref="beanB" method="methodB" fixed-rate="5000" />
18+
<task:scheduled ref="beanC" method="methodC" cron="*/5 * * * * MON-FRI" />
19+
</task:scheduled-tasks>
20+
</beans>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cron.expression=0 15 10 15 * ?
2+
fixedRate.in.millisecons=1000
3+
fixedDelay.in.millisecons=1000
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.baeldung.scheduling;
2+
3+
import org.baeldung.scheduling.SpringSchedulingConfig;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.test.context.ContextConfiguration;
7+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
9+
10+
@RunWith(SpringJUnit4ClassRunner.class)
11+
@ContextConfiguration(classes = { SpringSchedulingConfig.class }, loader = AnnotationConfigContextLoader.class)
12+
public class ScheduledAnnotationExampleTest {
13+
14+
@Test
15+
public void testScheduledAnnotation() throws InterruptedException {
16+
Thread.sleep(20000);
17+
}
18+
}

0 commit comments

Comments
 (0)