spring comes with lightweight regular tasks

Quartz has been used previously to do regular tasks, recent development framework to switch spring mvc, had also intended to use Quartz to do regular tasks on the Internet to find information, we found that spring comes with regular tasks, support in the form of notes, very convenient.


Used in the form of spring annotation timed tasks without the XML configuration, just in the conventional JAVA classes, a method for task timing annotations can be added before @Scheduled, for example:

    @Scheduled (the cron = "0 8-23 * 0/2 *?")   
    Public void autoJob () throws Exception {
        // 8:00 -23 points between every two minute timer trigger
    }

Which cron expression and Quartz rules similar to other parameters @ Scheduled specifically refer to the official website of the document.


For the timer task effectively, the content needs to be introduced in the applicationContext.xml related task, for example as follows:

<beans xmlns="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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
       http://www.springframework.org/schema/aop
       HTTP: / /www.springframework.org/schema/aop/spring-aop-3.2.xsd
       http://www.springframework.org/schema/task
       http://www.springframework.org/schema/task/spring-task-3.2 .xsd "
    default-the lazy-the init =" to false ">
   ! <- other content omitted -> 
   <Task: Annotation-Driven Scheduler =" qbScheduler "MODE =" Proxy "/>  
   <Task: Scheduler ID =" qbScheduler "the pool-size =" 10 "/>
   <-! omitted else -> 

</beans>


So far all over, you can test timed task.

Reproduced in: https: //my.oschina.net/cjkall/blog/195912

Guess you like

Origin blog.csdn.net/weixin_34197488/article/details/91756516