The interface defines the auditPurgeJob() method for the audit record housekeeping task. Listing
15-18 shows the implementation class with the @Scheduled annotation applied.
Listing 15-18. The HousekeepingServiceImpl Class
package com.apress.prospring3.springblog.service.jpa;
import
org.springframework.beans.factory.annotation.Value;
import
org.springframework.scheduling.annotation.Scheduled;
import
org.springframework.stereotype.Repository;
import
org.springframework.stereotype.Service;
import
org.springframework.transaction.annotation.Transactional;
import com.apress.prospring3.springblog.service.HousekeepingService;
@Service("housekeepingService")
@Repository
@Transactional
public class HousekeepingServiceImpl implements HousekeepingService {
@Value("${audit.record.history.days}")
private int auditHistoryDays;
@Scheduled(cron="0 0 0 * * ?")
public void auditPurgeJob() {
// Purge audit record logic goes here
}
}
From Listing 15-18, the @Scheduled annotation was applied to the auditPurgeJob() method with a
cron expression. The cron expression means that the job should run at midnight every day. For the
number of days that the audit records will be kept, we will externalize it into a properties file for easy
maintenance. To enable the annotation-style task scheduling, we will define the <task:annotation-
driven> tag in the root WebApplicationContext.
For more details, please refer to Chapter 21.
Summary
In this chapter, we covered Spring's support for task scheduling. We focused on Spring's built-in
TaskScheduler abstraction and demonstrated how to use it to fulfill task scheduling needs with a sample
batch data update job. We also covered how Spring 3 supports annotation for executing tasks
asynchronously.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home