By Qianxi
A scheduled task is a task that is executed periodically at an agreed time or a fixed frequency. In enterprise applications, backend businesses that are not initiated by user behaviors are generally implemented through scheduled tasks. Common scenarios are as follows:
• Asynchronous Data Processing: For example, the order is stored first, and then the unpaid orders are scanned every minute for batch processing.
• Automated O&M: For example, the historical records of a database are cleared every hour.
• System Monitoring: For example, the monitoring metrics are scanned every minute. If they exceed the threshold, an alarm is triggered.
• Data Synchronization: For example, data in MySQL is synchronized to the big data platform at 1 a.m. every day.
In monolithic applications, it is simple to implement scheduled tasks, such as java.util.Timer and ScheduledExecutorService in Java. The Spring framework also provides scheduling to facilitate the development of scheduled tasks. However, in microservices, an application generally has many nodes. Therefore, a fatal problem will arise in the implementation method mentioned above, that is, repeated execution of tasks.
Spring Cloud Alibaba released the Scheduling module #3732, which provides a set of open-source, lightweight, and highly available scheduled task solutions to help you quickly develop distributed scheduled tasks in the microservice system.
As one of the important capabilities in application development, the scheduled task function provides support for scheduled tasks in the Spring Framework. In its most basic usage scenario, a scheduled task can be easily developed through @Scheduled annotation (Analysis Reference). Some distributed capabilities can also be realized by extending and integrating the Quartz framework. However, it is currently not easy to quickly define a task through the @Scheduled annotation of Spring. These capabilities provided by the framework are sufficient in monolithic application scenarios, but they are somewhat out of reach in microservice distributed cluster scenarios.
Therefore, in the Spring Cloud Alibaba system, we hope to provide a module capability that can quickly access and use distributed scheduled tasks in microservice scenarios. The following figure compares the features of distributed scheduled tasks.
In distributed microservice scenarios, in addition to the basic scheduled task running capabilities, capabilities such as anti-duplicate task execution, shard running, and parallelism control of task execution are also needed. On this basis, you can continue to explore the scheduled scheduling support for microservice applications. You are welcome to put forward requirements and suggestions in the community.
The new Spring Cloud Alibaba version provides the distributed task scheduling quick start module spring-cloud-starter-alibaba-schedulerx to support the integration of various distributed scheduled task solutions. For microservice applications with Spring Cloud Alibaba, access can be completed by adding the following pom dependency.
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-schedulerx</artifactId>
<version>{version}</version>
</dependency>
The spring.cloud.scheduling.distributed-mode parameters are provided in the application configuration file to allow users to choose which implementation scheme to use. Currently, two modes are provided based on "Alibaba Cloud SchedulerX" and "Open-source Shedlock". In the future, other solutions will continue to be provided based on the needs of community users. For more information, please refer to Example Project.
The easiest way to access Spring distributed scheduled tasks is to directly access SchedulerX, a distributed task scheduling platform managed by Alibaba Cloud. SchedulerX is not only fully compatible with the @Scheduled annotation of Spring, but also features high availability, high security, high performance, no O&M operations, and low cost.
SchedulerX provides a task management platform that allows you to dynamically create, modify, maintain, and manage scheduled tasks. SchedulerX also provides enterprise-level observability solutions such as alarm monitoring, history recording, log services, and tracing analysis. It also provides enhanced capabilities such as shard running of tasks to ensure the stable operation of online scheduled tasks.
You can use the @SchedulerLock annotation to implement distributed scheduled tasks without using cloud services.
The @SchedulerLock annotation is a distributed lock framework. Combined with the @Scheduled annotation, it can ensure that tasks are executed only once on multiple nodes at the same time. The framework supports the implementation of multiple distributed locks, such as JDBC and ZooKeeper. The current integration module only provides the JDBC solution. In the future, other modes will be added to meet the needs of the existing scenario architecture. The principle is as follows:
The following example uses the MySQL distributed lock to demonstrate the access process of distributed tasks. First, you need to add spring-cloud-starter-alibaba-schedulerx and corresponding configuration parameters as described at the beginning of this section. In addition, you need to complete the following configurations:
1. Since JDBC is used as the implementation of distributed lock, the following pom dependency is also required:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
2. Add JDBC configuration to the application.properties file (if it has already been added, this step can be ignored).
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/testdb?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class=com.mysql.cj.jdbc.Driver
After you complete the preceding configuration and start the application, a shedlock table is created in the corresponding database for synchronous execution of scheduled tasks.
Sample application code and task reference are as follows:
Configure the startup class to enable SchedulingTasks:
@SpringBootApplication
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "3m")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Create a scheduled task:
import org.joda.time.DateTime;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
@Component
public class SpringJob {
/**
* Run every 5 minutes.
*/
@Scheduled(cron = "0 */5 * * * ?")
@SchedulerLock(name = "SpringJob.job1", lockAtMostFor = "2m", lockAtLeastFor = "1m")
public void job1() {
System.out.println("time=" + DateTime.now().toString("YYYY-MM-dd HH:mm:ss") + " do job1...");
}
/**
* Run every 5 seconds.
*/
@Scheduled(fixedRate = 5000)
@SchedulerLock(name = "SpringJob.job2", lockAtMostFor = "4s", lockAtLeastFor = "4s")
public void job2() {
System.out.println("time=" + DateTime.now().toString("YYYY-MM-dd HH:mm:ss") + " do job2...");
}
/**
* Run again 5 seconds after the last run.
* @throws InterruptedException
*/
@Scheduled(fixedDelay = 5000)
@SchedulerLock(name = "SpringJob.job3", lockAtMostFor = "4s", lockAtLeastFor = "4s")
public void job3() throws InterruptedException {
System.out.println("time=" + DateTime.now().toString("YYYY-MM-dd HH:mm:ss") + " do job3...");
Thread.sleep(10000);
}
}
Spring Cloud Alibaba distributed scheduled scheduling module provides a fast integration access solution for microservice application scenarios, and can provide fast integration for more distributed scheduled task implementation solutions in the future. For example, continue to integrate open-source components such as quartz/xxljob, and allow the above open-source components to also adapt to the @Scheduled annotation of Spring, so that one task code can be connected to different scheduling services. It also provides extended support for the business capabilities required in distributed task scenarios.
Welcome to the open-source community to put forward the business function demands and suggestions on distributed scheduled tasks.
Non-intrusive Instrumentation Technology for Golang Applications in OpenTelemetry
507 posts | 48 followers
FollowAlibaba Cloud Native Community - July 26, 2022
Alibaba Cloud Serverless - November 10, 2022
Alibaba Cloud Native Community - April 24, 2023
Aliware - October 20, 2020
Alibaba Cloud Native - March 28, 2024
Alibaba Clouder - December 1, 2020
507 posts | 48 followers
FollowAlibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.
Learn MoreMulti-source metrics are aggregated to monitor the status of your business and services in real time.
Learn MoreMSE provides a fully managed registration and configuration center, and gateway and microservices governance capabilities.
Learn MoreAccelerate and secure the development, deployment, and management of containerized applications cost-effectively.
Learn MoreMore Posts by Alibaba Cloud Native Community