SoFunction
Updated on 2025-04-13

Springboot integrates xxl-job to achieve dynamic parameter transmission

When working on projects, the first choice for the timed task framework is definitely xxl-job. So how much do you know about xxl-job?

1. What is xxl-job?

1. Basic composition

XXL-JOB mainly consists of two parts: scheduling center and executor:

Scheduling Center: Unified management of scheduling tasks on the task scheduling platform, responsible for triggering scheduling execution, and providing a task management platform. It is mainly responsible for managing scheduling information and issuing scheduling requests according to scheduling configuration, but it does not bear the business code itself.
Executor: receives the dispatching and executes the dispatching center, which can be executed directly or integrated into the project. It is responsible for receiving scheduling requests and executing task logic, including executing requests, termination requests, and log requests.

2. Features and Advantages

Simple and easy to use:XXL-JOB provides a friendly web interface, supports adding, deleting, modifying and checking tasks through the web interface, and also supports task management through the API interface. Users can complete task scheduling and management through simple operations.
Dynamic management: Support dynamic modification of task status, starting/stopting tasks and terminating running tasks, all operations will take effect in real time.
High availability: Both the scheduler and the executor support cluster deployment, which ensures high availability of scheduling and execution. Even if a node fails, it can automatically switch to other nodes to continue the task.
Elastic expansion and shrinkage: Once a new actuator machine is launched or offline, tasks will be reassigned during next scheduling to achieve elastic expansion and reduction.
Rich routing strategies: When deploying the executor cluster, it provides a variety of routing strategies, including the first, the last, the polling, the random, the consistent HASH, etc., to meet the needs of different scenarios.
Failover: If a machine in the executor cluster fails, Failover will automatically switch to a normal executor to send a scheduling request to ensure that the task can be executed normally.
Execution failed to view log: For tasks that fail to execute, you can view detailed log information to facilitate users to troubleshoot and repair problems.
Support email alarm: When the task execution fails, XXL-JOB supports sending emails to notify relevant personnel in order to handle exceptions in a timely manner.

III. Application scenarios

XXL-JOB is suitable for various scenarios where tasks need to be executed regularly or tasks are processed in real time, such as:

Send emails regularly: Through XXL-JOB, you can send emails to notify relevant personnel regularly.
Timely report generation: Various business reports can be generated regularly for relevant personnel to analyze and make decisions.
Clean up data regularly: Expired or invalid data can be cleaned regularly to ensure the clean and efficient operation of the database.
Real-time data collection: All kinds of business data can be collected in real time, and processed and analyzed.
Real-time message push: All kinds of business messages can be pushed to users or third-party systems in real time.

4. Comparison with other task scheduling frameworks

Compared with traditional task scheduling frameworks such as Quartz, XXL-JOB has the following advantages:

Low learning cost:XXL-JOB provides a visual web interface and rich document support, reducing learning costs.
Easy to operate: Through the web interface, you can easily create, edit, delete and query tasks.
Load balancing:XXL-JOB realizes collaboratively allocated operation tasks through the actuator, giving full play to the advantages of the cluster, and achieving load balancing.

2. How to dynamically transfer parameters to xxl-job?

  • Pass the task parameter field
    In the task management interface of XXL-JOB, each task has a "Task Parameters" field. You can fill in the parameters you want to pass to the executor in this field. These parameters are usually written in JSON, key-value pairs, or other formats, depending on how your executor parses them.

For example, you can fill in the "Task Parameters" field:

{
    "param1": "value1",
    "param2": "value2"
}

Then in the executor's task processing logic, the JSON string is parsed in some way (such as the Jackson library) and the specific parameter value is obtained.

  • Passing through custom parameters
    XXL-JOB supports passing custom parameters to the executor through the scheduling center. When creating or editing a task, you can write code in "GLUE source code" or "Bean mode", get the current task context through (), and then extract custom parameters from the context.

For example, in GLUE source code mode, you can write code like this:

import ;
import ;
import ;
import ;

import ;

@XxlJob("demoJobHandler")
public class DemoJobHandler {

    @Override
    public ReturnT<String> execute(String param) throws Exception {
        // Get the task context        Map<String, Object> context = ().getCustomParam();
        
        // Extract parameters from the context        String customParam1 = (String) ("customParam1");
        String customParam2 = (String) ("customParam2");
        
        // Execution task logic        ("customParam1: " + customParam1 + ", customParam2: " + customParam2);
        
        return ;
    }
}

However, note that the above code example is not entirely accurate, because the().getCustomParam() method may not exist (depending on the version of XXL-JOB and your configuration). Actually, you may need to get custom parameters in other ways, such as passing a JSON string through the task parameter field and parsing this string in the executor code.

  • Configuration via global parameters
    XXL-JOB also supports global parameter configuration, which can be set in the global configuration of the dispatch center and are passed to the executor through configuration files or command line parameters when the executor is started. However, this approach is often used to pass some unchanged global configuration information rather than dynamic task parameters.

  • Passing through API interface
    If your application scenario requires a more flexible and dynamic parameter delivery method, you can consider triggering tasks through the API interface and passing parameters in API requests. XXL-JOB provides a RESTful API interface, where you can trigger tasks through HTTP requests and pass parameters in the request body.

This is the article about Springboot integrating xxl-job to achieve dynamic transmission of parameters. This is the end of this article. For more related Springboot xxl-job content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!