How to use the XXL Job Core framework for distributed task scheduling
XXL job core is a lightweight distributed task scheduling framework, which is suitable for scheduling and execution of large -scale distributed tasks.This article will introduce you to how to use the XXL Job Core framework for distributed task scheduling and provide examples of Java code.
The use step of XXL job core is as follows:
1. Introduction dependencies:
In your project, you need to introduce the dependence of XXL Job Core.It can be achieved by adding the following dependencies in Maven Pom.xml file:
<dependency>
<groupId>com.xxl-job</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0</version>
</dependency>
2. Configuration scheduling center:
In your project, you need to configure the dispatch center address and port of the XXL Job Core.It can be achieved by adding the following configuration information to the Application.Properties (or Application.yml) file::
xxl.job.admin.addresses=http://localhost:8080/xxl-job-admin/
The address and port here need to be consistent with your actual XXL Job Admin address.
3. Define tasks:
In your project, define tasks that need to be scheduled.You can create an ordinary Java class that uses the method to execute the method of the `@xxljob` annotation logo.For example:
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
public class MyJob {
@XxlJob("myJobHandler")
public void myJobHandler(String param) throws Exception {
XxlJobLogger.log("Start my job handler.");
// Execute the task logic
// ...
XxlJobLogger.log("Finish my job handler.");
}
}
In the above example, the parameter of the annotation of `@xxljob` is the name of the task. You can name it according to the actual needs.
4. Initialize and start the scheduling center and actuator:
In your project, you need to initialize and start the dispatch center and actuator of the XXL job core.You can create a startup class, use the following code to start the scheduling center and the actuator:
import com.xxl.job.core.executor.XxlJobExecutor;
public class Application {
public static void main(String[] args) {
// Initialize scheduling center
XxlJobAdmin.init();
// Initialize the actuator
XxlJobExecutor.init();
// Start scheduling center
XxlJobAdmin.start();
// Start the actuator
XxlJobExecutor.start();
}
}
In this way, you successfully configure the XXL Job Core framework and start the dispatch center and the actuator.
5. Add tasks to the dispatch center:
Open the browser, enter the address and port of the scheduling center, and enter the management interface of XXL Job Admin.Here, you can add, modify or delete tasks, and check the execution and logs of the task.
When adding tasks, you need to fill in the basic information of the task, such as task names, task descriptions, CRON expressions, etc.You also need to choose a actuator to specify the task Handler and task parameters to be executed.
After adding the task, XXL Job Admin will trigger the execution of the task regularly according to the CRON expression you set.
At this point, you have learned how to use the XXL job core framework for distributed task scheduling.I hope this article can help you!If you need more detailed information about XXL Job Core, you can check the official documentation.
Attachment: Complete example code
MyJob.java:
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
public class MyJob {
@XxlJob("myJobHandler")
public void myJobHandler(String param) throws Exception {
XxlJobLogger.log("Start my job handler.");
// Execute the task logic
// ...
XxlJobLogger.log("Finish my job handler.");
}
}
Application.java:
import com.xxl.job.core.executor.XxlJobExecutor;
public class Application {
public static void main(String[] args) {
// Initialize scheduling center
XxlJobAdmin.init();
// Initialize the actuator
XxlJobExecutor.init();
// Start scheduling center
XxlJobAdmin.start();
// Start the actuator
XxlJobExecutor.start();
}
}
Please note that the above code is only an example and does not fully display all the characteristics and details of XXL job core.You can modify and expand according to your actual needs.