当前位置:网站首页>使用XXL-JOB自定义任务并调度
使用XXL-JOB自定义任务并调度
2022-06-24 23:49:00 【~Rookie~Newbie~Noob~】
该项目设计的几张表
- xxl_job_group:执行器信息表
- xxl_job_info:调度扩展信息表,主要用于保存xxl-job的调度任务扩展信息,比如说像任务分组,任务名,机器的地址等。
- xxl_job_lock:任务调度锁表
- xxl_job_log:日志表,主要是保存xxl-job任务调度历史信息,像调度结果,执行结果,调度入参等等。
- xxl_job_log_report:日志报表,会存储xxl-job任务调度的日志报表,会在调度中心里的报表功能使用到
- xxl_job_logglue:任务GLUE日志,用于保存GLUE日志的更新历史变化,支持GLUE版本的回溯功能
- xxl_job_registry:执行器注册表,用在维护在线的执行器与调度中心的地址信息
- xxl_job_user:系统的用户表
xxl-job-admin项目运行
1. 从github上拉取项目
2. 创建数据库xxl-job
在项目的doc/db目录下找到数据库建库建表脚本,在mysql5.7中创建xxl-job数据库
3. 启动xxl-job-admin项目
- 修改配置文件application.properties,修改数据库连接为自己刚创建的数据库,并配置数据库的用户名和密码
- 修改logback.xml配置文件 <property name=“log.path” value=“./data/applogs/xxl-job/xxl-job-admin.log”/>,确保项目对路径有读写权限。
- 启动项目

访问到如下页面,说明项目启动成功。
定义一个定时任务,进行调度
1. 创建一个springboot工程
引入项目依赖
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0</version>
</dependency>
2. application.properties配置文件内容
# 应用名称
spring.application.name=demo
# 应用服务 WEB 访问端口
server.port=8081
#-------------------xxl-job配置---------------
logging.config=classpath:logback.xml
# 调度中心部署地址,多个配置逗号分隔
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
# 执行器token,非空时启用xxl-job, access token
xxl.job.accessToke=default_token
# 执行器名称,和控制台那边配置一样的名称,不然注册不上去
xxl.job.executor.appname=cryptcurrency-market
xxl.job.executor.address=
xxl.job.executor.ip=
xxl.job.executor.port=9999
# 执行器日志存储文件,需要对该路径拥有读写权限;为空则使用默认路径
xxl.job.executor.logpath=./data/logs/xxl-job/executor
# 执行器日志保持天数
xxl.job.executor.logretentiondays=30
3. logback.xml配置文件的内容
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="true" scanPeriod="1 seconds">
<contextName>logback</contextName>
<property name="log.path" value="./data/logs/xxl-job.app.log"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{
HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{
36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}.%d{
yyyy-MM-dd}.zip</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{
36} [%file : %line] %msg%n
</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</root>
</configuration>
4. 读取配置文件内容
package com.example.demo.config;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class XxlJobConfig {
private Logger log = LoggerFactory.getLogger(XxlJobConfig.class);
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@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.accessToke}")
private String accessToken;
@Value("${xxl.job.executor.logpath}")
private String logPath;
@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;
@Bean
public XxlJobSpringExecutor xxlJobSimpleExecutor() {
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;
}
}
5. 定义一个定时任务
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Component
public class MyJobHandler {
private Logger log = LoggerFactory.getLogger(MyJobHandler.class);
@XxlJob(value = "demoJobHandler", init="init", destroy = "destory")
public ReturnT<String> execute(String param) {
log.info("exeute schedule trigger:" + LocalDateTime.now());
return ReturnT.SUCCESS;
}
private void init() {
log.info("init method is called successfully");
}
private void destory() {
log.info("destory method is called successfully");
}
}
配置定时任务调度
在执行管理器中定义一个执行器

在任务管理器中,让上一步定义的执行器与创建的定时任务关联,并启动定时任务执行(定时任务要想被调用,需要先启动自己上一步中创建的springboot工程)

启动定时任务,那么定时任务就可以被定时调度了。
几个关键注意的地方
- application.properties文件中的 xxl.job.accessToke 的值要与xxl-job-admin中的保持一致
- application.properties中的xxl.job.executor.appname的值要是xxl-job-admin管理页面中定义的执行器
- 在管理页面中配置的任务,是应用中自定义的任务。如该示例中的demoJobHandler
边栏推荐
- JS regular matching numbers, upper and lower case letters, underscores, midlines and dots [easy to understand]
- How transformers Roberta adds tokens
- ACM. HJ70 矩阵乘法计算量估算 ●●
- internship:svn的使用
- After reciting the eight part essay, I won the hemp in June
- qt打包exe文件,解决“无法定位程序输入点_ZdaPvj于动态链接库Qt5Cored.dll”
- Enlightenment of using shadergraph to make edge fusion particle shader
- love
- Detailed explanation of cache (for the postgraduate entrance examination of XD)
- 把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(1)——迁移数据到节点1
猜你喜欢

It's 2022, and you still don't know what performance testing is?

Application of TSDB in civil aircraft industry

Insurance can also be bought together? Four risks that individuals can pool enough people to buy Medical Insurance in groups

Is it out of reach to enter Ali as a tester? Here may be the answer you want

Lihongyi, machine learning 6 Convolutional neural network

【STL源码剖析】配置器(待补充)

用向量表示两个坐标系的变换

CUDA编程入门极简教程

14 BS object Node name Name attrs string get node name attribute content

Pytorch learning notes (VII) ------------------ vision transformer
随机推荐
centos7.3修改mysql默认密码_详解Centos7 修改mysql指定用户的密码
自动化测试
I've been doing software testing for two years. I'd like to give some advice to girls who are still hesitating
Unity存档系统——Json格式的文件
Pit entry machine learning: I. Introduction
DSPACE设置斑马线和道路箭头
入坑机器学习:一,绪论
记一次beego通过go get命令后找不到bee.exe的坑
qt打包exe文件,解决“无法定位程序输入点_ZdaPvj于动态链接库Qt5Cored.dll”
如何卸载cuda
mysql学习笔记--单张表上的增删改查
Detailed explanation of cache (for the postgraduate entrance examination of XD)
Getting started with unityshader - Surface Shader
Using qdomdocument to manipulate XML files in QT
运行时修改Universal Render Data
After reciting the eight part essay, I won the hemp in June
国信金太阳打新债步骤 开户是安全的吗
使用ShaderGraph制作边缘融合粒子Shader的启示
Sumati gamefi ecological overview, element design in the magical world
Advanced mathematics | proficient in mean value theorem problem solving routines summary