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 implementing distributed jobs, such as grid jobs.

Schedule jobs in your on-premises environment

  1. Create a Maven project named scx-example.
  2. In the following example, the dependencies of Spring Boot 2.1.4.RELEASE and Spring Cloud Finchley.SR1 are added 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
    • We recommend that you use Spring Boot 1.5.x and Spring Cloud Edgware rather than Spring Boot 1.x. The corresponding Spring Cloud Alibaba version 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.
  3. 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);
         }
     }               
  4. 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--------");
         }
     }                        
  5. 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;
         }
    
     }                        
  6. Create a scheduled job and add the required configuration.
    1. Log on to the EDAS console.In the test region, create a job group and record the group ID.
    2. In the job group that you create, create a job by specifying the following parameters:
      • Application ID: Select the ID of the group that you create in the test region.
      • Processor class name: The full name of the class that implements the job processing interface. In this example, the value is SimpleTask. This value is the same as the test job class in the application.
      • Task type: Set this parameter to Stand-alone operation.
      • cron expression: The default option is *0 * * * * ?. *0 * * * * ?* indicates that the job is executed once every minute.
      • Description: none.
      • Task parameters: none.
    3. In the src/main/resources path of the on-premises Maven project, create the application.properties file and add the following configuration to the file:
      server.port=18033
      # Specify the region (**regionName** of the test region is *cn-test*) and group ID (group-id) of the job.
      spring.cloud.alicloud.scx.group-id=***
      spring.cloud.alicloud.edas.namespace=cn-test                                
  7. 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 an application 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 code or configurations. For more information about deployment methods and steps, see Create and deploy applications (Kubernetes) and Overview.

After the application is 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 over the Internet. You can verify the deployment result in your on-premises environment or in the cloud. No permission limits are imposed. If you need to deploy applications to other regions, such as China (Hangzhou), you must perform the following steps when you create a scheduled job and schedule the job:

  1. Log on to the EDAS console.In the test region, create a job group and record the group ID.
  2. Go to the Security Management page and obtain the AccessKey ID and the AccessKey secret.
  3. Configure the scheduled job in the application.properties file.
    You can also configure the job types except Simple Job Single-instance Edition.
  4. In the application.properties file, add the AccessKey ID and the AccessKey secret of your Alibaba Cloud account.
    spring.cloud.alicloud.access-key=xxxxx
    spring.cloud.alicloud.secret-key=xxxxx                        

What to do next

After your application is deployed to EDAS, you can use the SchedulerX component to implement more job scheduling features.