Added XML configurations to schedule tasks.
This commit is contained in:
parent
fbe55d0584
commit
68ff312941
|
@ -3,7 +3,7 @@ package org.baeldung.scheduling;
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component("scheduledAnnotationExample")
|
||||||
public class ScheduledAnnotationExample {
|
public class ScheduledAnnotationExample {
|
||||||
|
|
||||||
@Scheduled(fixedDelay = 1000)
|
@Scheduled(fixedDelay = 1000)
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,8 +9,8 @@ import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableScheduling
|
@EnableScheduling
|
||||||
@ComponentScan("com.baeldung.spring.integration")
|
@ComponentScan("org.baeldung.scheduling")
|
||||||
@PropertySource("classpath:springIntegration.properties")
|
@PropertySource("classpath:springScheduled.properties")
|
||||||
public class SpringSchedulingConfig {
|
public class SpringSchedulingConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
@ -1,20 +1,26 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
|
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">
|
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 -->
|
<!-- Configure the scheduler -->
|
||||||
<task:scheduler id="myScheduler" pool-size="10" />
|
<task:scheduler id="myScheduler" pool-size="10" />
|
||||||
|
|
||||||
<bean id="myscheduler" name="myscheduler"
|
<bean id="myscheduler"
|
||||||
class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
|
class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler" />
|
||||||
</bean>
|
|
||||||
|
|
||||||
<!-- Configure the fixedDealy, fixedRate or cron based schduled tasks -->
|
<!-- Configure the fixedDealy, fixedRate or cron based schduled tasks -->
|
||||||
|
<bean id="schedulingWithXmlConfig" class="org.baeldung.scheduling.SchedulingWithXmlConfig" />
|
||||||
|
|
||||||
<task:scheduled-tasks scheduler="myScheduler">
|
<task:scheduled-tasks scheduler="myScheduler">
|
||||||
<task:scheduled ref="beanA" method="methodA" fixed-delay="5000" initial-delay="1000" />
|
<task:scheduled ref="schedulingWithXmlConfig" method="scheduleFixedDelayTask" fixed-delay="${fixedDelay.in.milliseconds}" initial-delay="1000" />
|
||||||
<task:scheduled ref="beanB" method="methodB" fixed-rate="5000" />
|
<task:scheduled ref="schedulingWithXmlConfig" method="scheduleFixedRateTask" fixed-rate="${fixedRate.in.milliseconds}" />
|
||||||
<task:scheduled ref="beanC" method="methodC" cron="*/5 * * * * MON-FRI" />
|
<task:scheduled ref="schedulingWithXmlConfig" method="scheduleTaskUsingCronExpression" cron="${cron.expression}" />
|
||||||
</task:scheduled-tasks>
|
</task:scheduled-tasks>
|
||||||
</beans>
|
</beans>
|
|
@ -1,3 +1,3 @@
|
||||||
cron.expression=0 15 10 15 * ?
|
cron.expression=0 15 10 15 * ?
|
||||||
fixedRate.in.millisecons=1000
|
fixedRate.in.milliseconds=1000
|
||||||
fixedDelay.in.millisecons=1000
|
fixedDelay.in.milliseconds=1000
|
|
@ -1,6 +1,5 @@
|
||||||
package org.baeldung.scheduling;
|
package org.baeldung.scheduling;
|
||||||
|
|
||||||
import org.baeldung.scheduling.SpringSchedulingConfig;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
@ -13,6 +12,6 @@ public class ScheduledAnnotationExampleTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testScheduledAnnotation() throws InterruptedException {
|
public void testScheduledAnnotation() throws InterruptedException {
|
||||||
Thread.sleep(20000);
|
Thread.sleep(5000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue