xxl-job for beginners (nanny-level introductory tutorial)

Hits: 0

Article directory


The official documentation of this distributed timer (the link below the official website) has been explained very clearly, and I will not repeat
the xxl-job that I just learned today. Going deeper, today’s goal is just how to use this distributed timer quickly.

Not much to say, beginners understand beginners best

Official website and source code download address

Official website: https://www.xuxueli.com/xxl-job/
Source code download address: https://github.com/xuxueli/xxl-job/releases


1. Download the source code

According to the aboveSource code download addressThe link goes in to download the source code and it’s done
. Use it under Windows to download this one.
After downloading the source code, unzip it.

2. IDEA import project

After importing it looks like this

It can be seen that the source code provided by the author is mainly divided into three modules

  • xxl-job-admin
  • xxl-job-core
  • xxl-job-executor-samples

Let’s take a look at xxl-job-adminthis

Data preparation must be done before starting this module

3. Data Preparation

Prepare the required tables first, and use the sql provided by the author directly here.

Copy sql and execute it in your own database

Modify the configuration file
and change it to the configuration of your own database (you can look at other configuration information, and there are also configurations such as modifying sending emails)

4. Log in to the task scheduling center

xxl-job-adminrun startup class

After the startup is complete, you can log in to see

Address: http://localhost:8080/xxl-job-admin

This is the way to go in, but you need an account and password

In fact, the account and password are in the sql that was run before

Account: adminPassword
: 123456

Enter account and password, log in

After logging in, you can see the control page of the task scheduling center.

You can click on this sidebar yourself and see what’s there

Now that the control platform has been opened, how do I add my scheduled tasks?

5. Test the scheduled task

Here the author provides xxl-job to integrate other frameworks or software. Here we first learn to integrate spring-boot. After all, this is the most important for java development.

It can be seen that the pom file in the project integrated with spring-boot has the dependency of introducing this xxl-job-core

<!-- xxl-job-core -->

So if we use this timer in our own project, we also need to add this dependency

The author also provides the docker configuration here, which can be used if you need to run on your own server

Let’s take a look at the configuration file of this project

The content of the configuration file is as follows:

# web port
# no web

# log config

### xxl-job admin address list, such as "http://address" or "http://address01,http://address02" 
# This is mainly to configure the address of your task scheduling center (that is, the control platform) , if you start locally, you don't need to change it, otherwise change it to the address on your server 
xxl.job.admin.addresses =http:// 127.0 . 0.1 : 8080 /xxl-job-admin

### xxl-job, access token

### xxl-job executor appname 
xxl.job.executor.appname =xxl-job-executor-sample
 ### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null 
xxl .job.executor.address =
 ### xxl-job executor server-info 
xxl.job.executor.ip =
 # This is the port where your timer executes, I changed it to use the port that comes with the service, and then configure it Make an offset in the class, so that the port is better to maintain the point 
xxl.job.executor.port = ${server.port} 
### xxl-job executor log-path 
xxl. job.executor.logpath =/data/applogs/xxl-job/jobhandler
 ### xxl-job executor log-retention-days 
xxl.job.executor.logretentiondays = 30

Look at the configuration class again

The content is as follows:

public class XxlJobConfig {
    private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

    private String adminAddresses;

    private String accessToken;

    private String appname;

    private String address;

    private String ip;

    private int port;

    private String logPath;

    private int logRetentionDays;

    public XxlJobSpringExecutor xxlJobExecutor() {
        logger.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();

        return xxlJobSpringExecutor;


Modify the actuator port offset (this depends on your own preferences, you can specify the port yourself, I directly add the service port and add 1000 to the actuator port)

If successful, then run the startup class of this xxl-job-executor-sample-springservice .

This timed task is xxl-job-executor-sample-springbootactually a task in , which can be found in the SampleXxlJob.javaclass

Then let’s execute this timer task.

Online Cron Expression Generator: https://cron.qqe2.com/

You can see that this is executed every day at 0:00 in the morning, this is not possible, then change it

Go and modify the demoJobHandlerscheduled , and print something for it to facilitate observation

Restart the project and clear the console

Start the scheduled task

When the point is reached, go to the console to see the execution

You can see that the console has six prints (because it is set to execute every 10 seconds starting at 11:35)

You can also query the log of this scheduled task

Integrate into your own project

Now it should be almost the same for how to use this xxl-job, and now I will integrate it into my own project

Adding modules to my own projectmike-xxl-job

The structure of the module is probably like this

I use the yml method for the configuration file here, and use [nacos to] manage my configuration. The following is the content of my various files

(The following content needs to be configured according to the project itself, for reference only)


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">





        <!-- spring-boot-starter-web (spring-webmvc + tomcat) -->


        <!-- xxl-job-core -->


                    <mainClass>com.mike.mo.job.XxlJobApplication </ mainClass > 
                    < layout > ZIP </ layout > 
                </ configuration > 
                < executions > 
                    < execution > 
                        < goals > 
                            < goal > repackage </ goal > <!-- you can put dependencies Packages are packaged into the generated Jar package --> 
                        </ goals > 
                    </ execution > 
                </ executions > 
            </ plugin > 
        </ plugins > 



  port: 6804
  config: classpath:logback.xml
  file: /www/mike/project-mike/logs/mike-xxl-job.log
    com.alibaba.nacos: error
    name: mike-xxl-job
    allow-bean-definition-overriding: true
        server-addr: localhost:8848
        namespace: f90ad91c-ac39-4a2d-8fb3-fcd33ded9123
    url: jdbc:mysql://
      username: 'root'
      password: 'root'
      driver-class-name: 'com.mysql.cj.jdbc.Driver'
      max-lifetime: 120000

    appName: mike-xxl-job
    port: ${server.port}
    logPath: /data/applogs/xxl-job/jobhandler
    logRetentionDays: 30


    name: mk-xxl-job
        file-extension: yaml
        namespace: f90ad91c-ac39-4a2d-8fb3-fcd33ded9123
    active: local

logback.xml(This is directly copied from the author, and I have not studied it), and it is also glued below

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="true" scanPeriod="1 seconds">

    <property />

    <appender >
            <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>

    <appender >
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n

    <root level="info">
        <appender-ref ref="console"/>
        <appender-ref ref="file"/>



public class XxlJobApplication {
    public static void main(String[] args) {


public class MikeXxlJob {
    private final static Logger logger = LoggerFactory.getLogger(MikeXxlJob.class);

     * my first timer task
    public void funJobHandler() {
        System.out.println( "Ah~ I executed~~~~" );

I integrated the configuration of xxl-job into a bean object, which is a bit different from the previous propertiesconfiguration file


@ConfigurationProperties(prefix = "xxl-job")
public class XxlJobBeanConfig {
    private Admin admin;
    private Executor executor;
    private String accessToken;

    public static class Executor {
        private String appName;
        private String ip;
        private int port;
        private String logPath;
        private int logRetentionDays;

    public static class Admin {
        private String addresses;

XxlJobConfig.javaxxl-job timer configuration class

@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class XxlJobConfig {
    private final Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

    private final XxlJobBeanConfig bean;

    public XxlJobSpringExecutor xxlJobExecutor() {
        logger.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setPort(bean.getExecutor().getPort()+ 1000 ); //The port is offset backward by 1000

        return xxlJobSpringExecutor;

The above is all the code of my timer module. Now let’s run mike-xxl-jobthe

Set the timing rules and start the newly added timing task

When the time is up, my scheduled task is also executed

View the scheduling log below


This is the whole content of the xxl-job quick start, and some content has not been added. You can learn it by yourself. After I have some practical experience, I will do in-depth research and share it with you.

You may also like...

Leave a Reply

Your email address will not be published.