Enterprise Distributed Application Service (EDAS) integrates SchedulerX into the console
as a component to schedule jobs. This topic describes how to use SchedulerX to schedule
jobs in Spring Cloud applications, deploy the applications to EDAS, and implement
job scheduling in Simple Job Single-instance Edition mode.
Why is SchedulerX used?
SchedulerX is a distributed job scheduling service that is developed by Alibaba. It
provides a second-level scheduling service that is accurate, highly reliable, and
highly available based on CRON expressions. It also provides models for running distributed
jobs, such as grid jobs.
Schedule jobs in your on-premises environment
- Create a Maven project named
scx-example
.
- In the following sample code, Spring Boot 2.1.4.RELEASEand Spring Cloud Finchley.SR1
are added as dependencies to the pom.xml file.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alicloud-schedulerx</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Note
- If you must use Spring Boot 1.x, we recommend that you use Spring Boot 1.5.x and Spring
Cloud Edgware. The corresponding version of Spring Cloud Alibaba is 1.5.1.RELEASE.
- Spring Boot 1.x has reached end of life. Therefore, we recommend that you use a later
version of Spring Boot to develop your applications.
- Create a startup class that is named
ScxApplication
for scx-example
. import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ScxApplication {
public static void main(String[] args) {
SpringApplication.run(ScxApplication.class, args);
}
}
- Create a simple class that is named
TestService
and use Spring to inject the class to the Inversion of Control (IoC) test job. import org.springframework.stereotype.Service;
@Service
public class TestService {
public void test() {
System.out.println("---------IOC Success--------");
}
}
- Create a simple class that is named
SimpleTask
as the test job class and inject TestService
to the class. import com.alibaba.edas.schedulerx.ProcessResult;
import com.alibaba.edas.schedulerx.ScxSimpleJobContext;
import com.alibaba.edas.schedulerx.ScxSimpleJobProcessor;
import org.springframework.beans.factory.annotation.Autowired;
public class SimpleTask implements ScxSimpleJobProcessor {
@Autowired
private TestService testService;
@Override
public ProcessResult process(ScxSimpleJobContext context) {
System.out.println("-----------Hello world---------------");
testService.test();
ProcessResult processResult = new ProcessResult(true);
return processResult;
}
}
- Run the main function in
ScxApplication
to start the service.
Verify the result
View the standard output in the IntelliJ IDEA console. You can find that the following
test information is periodically displayed:
-----------Hello world---------------
---------IOC Success--------
Deploy the application to EDAS
Spring Cloud AliCloud SchedulerX is developed to respond to the needs of migrating
applications from their original development environments to EDAS. You can deploy
applications to EDAS without the need to modify the code or configurations. For more
information about deployment methods and steps, see Overview and Overview.
After the application is deployed, you can schedule jobs in the EDAS console.
What to do next
After your application is deployed to EDAS, you can use the SchedulerX component to
implement more job scheduling features.