Integrate TimerTask in spring to execute scheduled tasks

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.