edit-icon download-icon

Quick start

Last Updated: Mar 12, 2018

This topic helps you get a quick start with distributed jobs (SchedulerX) and provides guidance on operations including creating and configuring a scheduled job, compiling a scheduled job processor, and configuring the Bean of the scheduled job client.

This topic covers the following tasks:

  1. Create a scheduled job processor.

  2. Create a scheduled job and set the job triggering period.

  3. Run the client and start the scheduled job.

Here is an sample of creating a scheduled job that outputs “Hello World” every one minute:

1. Create a scheduled job processor

  1. Add SchedulerX-Client dependency to the project.

    1. <dependency>
    2. <groupId>com.alibaba.edas</groupId>
    3. <artifactId>schedulerX-client</artifactId>
    4. <version>1.6.6</version>
    5. </dependency>
  2. Implement the job processor interface.

    1. package com.schedulerx.test;
    2. import java.util.Date;
    3. import com.alibaba.edas.schedulerX.ProcessResult;
    4. import com.alibaba.edas.schedulerX.ScxSimpleJobContext;
    5. import com.alibaba.edas.schedulerX.ScxSimpleJobProcessor;
    6. public class ExecuteShellJobProcessor implements ScxSimpleJobProcessor {
    7. public ProcessResult process(ScxSimpleJobContext context) {
    8. System.out.println("Hello World! "+new Date());
    9. return new ProcessResult(true);//true indicates that the job runs successfully, and false indicates that the job fails to run.
    10. }
    11. }

2. Create a scheduled job and set the job triggering period

  1. Log on to the EDAS console, choose Scheduler > Job Groups in the left-side navigation pane, and click Create Group at the upper-right corner of the page.

  2. In the Create Group dialog box, enter the group description and click OK.

  3. In the left-side navigation pane, choose Scheduler > Jobs and click Create Job at the upper-right corner of the page.

  4. In the Create Job dialog box, enter related information and click OK.

    Create Job

    • Job Group: Corresponds to the job group. Select the job region from the drop-down list to its right.

      You can select an existing job group or create a job group.

    • Job Processing Interface: enter the full class name (including the full package path and the class name) of the interface.

      Enter the full class name of the job processor created at Step 2 (com.schedulerx.test.ExecuteShellJobProcessor).

    • Cron expression: The expression can be generated with the tool, or you can manually modify the time expression string.

      • Click Generate with Tools to Generate the scheduling expression using the tool:

        Generate with tools

      • Manually modify the time expression string:

        Manually Enter

        The time expression is compatible with the Quartz time expression. The Quartz time expression provides seven fields, which corresponds to second, minute, hour, day, month, week, and year respectively. It’s safe to omit the year most of the time.

      For more information about the time expression, see Create a job.

3. Run the client and start the scheduled job

  1. Initialize SchedulerX Client.

    • Independent Java application method

      1. package com.schedulerx.test;
      2. import com.alibaba.edas.schedulerX.SchedulerXClient;
      3. public class schedulerxTestMain {
      4. public static void main(String[] args) {
      5. SchedulerXClient schedulerXClient = new SchedulerXClient();
      6. schedulerXClient.setGroupId("101-1-1-52");
      7. schedulerXClient.setRegionName("cn-hangzhou");
      8. /*
      9. //If a cluster in Shanghai region is used, you must set the domainName attribute and set RegionName to "cn-shanghai".
      10. schedulerXClient.setRegionName("cn-shanghai");
      11. schedulerXClient.setDomainName("schedulerx-shanghai.console.aliyun.com");
      12. */
      13. try {
      14. schedulerXClient.init();
      15. } catch (Throwable e) {
      16. e.printStackTrace();
      17. }
      18. }
      19. }
    • EDAS application management (WAR package deployment) method

      1. Add spring-web dependency to the project.

        1. <dependency>
        2. <groupId>org.springframework</groupId>
        3. <artifactId>spring-web</artifactId>
        4. <version>4.1.5.RELEASE</version>
        5. </dependency>
      2. Add configurations of the SchedulerX-Client bean to the Spring configuration file.

        1. <bean id="schedulerXClient" class="com.alibaba.edas.schedulerX.SchedulerXClient" init-method="init">
        2. <property name="groupId">
        3. <value>101-1-1-77</value>
        4. </property>
        5. <property name="regionName">
        6. <value>cn-hangzhou</value>
        7. </property>
        8. <!--If a cluster in Shanghai region is used, you must set the domainName attribute and set RegionName to cn-shanghai.
        9. <property name="regionName">
        10. <value>cn-shanghai</value>
        11. </property>
        12. <property name="domainName">
        13. <value>schedulerx-shanghai.console.aliyun.com</value>
        14. </property>-->
        15. </bean>
      3. Configure the Spring listener in the web.xml file.

        1. <?xml version="1.0" encoding="UTF-8"?>
        2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
        3. <display-name>scxWarTest</display-name>
        4. <context-param>
        5. <param-name>contextConfigLocation</param-name>
        6. <param-value>WEB-INF/applicationContext.xml</param-value>
        7. </context-param>
        8. <listener>
        9. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        10. </listener>
        11. </web-app>

    Note: For both methods, attribute values set for schedulerXClient must match those configured in the console, as shown in the following figure:

    Job list

    • Group: It corresponds to the groupId attribute in the SchedulerX-Client bean.
    • Region: It corresponds to the regionName attribute in the SchedulerX-Client bean.
  2. Start the scheduled job.

    • Independent Java application method

      1. Install EDAS Agent on the ECS instance where the application runs.

      2. Upload the JAR package of the application to the application server.

      3. Run the main function schedulerxTestMain and add the following parameter -Dspas.identity=/home/admin/.spas_key/default when the program starts.

    • EDAS application management (WAR package deployment) method

      1. Compress the application to a WAR package.

      2. Create the application in the “Application Management” module of the EDAS console and upload the WAR package.

      3. On the Application Management page, start the scheduled job application.

    After the scheduled job is started, the system calls and implements the class code (ExecuteShellJobProcessor) of the job processor interface according to your setting (every one minute) and runs the “Hello World” output job.

Thank you! We've received your feedback.