Enterprise Distributed Application Service (EDAS) integrates SchedulerX into the console
as a component to implement distributed job scheduling. 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-Server Edition mode.
Why is SchedulerX used?
SchedulerX is a distributed job scheduling product developed by Alibaba, which is
accurate, highly reliable, and highly available. It allows you to run jobs on a scheduled
basis in seconds by using the Cron expression. It provides models for implementing
distributed jobs, such as grid jobs.
Schedule jobs locally
- Create a Maven project called
scx-example
.
- The following takes Spring Boot 2.1.4.RELEASE and Spring Cloud Finchley.SR1 as an
example. Add the following 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 want to use Spring Boot 1.x, use Spring Boot 1.5.x, Spring Cloud Edgware, and
Spring Cloud Alibaba 1.5.1.RELEASE.
- Spring Boot 1.x has expired, so we recommend that you use a later version of Spring
Boot to develop applications.
- Create the startup class
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
TestService
and inject the class to IoC of the test task through Spring. import org.springframework.stereotype.Service;
@Service
public class TestService {
public void test() {
System.out.println("---------IOC Success--------");
}
}
- Create a simple class
SimpleTask
as the test task 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;
}
}
- Create a scheduled job group and add required configurations.
- Log on to the EDAS console. Create a scheduled job group in the test region and record the group ID.
- In the job group that you created, configure the scheduled job as follows:
- Job Group: Select the ID of the group you created in the test region.
- Job Processing Interface: Enter the name of the class that implements the job interface. In this example,
the value is SimpleTask, which is the same as the test job class in the application.
- Type: Select Simple Job Single-Server Edition.
- Cron Expression:
*0 * * * * ? *
is selected by default. It means that the job is run once every minute.
- Job Description: None.
- Custom Parameters: None.
- In the src/main/resources path of the local Maven project, create the application.properties file and add the following configuration to the file:
server.port=18033
# Configures the region (the **regionName** of the test region is *cn-test*) and the group ID (group-id) of the job.
spring.cloud.alicloud.scx.group-id=***
spring.cloud.alicloud.edas.namespace=cn-test
- Run the main function in
ScxApplication
to start the service.
Verify the result
Log on to the IntelliJ IDEA console and view the standard output. The following test
information is printed periodically:
-----------Hello world---------------
---------IOC Success--------
Deploy applications to EDAS
Spring Cloud Alibaba Cloud SchedulerX is designed for migrating applications from
the development environment to EDAS. You can directly deploy applications to EDAS
without modifying any code or configurations. For more information about deployment
methods and procedures, see Overview of application deployment.
After the applications are deployed, you can schedule jobs in the EDAS console.
Procedure of using scheduled jobs in non-test regions
In this topic, the test region is used and the test is performed in a public network
environment. You can verify the deployment result both on on-premises and off-premises
instances, without permission restrictions. If you want to deploy applications to
other regions, for example, China (Hangzhou), you need to perform the following steps
in addition to creating a scheduled job and scheduling it:
- Log on to the EDAS console. Create a job group and a scheduled job in the China (Hangzhou) region.
- Go to the Security Management page and retrieve the AccessKey ID and AccessKey secret.
- In the
application.properties
file, configure the scheduled job.You can also configure other types for the job than Simple Job Single-Server Edition.
- In the
application.properties
file, add the AccessKey ID and AccessKey secret of your Alibaba Cloud account.spring.cloud.alicloud.access-key=xxxxx
spring.cloud.alicloud.secret-key=xxxxx
Subsequent operations
After your application is deployed to EDAS, you can use SchedulerX to implement more
job scheduling functions.