Preparations

更新时间:
复制 MD 格式

This topic describes how to prepare for application development.

Set up a project

Before you use the task scheduling product, you must set up one of the following types of projects:

SOFABoot project

  1. Download and configure a SOFABoot project archetype.

    For more information, see Create a Web Project.

  2. For SOFABoot versions earlier than 3.4.3, add the Tracer dependency.

    Note

    • Before you add the dependency, check if your project already depends on Tracer to avoid JAR package conflicts.

    • SOFABoot 3.4.3 and later versions automatically include the Tracer dependency. You do not need to add it manually. For more information about the latest SOFABoot versions, see SOFABoot Version Guide.

    <dependency>
         <groupId>com.alipay.sofa</groupId>
         <artifactId>tracer-enterprise-sofa-boot-starter</artifactId>
    </dependency>
  3. In the pom.xml file of your SOFABoot project, add the task scheduling product dependency:

    <dependency>
          <groupId>com.alipay.sofa</groupId>
          <artifactId>scheduler-enterprise-sofa-boot-starter</artifactId>
    </dependency>
  4. Modify the global configuration items for SOFABoot.

    For more information about environment parameter configuration, see properties configuration items.

Non-SOFABoot project

  1. Configure the Maven settings.xml file for your project.

    In the settings.xml file in your local Maven path, configure the address, username, and password for the mvn.cloud.alipay.com repository. This allows your project to use Maven to retrieve the task scheduling JAR dependency package from this repository. You cannot retrieve the JAR package without this configuration.

    Note

    To obtain the address, username, and password for the mvn.cloud.alipay.com repository, submit a ticket.

  2. In the main pom.xml file of your project, add the antschedulerclient dependency.

    The following example uses version 1.0.7:

    <dependency>
        <groupId>com.alipay.antschedulerclient</groupId>
        <artifactId>antschedulerclient-core</artifactId>
        <version>1.0.7</version>
    </dependency>
    <dependency>
        <groupId>com.alipay.antschedulerclient</groupId>
        <artifactId>antschedulerclient-antvip</artifactId>
        <version>1.0.7</version>
    </dependency>
    
    <!-- Indirect dependency -->
    <dependency>
        <groupId>com.antcloud.antvip</groupId>
        <artifactId>antcloud-antvip-client</artifactId>
        <version>1.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.antcloud.antvip</groupId>
        <artifactId>antcloud-antvip-common</artifactId>
        <version>1.0.4</version>
    </dependency>
    <!-- Dependency for antvip-common -->
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.83</version>
    </dependency>
    
    <dependency>
        <groupId>com.alipay.sofa.cloud</groupId>
        <artifactId>sdk-core</artifactId>
        <version>0.1</version>
    </dependency>
    <dependency>
        <groupId>com.alipay.sofa.common</groupId>
        <artifactId>sofa-common-tools</artifactId>
        <version>1.0.12</version>
    </dependency>
    
    <dependency>
        <groupId>com.alipay.sofa</groupId>
        <artifactId>bolt</artifactId>
        <version>1.4.1</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba.toolkit.common</groupId>
        <artifactId>toolkit-common-lang</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>com.alipay.sofa</groupId>
        <artifactId>hessian</artifactId>
        <version>3.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.alipay.sofa.lookout</groupId>
        <artifactId>lookout-api</artifactId>
        <version>1.4.3</version>
    </dependency>
    <dependency>
        <groupId>com.alipay.sofa</groupId>
        <artifactId>tracer-core</artifactId>
        <version>2.1.1</version>
    </dependency>
    <dependency>
        <groupId>com.alipay.sofa</groupId>
        <artifactId>tracer-extensions</artifactId>
        <version>2.1.1</version>
    </dependency>
    <dependency>
        <groupId>com.alipay.common</groupId>
        <artifactId>tracer</artifactId>
        <version>2.1.1</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>20.0</version>
    </dependency>
  3. Obtain the environment configuration parameters.

    For more information about environment parameter configuration, see properties configuration items.

  4. Initialize the client.

    The following code provides an example:

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        try {
            ApplicationContext applicationContext = event.getApplicationContext();
            Map<String, IJobHandler> handlerBeans = applicationContext
                    .getBeansOfType(IJobHandler.class);
            for (IJobHandler handler : handlerBeans.values()) {
                handlerList.add(handler);
            }
    // zone(cell). If this is empty, it means no zone(cell) is specified.
            zone = System.getProperty("com.alipay.ldc.zone");
    
            // An Alibaba Cloud account AccessKey has full API access, which is a high security threat.
            // We recommend that you create and use a RAM user for API access or daily O&M.
            // Log on to the RAM console to create a RAM user.
            // This example shows how to save the AccessKey and AccessKeySecret in environment variables.
            // To prevent key leakage, do not hardcode the AccessKey and AccessKeySecret in your code.
            Config config = new Config(app, zone, instanceId, accessKey, securityKey);
            AntVipRouterConfig routerConfig = new AntVipRouterConfig();
            routerConfig.setEndPoint(antvipEndpoint);
            AntVipRouter router = new AntVipRouter(routerConfig);
            antSchedulerClient = new AntSchedulerClient(config, router, handlerList);
            antSchedulerClient.init();
        } catch (Throwable t) {
            LOGGER.error("init antschedulerclient error", t);
            throw new RuntimeException(t);
        }
    }

Download sample code

To quickly develop a local task scheduling project, download the sample project.