Spring scheduled implements timed tasks

Source: Internet
Author: User

1 introducing namespaces in the. XML configuration file

xmlns:task="http://www.springframework.org/schema/task"http://www.springframework.orghttp://www.springframework.org/schema/task/spring-task-3.0.xsd"

2 Setting up auto-discovery

<task

3 Add the @scheduled annotation to the method that requires timing execution, an example is as follows:

@Service Public  class areacacheservice implements Initializingbean {Ehcache Areacache;@Override     Public void Afterpropertiesset()throwsException {refresh (); }@Scheduled(Fixeddelay = +, InitialDelay = +) Public void Refresh() {SimpleDateFormat SDF =NewSimpleDateFormat ("YyyyMMdd HH:mm:ss"); System.out.println (Sdf.format (NewDate ()) +": Refresh Cache ..."); }}

The above example allows the method to continue executing after each execution, with the following results:

20160820 17:54:47:refresh Cache ...
20160820 17:54:48:refresh Cache ...
20160820 17:54:49:refresh Cache ...
20160820 17:54:50:refresh Cache ...
20160820 17:54:51:refresh Cache ...
20160820 17:54:52:refresh Cache ...

Attention:

Exactly one of the ' cron ', ' Fixeddelay (String) ', or ' fixedrate (string) ' attributes is required
(Cron, Fixddelay, fixedrate Three properties must be set one)

Attribute meaning:

Properties explain
Cron A cron-like expression, extending the usual un*x definition to include triggers on the second as well as minute, Hour, day of month, month and day of week.
Fixeddelay Execute The annotated method with a fixed period in milliseconds between the end of the last invocation and the start of the next.
Fixeddelaystring Execute The annotated method with a fixed period in milliseconds between the end of the last invocation and the start of T He next.
Fixedrate Execute The annotated method with a fixed period in milliseconds between invocations.
Fixedratestring Execute The annotated method with a fixed period in milliseconds between invocations.
InitialDelay Number of milliseconds to delay before the first execution of a fixedrate () or Fixeddelay () task.
Initialdelaystring Number of milliseconds to delay before the first execution of a fixedrate () or Fixeddelay () task.
Zone A time zone for which the cron expression would be resolved.

You can also configure a scheduled task with a configuration file:

<task:scheduled-tasks>           <task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>   </task:scheduled-tasks>  

The taskJob corresponding JavaBean is the job1 method to be executed on a timed basis.

Spring scheduled implements 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.