http://spring-java4dev.com/spring-framework/spring-scheduler
How to work with
Spring Scheduler
Spring Framework provides a mechanism for scheduling purpose: Spring Scheduler. The tutorial will introduce about how to schedule tasks with Spring.
I.
Technology for Spring Scheduler tutorial
–
Java 1.8
–
Maven: 3.3.9
– Editor: Spring
Tool Suite –
Version 3.7.3.RELEASE
– Spring
Boot: Version: 3.
8.0.RELEASE
II. Overview
1.
Structure of project
2.
Step to Do
–
Create Spring Boot project
– Create Create Scheduler Task
– Enable Spring Scheduler
– Run &
Check result
3.
Demo Video
III. Practices
1. Create Spring Boot project
Open Spring Tool Suite, on main menu,
Choose File-New-Spring
Starter Project.
Input needed information about project as below images
Press Next button then press Finish. Spring Boot project is created.
2. Create Create Scheduler Task
Create a simple component class with 3 scheduled tasks defined by 3 methods:
– @Scheduled(fixedRate =
2000)
public void timeReport(): the method will be invoked each 2 seconds
– @Scheduled(initialDelay=
1000, fixedRate = 2000)
public void initDelayTimeReport(): the method will be
delay 1 second & will be invoked each 2 seconds after the first time
– @Scheduled(cron = “*/5 * * * * *”)
public void cronTimeReport(): using cron-expression for define a scheduled task that will be invoked for each 5 seconds.
– @Scheduled: An annotation that marks a method to be scheduled.
Exactly one of the cron(), fixedDelay(), or fixedRate() attributes must be specified.
3. Enable Spring Scheduler
Using @EnableScheduling in main Spring class: Enables Spring’s scheduled task execution capability, similar to functionality found in Spring’s
XML namespace.
4. Run & Check result
– Compile sourcecode:
clean install
– Run project with Spring mode: Spring Boot App.
– Log:
[TimeReport]
Now: 16:01:13
2016-08-21 16:01:13.938
INFO 5292 --- [ main] c.s.s.SpringSchedulerApplication : Started SpringSchedulerApplication in 4.827 seconds (
JVM running for 7.839)
[InitDelayTimeReport] Now: 16:01:14
[CronTimeReport] Now: 16:01:15
[TimeReport] Now: 16:01:15
[InitDelayTimeReport] Now: 16:01:16
[TimeReport] Now: 16:01:17
[InitDelayTimeReport] Now: 16:01:18
[TimeReport] Now: 16:01:19
[CronTimeReport] Now: 16:01:20
[InitDelayTimeReport] Now: 16:01:20
[TimeReport] Now: 16:01:21
[InitDelayTimeReport] Now: 16:01:22
[TimeReport] Now: 16:01:23
[InitDelayTimeReport] Now: 16:01:24
[CronTimeReport] Now: 16:01:25
[TimeReport] Now: 16:01:25
[InitDelayTimeReport] Now: 16:01:26
IV.
Source code
Spring Scheduler
=============================
Related Keyword================================
#spring scheduler example #springschedulerexample
#spring scheduler vs quartz #springschedulervsquartz
#spring scheduler clustered environment #springschedulerclusteredenvironment
#spring scheduler tutorial #springschedulertutorial
#spring boot scheduler #springbootscheduler
#spring scheduler cron expression #springschedulercronexpression
#threadpooltaskscheduler #threadpooltaskscheduler
#spring scheduler cron syntax #springschedulercronsyntax
#how to enable task scheduler in windows 7 #howtoenabletaskschedulerinwindows7
#scheduled task enable history #scheduledtaskenablehistory
#powershell enable scheduled task #powershellenablescheduledtask
#how to schedule a exe in task scheduler #howtoscheduleaexeintaskscheduler
#schedule vbscript task scheduler #schedulevbscripttaskscheduler
#schedule reboot task scheduler #schedulereboottaskscheduler
#spring task scheduler example #springtaskschedulerexample
#spring boot task scheduler #springboottaskscheduler
- published: 27 Aug 2016
- views: 10