当前位置:网站首页>使用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
边栏推荐
- 计算机三级(数据库)备考题目知识点总结
- Copilot免费时代结束!学生党和热门开源项目维护者可白嫖
- 算力服务网络:一场多元融合的系统革命
- 使用ShaderGraph制作边缘融合粒子Shader的启示
- Uncaught Error: [About] is not a <Route> component. All component children of <Routes> must be a <Ro
- 14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
- Distributed transaction solutions and code implementation
- js正则匹配数字、大小写字母、下划线、中线和点[通俗易懂]
- MATLAB主窗口与编辑器窗口分开为两个界面的解决办法
- LINQ query (3)
猜你喜欢

CUDA编程入门极简教程

背了八股文,六月赢麻了……

数组-一口气冲完快慢指针

yarn : 无法加载文件 C:\Users\xxx\AppData\Roaming\npm\yarn.ps1,因为在此系统上禁止运行脚本

DDD concept is complex and difficult to understand. How to design code implementation model in practice?

Solution of separating matlab main window and editor window into two interfaces

保险也能拼购?个人可以凑够人数组团购买医疗保险的4大风险

mysql学习笔记--单张表上的增删改查

I've been doing software testing for two years. I'd like to give some advice to girls who are still hesitating

折叠屏将成国产手机分食苹果市场的重要武器
随机推荐
背了八股文,六月赢麻了……
14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
Insurance can also be bought together? Four risks that individuals can pool enough people to buy Medical Insurance in groups
计算机三级(数据库)备考题目知识点总结
Call system function security scheme
Talking about the advantages of flying book in development work | community essay solicitation
js正则匹配数字、大小写字母、下划线、中线和点[通俗易懂]
MySQL command backup
202112-2 序列查询新解
How to uninstall CUDA
Uncaught Error: [About] is not a <Route> component. All component children of <Routes> must be a <Ro
Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (1) -- migrate data to node 1
E - Average and Median(二分)
高数 | 精通中值定理 解题套路汇总
Leetcode 210: curriculum II (topological sorting)
Application of TSDB in civil aircraft industry
DSPACE设置斑马线和道路箭头
14 BS object Node name Name attrs string get node name attribute content
打新债是不是骗局 开户是安全的吗
Tell you about mvcc sequel