All Products
Search
Document Center

Enterprise Distributed Application Service:Implement job scheduling

Last Updated:Mar 04, 2024

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

  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.

     <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 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 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.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 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 instance 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 * * * * ?* 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 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.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 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 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.

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 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 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.