The core of Spring's support for Timer is composed of ScheduledTimerTask and TimerFactoryBean classes. The ScheduledTimerTask class implements the TimerTask package, so that you can define trigger information for this task. By using the TimerFactoryBean class, you can let Spring use the configuration to create a trigger and automatically create a Timer instance for a group of specified ScheduledTimerTask beans.
1. Customize timerTask, for example, to regularly output information in ServletContext. In this example, the absolute path of the output project (for example, D: \ software \ apache-tomcat-6.0.33 \ webapps \ spring \)
Java code
Public class BirthdayReminderTimerTask extends TimerTask implements ServletContextAware {
/* Implement ServletContextAware to obtain servletContext */
Private ServletContext servletContext;
Private static Logger logger = Logger. getLogger (BirthdayReminderTimerTask. class );
@ Override
Public void run (){
// Logger. debug ("BirthdayReminderTimerTask is running ");
SetServletContext (servletContext );
Try {
System. out. println (this. servletContext. getRealPath ("/"));
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public void setServletContext (ServletContext servletContext ){
This. servletContext = servletContext;
}
}
Public class BirthdayReminderTimerTask extends TimerTask implements ServletContextAware {
/* Implement ServletContextAware to obtain servletContext */
Private ServletContext servletContext;
Private static Logger logger = Logger. getLogger (BirthdayReminderTimerTask. class );
@ Override
Public void run (){
// Logger. debug ("BirthdayReminderTimerTask is running ");
SetServletContext (servletContext );
Try {
System. out. println (this. servletContext. getRealPath ("/"));
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public void setServletContext (ServletContext servletContext ){
This. servletContext = servletContext;
}
}
2. Configure in the bean configuration file of spring. When the system loads the file, the configured timer is automatically started and managed by spring.
Xml Code
<! -- Custom task -->
<Bean id = "birthdayReminder" class = "com. jep. task. BirthdayReminderTimerTask"> </bean>
<! -- ScheduledTimerTask class is the implementation of the TimerTask package, so that you can define trigger information for this task. -->
<Bean id = "birthdayReminderTimerTask"
Class = "org. springframework. scheduling. timer. ScheduledTimerTask">
<! -- Set the startup delay -->
<Property name = "delay">
<Value> 3000 </value>
</Property>
<! -- Subsequent delay -->
<Property name = "period">
<Value> 5000 </value>
</Property>
<! -- Specify trigger information -->
<Property name = "timerTask">
<Ref local = "birthdayReminder"/>
</Property>
</Bean>
<! -- Using the TimerFactoryBean class, you can let Spring use the configuration to create a trigger and automatically create a Timer instance for a group of specified ScheduledTimerTask beans. -->
<Bean id = "timerFactory"
Class = "org. springframework. scheduling. timer. TimerFactoryBean">
<Property name = "scheduledTimerTasks">
<List>
<Ref local = "birthdayReminderTimerTask"/>
</List>
</Property>
</Bean>
<! -- Custom task -->
<Bean id = "birthdayReminder" class = "com. jep. task. BirthdayReminderTimerTask"> </bean>
<! -- ScheduledTimerTask class is the implementation of the TimerTask package, so that you can define trigger information for this task. -->
<Bean id = "birthdayReminderTimerTask"
Class = "org. springframework. scheduling. timer. ScheduledTimerTask">
<! -- Set the startup delay -->
<Property name = "delay">
<Value> 3000 </value>
</Property>
<! -- Subsequent delay -->
<Property name = "period">
<Value> 5000 </value>
</Property>
<! -- Specify trigger information -->
<Property name = "timerTask">
<Ref local = "birthdayReminder"/>
</Property>
</Bean>
<! -- Using the TimerFactoryBean class, you can let Spring use the configuration to create a trigger and automatically create a Timer instance for a group of specified ScheduledTimerTask beans. -->
<Bean id = "timerFactory"
Class = "org. springframework. scheduling. timer. TimerFactoryBean">
<Property name = "scheduledTimerTasks">
<List>
<Ref local = "birthdayReminderTimerTask"/>
</List>
</Property>
</Bean>
3. for web projects, you must configure the following in web. xml:
Xml Code
<! -- Path of Spring ApplicationContext configuration file this parameter is used for Spring-Context loader later -->
<Context-param>
<Param-name> contextConfigLocation </param-name>
<Param-value> classpath: beans. xml </param-value>
</Context-param>
<! -- Load Spring ApplicationContext -->
<Listener>
<Listener-class> org. springframework. web. context. ContextLoaderListener </listener-class>
</Listener>