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