Skip to content

Commit 7bff6bb

Browse files
author
Eugen
committed
Merge pull request eugenp#60 from mgooty/master
Added XML configurations support to schedule tasks
2 parents bf8bc8b + 68ff312 commit 7bff6bb

File tree

7 files changed

+53
-16
lines changed

7 files changed

+53
-16
lines changed

spring-all/src/main/java/org/baeldung/scheduling/ScheduledAnnotationExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.springframework.scheduling.annotation.Scheduled;
44
import org.springframework.stereotype.Component;
55

6-
@Component
6+
@Component("scheduledAnnotationExample")
77
public class ScheduledAnnotationExample {
88

99
@Scheduled(fixedDelay = 1000)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.baeldung.scheduling;
2+
3+
public class SchedulingWithXmlConfig {
4+
5+
public void scheduleFixedDelayTask() {
6+
System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000);
7+
}
8+
9+
public void scheduleFixedRateTask() {
10+
System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000);
11+
}
12+
13+
public void scheduleTaskUsingCronExpression() {
14+
System.out.println("schedule tasks using cron expressions - " + System.currentTimeMillis() / 1000);
15+
}
16+
}

spring-all/src/main/java/org/baeldung/scheduling/SpringSchedulingConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
@Configuration
1111
@EnableScheduling
12-
@ComponentScan("com.baeldung.spring.integration")
13-
@PropertySource("classpath:springIntegration.properties")
12+
@ComponentScan("org.baeldung.scheduling")
13+
@PropertySource("classpath:springScheduled.properties")
1414
public class SpringSchedulingConfig {
1515

1616
@Bean
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
<?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
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xsi:schemaLocation="
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
7+
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
58
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
69

10+
<context:property-placeholder location="classpath:springScheduled.properties" />
11+
712
<!-- Configure the scheduler -->
813
<task:scheduler id="myScheduler" pool-size="10" />
914

10-
<bean id="myscheduler" name="myscheduler"
11-
class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
12-
</bean>
15+
<bean id="myscheduler"
16+
class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler" />
1317

1418
<!-- Configure the fixedDealy, fixedRate or cron based schduled tasks -->
19+
<bean id="schedulingWithXmlConfig" class="org.baeldung.scheduling.SchedulingWithXmlConfig" />
20+
1521
<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" />
22+
<task:scheduled ref="schedulingWithXmlConfig" method="scheduleFixedDelayTask" fixed-delay="${fixedDelay.in.milliseconds}" initial-delay="1000" />
23+
<task:scheduled ref="schedulingWithXmlConfig" method="scheduleFixedRateTask" fixed-rate="${fixedRate.in.milliseconds}" />
24+
<task:scheduled ref="schedulingWithXmlConfig" method="scheduleTaskUsingCronExpression" cron="${cron.expression}" />
1925
</task:scheduled-tasks>
2026
</beans>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
cron.expression=0 15 10 15 * ?
2-
fixedRate.in.millisecons=1000
3-
fixedDelay.in.millisecons=1000
2+
fixedRate.in.milliseconds=1000
3+
fixedDelay.in.milliseconds=1000

spring-all/src/test/java/org/baeldung/scheduling/ScheduledAnnotationExampleTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.baeldung.scheduling;
22

3-
import org.baeldung.scheduling.SpringSchedulingConfig;
43
import org.junit.Test;
54
import org.junit.runner.RunWith;
65
import org.springframework.test.context.ContextConfiguration;
@@ -13,6 +12,6 @@ public class ScheduledAnnotationExampleTest {
1312

1413
@Test
1514
public void testScheduledAnnotation() throws InterruptedException {
16-
Thread.sleep(20000);
15+
Thread.sleep(5000);
1716
}
1817
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.baeldung.scheduling;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.test.context.ContextConfiguration;
6+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
7+
8+
@RunWith(SpringJUnit4ClassRunner.class)
9+
@ContextConfiguration("classpath:springScheduled-config.xml")
10+
public class SchedulingWithXmlConfigTest {
11+
12+
@Test
13+
public void testXmlBasedScheduling() throws InterruptedException {
14+
Thread.sleep(5000);
15+
}
16+
}

0 commit comments

Comments
 (0)