The class is simple; just bootstrap the ApplicationContext and then keep looping. If the application
is deployed to an application server environment, the scheduler will keep running.
Running the program will produce the following batch job output every ten seconds:
INFO [com.apress.prospring3.ch15.service.jpa.CarServiceImpl] - <>
INFO [com.apress.prospring3.ch15.service.jpa.CarServiceImpl] - <Car age update job started>
INFO [com.apress.prospring3.ch15.service.jpa.CarServiceImpl] - <Car age update--- License:
LICENSE-1001 ­ Manufacturer: Ford - Manufacture Date: 1980-07-30T00:00:00.000+08:00 - Age: 31>
INFO [com.apress.prospring3.ch15.service.jpa.CarServiceImpl] - <Car age update--- License:
LICENSE-1002 ­ Manufacturer: Toyota - Manufacture Date: 1992-12-30T00:00:00.000+08:00 - Age:
18>
INFO [com.apress.prospring3.ch15.service.jpa.CarServiceImpl] - <Car age update--- License:
LICENSE-1003 ­ Manufacturer: BMW - Manufacture Date: 2003-01-06T00:00:00.000+08:00 - Age: 8>
INFO [com.apress.prospring3.ch15.service.jpa.CarServiceImpl] - <Car age update job completed
successfully>
INFO [com.apress.prospring3.ch15.service.jpa.CarServiceImpl] - <>
From the output, you can see the cars' age attributes were updated.
Besides a fixed interval, a more flexible scheduling mechanism is to use a cron expression. In Listing
15-8, change the line from this:
<task:scheduled ref="carService" method="updateCarAgeJob" fixed-delay="10000"/>
to the following:
<task:scheduled ref="carService" method="updateCarAgeJob" cron="0 * * * * *"/>
After the change, run the ScheduleTaskSample class again, and you will see the job will run every
minute. The Quartz's CronTrigger tutorial page (e.g., www.quartz-scheduler.org/documentation/quartz-
2.1.x/tutorials/crontrigger) provides a detailed description of the structure and gives examples of
cron expressions.
Task Scheduling Using Annotation
Another option for scheduling tasks using Spring's TaskScheduler abstraction is to use an annotation.
Spring provides the @Scheduled annotation for this purpose.
To enable annotation support for task scheduling, we need to provide the <task:annotation-
driven> tag in Spring's XML configuration. Listing 15-10 shows the configuration (task-annotation-app-
context.xml).
Listing 15-10. Spring Configuration for Annotation-Based Scheduling
<?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"/>
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home