Easily complete timed tasks with spring task

Source: Internet
Author: User

I. BACKGROUND

The need to use the release of inventory for scheduled tasks in recent projects summarizes how you can use Spring task to do a simple configuration to complete the requirement, this article describes the custom development of the Spring3.0 after the scheduled task tool,

Spring task, we can compare it to a lightweight quartz, easy to use, except spring-related packages do not require additional packages, and support annotations and configuration file two forms, I will describe the two ways.

Second, the timing task development steps

Development environment

Spring 4.2.6.RELEASE

Maven 3.3.9

JDK 1.7

Idea 15.04

"1". Profile-based

1. Writing plain Java class

 PackageCom.hafiz.www.cron;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;/*** Desc: The first Springtask-based scheduling task * Created by Hafiz.zhang on 2016/12/11.*/ Public classFirstcron {Private Static FinalLogger Logger = Loggerfactory.getlogger (Firstcron.class);  Public voidcron () {Logger.info ("Scheduled Tasks in progress ..."); //Do something else    }}

2. Add a namespace and description in the Spring configuration file header (bold below) and configure the timer task

1 <Beansxmlns= "Http://www.springframework.org/schema/beans"2 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"3  xmlns:task= "Http://www.springframework.org/schema/task" 4 xsi:schemalocation= "Http://www.springframework.org/schema/beans5 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd6  Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd ">7 8     <BeanID= "Firstcron"class= "Com.hafiz.www.cron.FirstCron"/>9     <Task:scheduled-tasks>Ten         <task:scheduledref= "Firstcron"Method= "Cron"Cron= "0/5 * * * *?"/> One     </Task:scheduled-tasks> A </Beans>

We set it to run every 5 seconds. For a cron expression on spring task, see another blog: @scheduled cron expressions that get rid of spring timed tasks

"2" based on annotations

We can use @scheduled annotations for development, first of all we look at the source code of the annotations

1  Packageorg.springframework.scheduling.annotation;2 3 Importjava.lang.annotation.Documented;4 ImportJava.lang.annotation.ElementType;5 Importjava.lang.annotation.Repeatable;6 Importjava.lang.annotation.Retention;7 ImportJava.lang.annotation.RetentionPolicy;8 ImportJava.lang.annotation.Target;9 ImportOrg.springframework.scheduling.annotation.Schedules;Ten  One @Target ({elementtype.method, elementtype.annotation_type}) A @Retention (retentionpolicy.runtime) - @Documented -@Repeatable (schedules.class) the  Public@InterfaceScheduled { -String Cron ()default""; -  -String Zone ()default""; +  -     LongFixeddelay ()default-1l; +  AString fixeddelaystring ()default""; at  -     LongFixedrate ()default-1l; -  -String fixedratestring ()default""; -  -     LongInitialDelay ()default-1l; in  -String initialdelaystring ()default""; to}

It can be seen that the note has five methods or parameters, respectively, that means:

Cron: Specifying cron expressions

Zone: Official document explanation: A time zone for which the cron expression would be resolved. Specify the time zone in which cron expressions run

Fixeddelay: Official documentation explains: An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds. That is, the interval from the start of the previous task to the start of the next task, in milliseconds.

Fixedrate: Official documentation explains: An interval-based trigger where the interval are measured from the start time of the previous task. The time unit value is measured in milliseconds. The interval from the start of the previous task to the start of the next task, in milliseconds.

InitialDelay: Official Document explanation: Number of milliseconds to delay before fixedRate() the first execution of a or fixedDelay() task. The delay before the task is called for a time, in milliseconds

1. Timed task class for writing annotations

1  PackageCom.hafiz.www.cron;2 3 ImportOrg.slf4j.Logger;4 Importorg.slf4j.LoggerFactory;5 Importorg.springframework.scheduling.annotation.Scheduled;6 7 /**8 * Desc: The first Springtask-based scheduling task9 * Created by Hafiz.zhang on 2016/12/11.Ten  */ One  Public classFirstcron { A     Private Static FinalLogger Logger = Loggerfactory.getlogger (Firstcron.class); -  -@Scheduled (cron = "0/5 * * * * *?")) the      Public voidcron () { -Logger.info ("Timed tasks in progress ....")); -         //Do something else -     } +}

2. Add a namespace and description to the Spring profile header (bold below) and turn on the timed task note driver

1 <Beansxmlns= "Http://www.springframework.org/schema/beans"2 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"3  xmlns:task= "Http://www.springframework.org/schema/task" 4 xsi:schemalocation= "Http://www.springframework.org/schema/beans5 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd6  Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd ">7 8     <BeanID= "Firstcron"class= "Com.hafiz.www.cron.FirstCron"/>9     <Task:schedulerID= "Qbscheduler"pool-size= "Ten"/>Ten     <Task:annotation-drivenScheduler= "Qbscheduler"Mode= "Proxy"/> One     <!--In short, we just need to <task:annotation-driven/> this sentence, these parameters are not required - A </Beans>

We have completed the development of the annotation-based timing task, is it very simple?

Operation Result:

  

Iii. Summary

In fact, some of the knowledge we seemingly difficult, but when we actually operate, found quite simple, as long as the problem we think more often, there will be a solution. There is also a spring quartz implementation for timed tasks, which we will introduce later. Welcome message exchange ....

Easily complete timed tasks with spring task

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.