Use of XXl-JOB+ various timers

xxl-job task scheduling

Distributed task scheduling platform XXL-JOB

Types of scheduled tasks:

  1. Thread—Thread.sleep method (the underlying implementation of task scheduling) The way to create multi-threads-inherit Thread to implement runable and callable (has a return value and can throw exceptions) Thread pool (can reuse threads to save resources)

  2. TimerTask—java—util new TimerTask

  3. Thread pool scheduled task ScheduledExecutorService

  4. SpringBoot’s own scheduled task annotation @Schedule

  5. Third-party framework quartz

  6. xxl-job (solve the problem of repeated execution in traditional task scheduling [which cannot solve the idempotence of task scheduling in a distributed environment] in a single JVM)

1.Thread implements task scheduling

2.TimerTask implements task scheduling

3.ScheduleExecutorService implements task scheduling

4.quartz implements task scheduling (cron expression generates website
Online Cron expression generator)

-- dependencies

<!--quartz-->

<dependency>

<groupId>org.quartz-scheduler</groupId>

<artifactId>quartz</artifactId>

<version>2.2.1</version>

</dependency>

<dependency>

<groupId>org.quartz-scheduler</groupId>

<artifactId>quartz-jobs</artifactId>

<version>2.2.1</version>

</dependency>





-- Task class to be executed

public class MyJob implements Job {

/**

* Code for specific executor tasks

*/

@Override

public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

System.out.println("I'm using quartz");

}

}





-- Execution method

public static void main(String[] args) throws SchedulerException {

//1. Create Scheduler factory

SchedulerFactory sf = new StdSchedulerFactory();



//2. Get the scheduling instance from the factory

Scheduler scheduler = sf.getScheduler();



//3.Create JobDetail

JobDetail jb = JobBuilder.newJob(MyJob.class)

.withDescription("this is a run job") //job description

.withIdentity("tunJob", "runGroup") //job name and group

.build();

//Task running time, SimpleSchedule

long time = System.currentTimeMillis() + 3 * 1000L; //Start the task after 3 seconds

Date startTime = new Date();



//4. Create Trigger //Use SimpleScheduleBuilder or CronScheduleBuilder

Trigger t = TriggerBuilder.newTrigger()

.withDescription("")

.withIdentity("runTrigger", "runTriggerGroup")

//.withSchedule(SimpleScheduleBuilder.simpleSchedule())

.startAt(startTime)

.withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ? "))

.build();



//5. Register tasks and timers

scheduler.scheduleJob(jb, t);



//6. Start the scheduler

scheduler.start();



}

5.ScheduleExecutorService implements task scheduling

<!-- xxl-job -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.4.0</version>
</dependency>


# xxl-job configuration
xxl:
job:
admin:
# Dispatch center deployment and address [optional]: If there are multiple addresses for the dispatch center cluster deployment, separate them with commas. The executor will use this address for "executor heartbeat registration" and "task result callback"; if it is empty, automatic registration will be turned off; the address of the xxl-job background management interface
addresses: ${common-config.xxl-job.addresses}
executor:
# Executor registration [optional]: Priority is given to using this configuration as the registration address. If it is empty, the embedded service "IP:PORT" is used as the registration address. This provides more flexible support for container-type executor dynamic IP and dynamic mapping port issues.
address:
# Executor AppName [optional]: executor heartbeat registration grouping basis; if empty, automatic registration will be turned off
appname: order-service
# Executor IP [optional]: The default is empty to automatically obtain the IP. If there are multiple network cards, you can manually set the specified IP. The IP will not be bound to the Host and is only used for communication; the address information is used for "executor registration" and "scheduling" Center requests and triggers tasks";
ip:
# Executor port number [optional]: automatically obtained if it is less than or equal to 0; the default port is 9999. When deploying multiple executors on a single machine, be careful to configure different executor ports;
port: 28882
# Executor running log file storage disk path [optional]: You need to have read and write permissions on this path; if it is empty, the default path will be used;
logpath: /javaLogs/xxl-job/order-service
# Number of days to save executor log files [optional]: Expired logs are automatically cleaned, and it takes effect when the limit value is greater than or equal to 3; otherwise, such as -1, the automatic cleaning function is turned off;
logretentiondays: 15
# Actuator communication TOKEN [optional]: enabled when not empty;
accessToken: default_token



package com.micro.order.config;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class XxlJobConfig {

@Value("${xxl.job.admin.addresses}")
private String adminAddresses;

@Value("${xxl.job.accessToken}")
private String accessToken;

@Value("${xxl.job.executor.address}")
private String address;

@Value("${xxl.job.executor.appname}")
private String appName;

@Value("${xxl.job.executor.ip}")
private String ip;

@Value("${xxl.job.executor.port}")
private int port;

@Value("${xxl.job.executor.logpath}")
private String logPath;

@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;


@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
log.info("====xxl-job config init====");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppname(appName);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
}



@Component
@Slf4j
public class ServiceTimedActivation {

@XxlJob("ServiceTimedActivation")
public void run() {
System.out.println("9999999996");
}

}

Function of distributed task scheduling platform:

  1. Solving the idempotence of distributed task scheduling

  2. High availability, fault tolerance mechanism, load balancing, support for clusters, management mechanism, alarm scheme, automatic management, easy to learn (similar to nginx. Any request needs to be handed over to nginx first to forward any job to the task scheduling platform for management and distribution. Tasks need to be configured. distribution address)

Principle of xxl-job, a distributed task scheduling platform (the bottom layer is packaged by the quartz framework):

  1. Create an executor (client consumption address)

  2. Create tasks and execute them using quartz expressions

  3. Any job will be executed once on the task scheduling platform, a thread will be created, and requests will be sent to the executor address regularly according to the cron expression to execute the task.