Using task-namespace for task scheduling is very simple. Listing 15-8 shows the configuration file
(task-namespace-app-context.xml).
Listing 15-8. Spring Configuration Using task-namespace
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=http://www.springframework.org/schema/beans
xmlns:task="http://www.springframework.org/schema/task"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd"
>
<import resource="car-job-app-context.xml"/>
<task:scheduler id="myScheduler" pool-size="10"/>
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="carService" method="updateCarAgeJob"
fixed-delay="10000"/>
</task:scheduled-tasks>
</beans>
As shown in Listing 15-8, the context for the car application was imported. When it encounters the
<task:scheduler> tag, Spring will instantiate an instance of the ThreadPoolTaskScheduler class, while the
attribute pool-size specifies the size of the thread pool that the scheduler can use. Within the
<task:scheduled-tasks> tag, one or more tasks can be scheduled. In the <task:scheduled> tag, a task can
reference a Spring bean (the carService bean in this case) and a specific method within the bean (in this
case the updateCarAgeJob() method). The attribute fixed-delay will instruct Spring to instantiate a
PeriodicTrigger as the Trigger implementation for the TaskScheduler.
Listing 15-9 shows the testing program for task scheduling.
Listing 15-9. Testing Task Scheduling in Spring
package com.apress.prospring3.ch15.schedule;
import org.springframework.context.support.GenericXmlApplicationContext;
public class ScheduleTaskSample {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:task-namespace-app-context.xml");
ctx.refresh();
while (true) {
}
}
}
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home