Define simple Scheduled tasks using @scheduled annotations in spring projects

Source: Internet
Author: User

As title shows, sometimes we need to configure simple scheduled tasks in a Web project, and because the tasks are not complex and do not want to use a scheduled scheduling framework (Ps:quartz, ActiveMQ, Kafka, etc.), consider using @scheduled annotations to define simple scheduled tasks. Its full configuration is as follows:

(1) Add the timing task related configuration in spring's configuration file:

The header file for the XML configuration is added:

xmlns:task= "Http://www.springframework.org/schema/task"

And adding in Xsi:schemalocation:

Http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-4.0.xsd

Last added:

<context:component-scan base-package= "Cn.zifangsky.task"/><task:executor id= "executor" pool-size= "5"/ ><task:scheduler id= "Scheduler" pool-size= "ten"/><task:annotation-driven executor= "executor" scheduler= "Scheduler"/>

In this case, the package, the "Cn.zifangsky.task", where spring Auto-scan timed tasks are defined first. It then defines two thread pools and the scanning mechanism for enabling timed tasks.

(2) Add Test task:

package cn.zifangsky.task;import java.text.format;import  java.text.simpledateformat;import java.util.date;import  org.springframework.scheduling.annotation.scheduled;import org.springframework.stereotype.component;@ componentpublic class simplespringtask {/** *  resumes execution of the  */@Scheduled after each task has finished executing ( fixeddelay=2000) Public void say () {date current = new date (); Format format = new simpledateformat ("Yyyy-mm-dd hh:mm:ss"); System.out.println ("--------"  + format.format (current)  +  "---------");} /** * 0 seconds to print  */@Scheduled (cron= "0&NBSP;*&NBSP;*&NBSP;*&NBSP;*&NBSP;?") Public void print () {System.out.println ("Current is the whole score!!! ");}}

, such as:

/** * Executes this method every two seconds */@Scheduled (fixedrate=2000) public void Say () {Date current = new Date (); Format format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); System.out.println ("--------" + format.format (current) + "---------");}

Of course, the second type of task above is similar to the crontab scheduled task under Linux, with several parameter bits representing: seconds, minutes, hours, days (days in the month), month, and week

Note: If you want to learn more about using the crontab command in Linux, you can refer to this article: https://www.zifangsky.cn/591.html

(3) Test:

After running this project, the output from the final console is as follows:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/8A/D0/wKioL1g86fDxr5SeAAByRzmqIeQ842.png "title=" 20161106190901874.png "alt=" Wkiol1g86fdxr5seaabyrzmqieq842.png "/>

PS: The above image of the watermark is my personal blog domain name, so also please administrator mercy don't give me marked as "Reprint article", Thank you!!!

This article is from "Zifangsky's personal blog" blog, make sure to keep this source http://983836259.blog.51cto.com/7311475/1877598

Define simple Scheduled tasks using @scheduled annotations in spring projects

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.