All Products
Search
Document Center

Enterprise Distributed Application Service:Implement job scheduling

Last Updated:Apr 01, 2024

SchedulerX is a core component of Alibaba Cloud Enterprise Distributed Application Service (EDAS) to implement efficient job scheduling. This topic describes how to use SchedulerX to schedule jobs of the Simple Job Single-instance Edition type for Spring Cloud applications and deploy the applications to EDAS.

Why SchedulerX?

SchedulerX is a distributed job scheduling platform that is developed by Alibaba Cloud. SchedulerX provides sub-minute, accurate, highly reliable, and highly available scheduling service and supports cron expressions. SchedulerX also provides distributed job execution models such as MapReduce.

Schedule jobs in your on-premises environment

  1. Create a Maven project named scx-example.

  2. Add the dependencies of Spring Boot 2.1.4.RELEASE and Spring Cloud Finchley.SR1 to the pom.xml file, as shown in the following sample code:

     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.1.4.RELEASE</version>
         <relativePath/>
     </parent>
    
      <dependencies>
        <dependency>
          <groupId>com.aliyun.schedulerx</groupId>
          <artifactId>schedulerx2-spring-boot-starter</artifactId>
          <version>${schedulerx2.version}</version>
          <!--If you use logback, exclude log4j and log4j2. -->
          <exclusions>
            <exclusion>
              <groupId>org.apache.logging.log4j</groupId>
              <artifactId>log4j-api</artifactId>
            </exclusion>
            <exclusion>
              <groupId>org.apache.logging.log4j</groupId>
              <artifactId>log4j-core</artifactId>
            </exclusion>
            <exclusion>
              <groupId>log4j</groupId>
              <artifactId>log4j</artifactId>
             </exclusion>
          </exclusions>
        </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 the end of life. Therefore, we recommend that you use a later version of Spring Boot to develop your applications.

  3. Create a startup class 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 named SimpleTask as the test job class and inject TestService to the class.

     import com.alibaba.edas.schedulerx.ProcessResult;
    import com.alibaba.schedulerx.worker.domain.JobContext;
     import com.alibaba.schedulerx.worker.processor.JavaProcessor;
     import org.springframework.beans.factory.annotation.Autowired;
    
     public class SimpleTask implements ScxSimpleJobProcessor {
    
         @Autowired
         private TestService testService;
    
         @Override
         public ProcessResult process(JobContext context) {
             System.out.println("-----------Hello world---------------");
             testService.test();
            return new ProcessResult(true);
         }
    
     }                        
  6. Create a scheduled job and add the required configurations.

    1. Log on to the EDAS console. In the test region, create a scheduled 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 value is *0 * * * * ?*. *0 * * * * ?* specifies 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 configurations 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.schedulerx2.endpoint=acm.aliyun.com
      spring.schedulerx2.namespace=d1ce9d38-ab9b-444e-ab2a-4*****
      spring.schedulerx2.groupId=****
      spring.schedulerx2.appKey=5pHlwaWpluJGd39******                            
  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 printed:

-----------Hello world---------------
---------IOC Success--------            

Deploy an application to EDAS

SchedulerX is designed to migrate 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 steps, see Overview of creating and deploying applications in Kubernetes clusters and Overview of creating and deploying applications in Elastic Compute Service (ECS) clusters.

After the application is deployed, you can schedule jobs in the EDAS console.

Deploy an application to EDAS 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 want to deploy applications in other regions, such as China (Hangzhou), you must perform the following steps when you create and execute a scheduled job:

  1. Log on to the EDAS console. In the test region, create a scheduled 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 other 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.