This chapter will demonstrate the use of @Scheduled Annotate to implement distributed scheduled tasks.
@The scheduled annotation is the default implementation of a scheduled task scheduling framework under the spring context, which can implement the scheduled scheduling of a certain method and supports Cron, fixedDelay, and fixedRate expressions.
Access to cloud version
The simplest way to connect to Spring’s distributed scheduled tasks is to directly connect to the Alibaba Cloud hosted Distributed Task Scheduling Platform SchedularX Not only is it fully compatible with Spring @Schedule annotations, but it also features high availability, high security, high performance, maintenance free, and low cost.
The task management platform of the Task Scheduling Platform SchedularX can help you dynamically add, modify, and operate scheduled tasks, as well as enterprise level observable solutions such as alarm monitoring, historical recording, log services, and link tracking, ensuring the stable operation of your scheduled tasks.
Reference access document access, each account has 5 free task quotas.
Local access method
Without relying on cloud products, local access methods can be combined with @ Scheduling Lock Annotate to implement distributed scheduled tasks.
@The Scheduling Lock annotation is a framework for distributed locks, combined with the @ Scheduling annotation, to ensure that tasks are executed only once on multiple nodes at the same time. This framework supports the implementation of various distributed locks, such as Jdbc, Zookeeper, Redis, etc. The principle is as follows
Access steps
Taking MySQL distributed lock as an example, demonstrate the access process of distributed tasks
Create tables in MySQL
Introduce the dependency of shedlock in the pom.xml file
Add the jdbc configuration in the application.properties file (ignore this step if it has already been added)
Configure LockProvider
Configure the startup class and enable Scheduling Tasks