Spring Schedule Timer Task (i): How to Annotate

Source: Internet
Author: User

Some of the common ways that I know about Java timed tasks:
1, Spring Schedule annotation way, 2, Spring schedule configuration file way; 3, Java class inheritanceTimerTask;
implementation of the first approach:
1. Use maven to create the spring project, schedule the package under Spring-context.jar, so you need to import the package associated with it, and I'm with the Spring Web project, The Spring-web and SPRING-WEBMVC packages are also imported, as follows:
<dependency>        <groupId>org.springframework</groupId>        <artifactid>spring-context </artifactId>        <version>4.1.7.RELEASE</version>    </dependency>    < dependency>        <groupId>org.springframework</groupId>        <artifactid>spring-web</ artifactid>        <version>4.1.6.RELEASE</version>    </dependency>    <dependency>        <groupId>org.springframework</groupId>        <artifactid>spring-webmvc</artifactid >        <version>4.1.6.RELEASE</version>    </dependency>

MAVEN package configuration only needs to be as follows, other related dependencies, MAVEN will resolve itself.
2. Configure the Spring project's base file Spring.xml:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns:task= "Http://www.springframework.org/schema/task"    xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"    xmlns:context= "Http://www.springframework.org/schema/context"    xsi:schemalocation= "http// www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp:// Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp ://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.1.xsd ">    <!--start a timed task--    <task:annotation-driven/>    <!--turn on annotations-    <context: Annotation-config/>    <!--Specify the relevant package path--    <context:component-scan base-package= "Scheduletest"/ ></beans>


3, write Java business code, you need to add the top of the class declaration@Component Annotations and add @Scheduled (cron = "0/5 * * * * *") on the method declaration that requires timed task execution Annotations and related parameters. Parameter use can refer to http://blog.csdn.net/isnotsuitable/article/details/7464556, in my example I mean every five seconds:
Package Scheduletest;import Java.text.simpledateformat;import Java.util.date;import Org.springframework.scheduling.annotation.scheduled;import org.springframework.stereotype.component;/** * Spring Timer 1 *  * @author tuzongxun123 * */@Componentpublic class Scheduletest {    @Scheduled (cron = "0/5 * * * * *?") Public    void SchTest1 () {        date date = new Date ();        SimpleDateFormat sim = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");        String datestr = Sim.format (date);        System.out.println ("This is Spring timer 1, executed once every five seconds, current time:" + datestr);}    }


4. Web project's basic configuration file, xml:
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "id=" webapp_id "version=" 3.0 ">  <display-name>appversion </display-name>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:spring.xml</param-value>  </context-param>  <listener >    <description>spring listeners </description>    <listener-class> org.springframework.web.context.contextloaderlistener</listener-class>  </listener>  < welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list> </web-app>


A spring context listener is used in the configuration above, in which case the scheduled task configuration in Spring.xml takes effect after the project is started. But this is not the only way, you can also use the following configuration, you can see the same effect after starting the project:
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "id=" webapp_id "version=" 3.0 "> <display-name>appversion</ display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value >classpath:spring.xml</param-value> </context-param> <servlet> <servlet-name> Dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</ servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-val Ue>classpath:spring.xml</param-value> </init-param> <load-on-startup>1</load-on-startup&  Gt </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-fi Le>index.jsp</welcome-file> </welcome-file-list></web-app>

This is it. Spring's listener is replaced by the MVC Scheduler, which runs the scheduled task configuration in Spring.xml when the scheduler is loaded, so you see the same effect when you start the project. If you put the listener above with the MVC Scheduler here, you will see that the scheduled task will execute two times during the same time after the startup project.

Spring Schedule Timer Task (i): How to Annotate

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.