Spring MVC uses schedule to implement timed tasks

Source: Internet
Author: User

Schedule exists in the Spring-context.jar package.

To implement a simple step:

1. Configure Bean.xml to turn on timed task support.

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:task= "Http://www.springframework.org/schema/task"xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans                              /spring-beans.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context.xsd HTTP://WWW.SPRINGFRAMEWORK.O                              Rg/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/s Pring-task.xsd ">    <!--turn on timed tasks--<task:annotation-driven/>    <Context:component-scanBase-package= "Com.jsoft.testspring" />    <Context:annotation-config/>    <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "prefix">            <value>/web-inf/jsp/</value>        </ Property>        < Propertyname= "suffix">            <value>. jsp</value>        </ Property>    </Bean>    </Beans>

Code implementation:

 PackageCom.jsoft.testspring.testmvchelloworld;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importorg.springframework.scheduling.annotation.Scheduled;Importorg.springframework.stereotype.Component; @Component    Public classscheduletest { @Scheduled (cron  = "0/5 * * * *?" )       Public voidSchTest1 () {Date Date=NewDate (); SimpleDateFormat Sim=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String Datestr=Sim.format (date); System.out.println ("This is spring timer 1, executed every five seconds, current time:" +datestr); }  }  

Note that annotations such as @component are added.

Example Project: Https://github.com/easonjim/5_java_example/tree/master/springtest/test24/testmvchelloworld

Reference:

http://blog.csdn.net/tuzongxun/article/details/51576301

Spring MVC uses schedule to implement timed tasks

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.