Spring scheduled task @ schedule
Now that the project is complete, you want to add scheduled tasks to the project. For example, the accesstoken is obtained every S. The specific modification is as follows:
1. Add the task declaration to the spring configuration file.
xmlns:task="http://www.springframework.org/schema/task" http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
2. Configure "Asynchronous and scheduled" Annotations
3. Add a class and use the annotation component to annotate schedule in the method. The Code is as follows:
Package com. ciji. zzaclient. schedule; import org. springframework. beans. factory. annotation. autowired; import org. springframework. scheduling. annotation. scheduled; import org. springframework. stereotype. component; import com. ciji. zzaservice. service. registerService; @ Componentpublic class ScheduleSpringTest {@ Autowired RegisterService registerService; @ Scheduled (cron = "0/3 ****? ") Public void updateWeixinAccessToken () {System. out. println (" I will execute it regularly... "+ RegisterService. selectByPk (44 ));}}
In this way, the timer code is executed every three seconds in the project. Specific timing rules can be configured according to specific business.
The preceding scheduled task can only be executed once every three seconds. If you want to execute the task every three seconds. The time expression for configuring schedule is as follows:
@ Scheduled (fixedRate = 1000*3) public void updateweixinaccesen en () {System. out. println ("I am executing at a Scheduled time... ");}