All Products
Search
Document Center

Microservices Engine:Get started in 10 minutes

Last Updated:Apr 23, 2025

You can upload an application image to the Container Service for Kubernetes (ACK) console and connect the application to XXL-JOB. This allows you to schedule jobs and perform job sharding in an efficient manner.

Prerequisites

Overview

This solution provides an example on how to deploy an image to a container by using ACK Serverless or Serverless App Engine (SAE) to develop and deploy an application, connect the application to XXL-JOB, and then test the connected application by using standalone and sharding broadcast jobs. In this example, the demo application image provided by XXL-JOB is used. This helps you understand the use and configuration of XXL-JOB. To implement this solution, perform the following steps:

  1. Create an application: You can create an application in XXL-JOB to manage jobs in a centralized manner. This allows you to view, configure, and schedule jobs with ease and improves job management efficiency.

  2. Deploy a demo project to a container: You can quickly deploy and scale the demo image by using ACK Serverless or SAE.

  3. Perform testing and verification: You can verify that jobs on the connected application can be automatically scheduled and managed in XXL-JOB, and the jobs can be executed at the specified point in time.

Step 1: Create an application

  1. Log on to the MSE console, and select a region in the top navigation bar.

  2. On the page that appears, click the ID of your instance to go to the instance details page. On the instance details page, click Application Management in the left-side navigation pane. On the page that appears, click CreateApplication. In the CreateApplication panel, configure the AppName and Name parameters, click Automatic Generation for AccessToken to obtain an access token, and then click OK.

Step 2: Deploy a demo project to a container

XXL-JOB provides the following public demo application image: registry.cn-hangzhou.aliyuncs.com/schedulerx/xxljob-demo:2.4.1. You can use one of the following methods to deploy the demo application and connect it to XXL-JOB.

Important

Make sure that the cluster in which the application is deployed resides in the same virtual private cloud (VPC) as the XXL-JOB instance, and that the inbound rules of the security group to which the application node belongs allow access from the CIDR blocks of the VPC.

ACK Serverless

  1. Log on to the ACK console. On the Clusters page, click Create Kubernetes Cluster. On the ACK Serverless tab of the buy page, select Configure SNAT for VPC to facilitate image pulling. Skip this operation if SNAT is already configured for the VPC.

  2. On the page that appears, click Application Management in the left-side navigation pane. Then, find the desired application and click Access Configuration in the Operation column.

  3. On the Clusters page of the ACK console, click the name of the created cluster. In the left-side navigation pane, choose Workloads > Deployments. On the Deployments page, click Create from YAML. In this example, Access Method 2 is used and the application is restarted by using the -D parameter. Copy the code provided by the access method and replace the value of the JAVA_OPTS environment variable with the copied code.

    image

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: xxljob-demo
      labels:
        app: xxljob-demo
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: xxljob-demo
      template:
        metadata:
          labels:
            app: xxljob-demo
        spec:
          containers:
          - name: xxljob-executor
            image: registry.cn-hangzhou.aliyuncs.com/schedulerx/xxljob-demo:2.4.1
            ports:
            - containerPort: 9999
            env:
              - name: JAVA_OPTS
                value: >-
                  -Dxxl.job.admin.addresses=http://xxljob-xxxxx.schedulerx.mse.aliyuncs.com
                  -Dxxl.job.executor.appname=xxxxx
                  -Dxxl.job.accessToken=xxxxxxx

SAE

  1. Log on to the SAE console and choose Application Management > Applications in the left-side navigation pane. On the Applications page, click Create Application to create an application.

  2. Configure the basic information about the application. Select an existing namespace, and select a vSwitch and a security group in the VPC associated with the namespace. Click Specify Image in the Application Deployment Method section. In the Specify Image panel, enter the following URL of the demo image on the Custom Image tab: registry.cn-hangzhou.aliyuncs.com/schedulerx/xxljob-demo:2.4.1. Then, click OK.

    Important

    The namespace that you select must be associated with the VPC in which the XXL-JOB instance resides.

3. In the Advanced Settings step, configure the JAVA_OPTS environment variable.

You can obtain the value of the environment variable by performing the following operations:

Log on to the MSE console, and select a region in the top navigation bar. On the XXL-JOBVersion page, find the instance to which you want to connect the application and click the instance ID. The instance details page appears. In the left-side navigation pane, click Application Management. On the page that appears, find the created application and click Access Configuration in the Operation column. In this example, Access Method 2 is used. Copy the code provided by the access method and use the copied code as the value of the JAVA_OPTS environment variable.

image

4. Click Create Application with One Click. The application is created in the SAE console.

Step 3: Perform testing and verification

1. Check executors

On the instance details page of the MSE console, click Application Management in the left-side navigation pane. On the Application Management page, find the deployed application and click the value in the Number of actuators column to view the IP address and online status of each executor.

image

2. Test jobs

The demo image contains the code for testing jobs, including the JobHandler of different types of jobs:

  • JobHandler of standalone jobs: helloworld

  • JobHandler of sharding broadcast jobs: shardingJobHandler

Sample code

@Component
public class SampleXxlJob {
    private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);

    /**
     * 1. A sample simple job, which returns the result immediately.
     */
    @XxlJob("helloworld")
    public ReturnT<String> helloworld(String param) throws Exception {
        XxlJobLogger.log("XXL-JOB, Hello World, param=" + param);
        System.out.println("XXL-JOB, Hello World, finished");
        return ReturnT.SUCCESS;
    }

    /**
     * 2. A sample simple job, which repeats five times.
     */
    @XxlJob("demoJobHandler")
    public ReturnT<String> demoJobHandler(String param) throws Exception {
        XxlJobLogger.log("XXL-JOB, demoJobHandler, param=" + param);
        System.out.println("XXL-JOB, demoJobHandler, start...");
        for (int i = 0; i < 5; i++) {
            System.out.println("beat at:" + i);
            XxlJobLogger.log("beat at:" + i);
            TimeUnit.SECONDS.sleep(2);
        }
        System.out.println("XXL-JOB, demoJobHandler, end...");
        return ReturnT.SUCCESS;
    }

    /**
     * 3. A sharding broadcast job.
     */
    @XxlJob("shardingJobHandler")
    public ReturnT<String> shardingJobHandler(String param) throws Exception {

        // Shard parameters
        ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
        XxlJobLogger.log("Shard parameters: Current shard ID = {}, Total shard count = {}", shardingVO.getIndex(), shardingVO.getTotal());

        // Business logic
        for (int i = 0; i < shardingVO.getTotal(); i++) {
            if (i == shardingVO.getIndex()) {
                XxlJobLogger.log("Shard {}, Process the matching shard", i);
            } else {
                XxlJobLogger.log("Shard {}, Ignore", i);
            }
        }

        return ReturnT.SUCCESS;
    }

    /**
     * 4. A lifecycle job: Custom logic is supported when a job is initialized or destroyed.
     */
    @XxlJob(value = "demoJobHandler2", init = "init", destroy = "destroy")
    public ReturnT<String> demoJobHandler2(String param) throws Exception {
        XxlJobLogger.log("XXL-JOB, Hello World.");
        return ReturnT.SUCCESS;
    }
    public void init(){
        logger.info("init");
    }
    public void destroy(){
        logger.info("destory");
    }

    /**
     * 5. A failed job and 1 / 0 exception is thrown.
     */
    @XxlJob(value = "failedJobHandler")
    public ReturnT<String> failedJobHandler(String param) throws Exception {
        XxlJobLogger.log("XXL-JOB, Hello World.");
        int a = 1/0;
        return ReturnT.SUCCESS;
    }

}

Standalone job

A standalone job is executed in an idempotent manner in an executor selected based on the routing policy.

  1. On the instance details page, click Task Management in the left-side navigation pane. On the page that appears, click Create Task. In the Basic configuration step of the Create Task panel, configure the Task Name and jobhandler Name parameters, select the deployed application from the Associated Applications drop-down list, set the Routing Policy parameter to Polling, and then click Next step.

    image

  2. In the Timing Configuration step, set the Time Type parameter to cron and click Using the Build Tool to generate a cron expression. In this example, the job is executed once every day at 12:00. Then, click Next step.

    image

  3. In the Notification Configuration step, you can configure alerts for execution timeout, execution success, and execution failure, and specify notification methods and alert contacts. In this example, the default configurations in the console are used.

    image

  4. After the job is created, find the job on the Task Management page and click Run once in the Operation column. In the Perform tasks manually dialog box, select a machine on which you want to execute the job, enter instance parameters, and then click OK.

    image

  5. Find the job on the Task Management page and choose More > Scheduling Records in the Operation column to view the execution records of the job.

  6. On the instance details page, click Execution List in the left-side navigation pane. On the page that appears, find the execution record that you want to view and click Log in the Operation column to view the logs of the job execution.

    image

Sharding broadcast job

A sharding broadcast job is executed in all executors of the application, and each executor is assigned a unique shard number. This type of job is applicable to distributed batch processing. Unlike open source XXL-JOB that does not support the aggregation feature, XXL-JOB aggregates and displays the execution of a sharding broadcast job on each shard.

  1. On the instance details page, click Task Management in the left-side navigation pane. On the page that appears, click Create Task. In the Basic configuration step of the Create Task panel, configure the Task Name and jobhandler Name parameters, select the deployed application from the Associated Applications drop-down list, set the Routing Policy parameter to Multipart Broadcast, and then click Next step.

    image

  2. In the Timing Configuration step, set the Time Type parameter to cron and click Using the Build Tool to generate a cron expression. In this example, the job is executed at the 10th minute of every hour. Then, click Next step.

    image

  3. In the Notification Configuration step, you can configure alerts for execution timeout, execution success, and execution failure, and specify notification methods and alert contacts. In this example, the default configurations in the console are used.

    image

  4. After the job is created, find the job on the Task Management page and click Run once in the Operation column. In the Perform tasks manually dialog box, select one or more machines on which you want to execute the job, enter instance parameters, and then click OK.

    image

  5. Find the job on the Task Management page and choose More > Scheduling Records in the Operation column to view the execution records of the job.

  6. On the instance details page, click Execution List in the left-side navigation pane. On the page that appears, find the execution record that you want to view and click Details in the Operation column. In the Scheduling Details panel, view the execution details on each machine on the Slice Details tab.

    image

  7. Find a shard and click Log in the Operation column to view the logs of the job execution of the shard.

    image