Use annotations @Scheduled to perform timed tasks in spring

Source: Internet
Author: User

  

Note @scheduled can be added as a trigger source to a method, for example, the following method will be executed at a fixed delay time of 5 seconds, which is the completion time of one of the above calling tasks, and after the previous task completes, 5s executes again:

1 @Scheduled (fixeddelay=5000)2publicvoid  dosomething () { 3     // something that should execute periodically 4 }

If you need to perform at a fixed rate, as long as you change the name of the property specified in the annotation to Fixedrate, the following method invokes the execution at a fixed rate of 5s, which is the baseline of the above task start time, and is called again after the previous task begins execution 5s:

1 @Scheduled (fixedrate=5000)2publicvoid  dosomething () {  3     //  something that should execute periodically4 }

For fixed-delay and fixed-rate tasks, you can specify an initial delay that represents the number of milliseconds that the method waits before the first call is executed:

1 @Scheduled (initialdelay=1000, fixedrate=5000)2publicvoid  DoSomething () {3     //  something that should execute periodically4 }

If a simple recurring schedule cannot be met, then the cron expression provides the possibility. For example, the following method will only be performed on weekdays:

1 @Scheduled (cron= "*/5 * * * * * MON-FRI")2publicvoid  dosomething () {  3     //  something that should execute on weekdaysonly4 }

You can also specify the time zone that a cron expression is called by using the Zone property.

Attention:

1, spring annotation @scheduled need to write on the implementation method;

2, the task method of the timer can not have a return value (if there is a return value, spring initialization will tell you that there is an error, you need to set a Proxytargetclass value of true), can not point to any parameters;

3. If the method needs to interact with other objects in the application context, it is usually done through dependency injection;

4, the realization class to have the component annotation @component.

Reference Document:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/# Scheduling-annotation-support-scheduled

Use annotations @Scheduled to perform timed tasks in spring

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.