Added XML configurations to schedule tasks.

This commit is contained in:
mgooty 2014-10-20 15:50:41 +05:30
parent fbe55d0584
commit 68ff312941
7 changed files with 53 additions and 16 deletions

View File

@ -3,7 +3,7 @@ package org.baeldung.scheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@Component("scheduledAnnotationExample")
public class ScheduledAnnotationExample {
@Scheduled(fixedDelay = 1000)

View File

@ -0,0 +1,16 @@
package org.baeldung.scheduling;
public class SchedulingWithXmlConfig {
public void scheduleFixedDelayTask() {
System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000);
}
public void scheduleFixedRateTask() {
System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000);
}
public void scheduleTaskUsingCronExpression() {
System.out.println("schedule tasks using cron expressions - " + System.currentTimeMillis() / 1000);
}
}

View File

@ -9,8 +9,8 @@ import org.springframework.scheduling.annotation.EnableScheduling;
@Configuration
@EnableScheduling
@ComponentScan("com.baeldung.spring.integration")
@PropertySource("classpath:springIntegration.properties")
@ComponentScan("org.baeldung.scheduling")
@PropertySource("classpath:springScheduled.properties")
public class SpringSchedulingConfig {
@Bean

View File

@ -1,20 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:property-placeholder location="classpath:springScheduled.properties" />
<!-- Configure the scheduler -->
<task:scheduler id="myScheduler" pool-size="10" />
<bean id="myscheduler" name="myscheduler"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
</bean>
<bean id="myscheduler"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler" />
<!-- Configure the fixedDealy, fixedRate or cron based schduled tasks -->
<bean id="schedulingWithXmlConfig" class="org.baeldung.scheduling.SchedulingWithXmlConfig" />
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="beanA" method="methodA" fixed-delay="5000" initial-delay="1000" />
<task:scheduled ref="beanB" method="methodB" fixed-rate="5000" />
<task:scheduled ref="beanC" method="methodC" cron="*/5 * * * * MON-FRI" />
<task:scheduled ref="schedulingWithXmlConfig" method="scheduleFixedDelayTask" fixed-delay="${fixedDelay.in.milliseconds}" initial-delay="1000" />
<task:scheduled ref="schedulingWithXmlConfig" method="scheduleFixedRateTask" fixed-rate="${fixedRate.in.milliseconds}" />
<task:scheduled ref="schedulingWithXmlConfig" method="scheduleTaskUsingCronExpression" cron="${cron.expression}" />
</task:scheduled-tasks>
</beans>

View File

@ -1,3 +1,3 @@
cron.expression=0 15 10 15 * ?
fixedRate.in.millisecons=1000
fixedDelay.in.millisecons=1000
fixedRate.in.milliseconds=1000
fixedDelay.in.milliseconds=1000

View File

@ -1,6 +1,5 @@
package org.baeldung.scheduling;
import org.baeldung.scheduling.SpringSchedulingConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
@ -13,6 +12,6 @@ public class ScheduledAnnotationExampleTest {
@Test
public void testScheduledAnnotation() throws InterruptedException {
Thread.sleep(20000);
Thread.sleep(5000);
}
}

View File

@ -0,0 +1,16 @@
package org.baeldung.scheduling;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:springScheduled-config.xml")
public class SchedulingWithXmlConfigTest {
@Test
public void testXmlBasedScheduling() throws InterruptedException {
Thread.sleep(5000);
}
}