当前位置:网站首页>调度中心xxl-Job
调度中心xxl-Job
2022-07-31 00:51:00 【C#ToJava】
当我们使用定时JOB时,可以借助开源框架XXL-JOB。
XXL-JOB是一个分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。
使用XXL-JOB我们需要两个项目,一个是XXL-JOB的项目,一个是被调用任务相关的项目。
1.XXL-JOB
git地址:https://gitee.com/xuxueli0323/xxl-job
中文文档地址:https://www.xuxueli.com/xxl-job/
克隆下来后最好切换到和项目版本相同的分支,不然可能会出现错误
如果自己中央仓库没有对应的jar包,可以链接XXL-JOB中央仓库
<!-- http://repo1.maven.org/maven2/com/xuxueli/xxl-job-core/ -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>${最新稳定版本}</version>
</dependency>
项目下载下来后会看到下面几个目录
doc:文档
xxl-job-admin:调度中心
xxl-job-core:公共依赖
xxl-job-executor-samples:执行器Sample示例(选择合适的版本执行器,可直接使用,也可以参考其并将现有项目改造成执行器)
:xxl-job-executor-sample-springboot:Springboot版本,通过Springboot管理执行器,推荐这种方式;
:xxl-job-executor-sample-frameless:无框架版本;
其中doc下是相关文档和数据库脚本
数据库脚本要在mysql下执行,新增项目所需的表
如果没有远程数据库可以安装本地数据库,安装mysql数据库可以借鉴:免安装版的Mysql安装与配置——详细教程_朝梦无痕的博客-CSDN博客_mysql免安装配置教程
然后需要添加调度中心配置文件
/xxl-job/xxl-job-admin/src/main/resources/application.properties
### web
server.port=52003
server.context-path=/xxl-job-admin
### actuator
management.context-path=/actuator
management.health.mail.enabled=false
### resources
spring.mvc.servlet.load-on-startup=0
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static/
### freemarker
spring.freemarker.templateLoaderPath=classpath:/templates/
spring.freemarker.suffix=.ftl
spring.freemarker.charset=UTF-8
spring.freemarker.request-context-attribute=request
spring.freemarker.settings.number_format=0.##########
### mybatis
mybatis.mapper-locations=classpath:/mybatis-mapper/*Mapper.xml
### xxl-job, datasource
spring.datasource.url=jdbc:mysql://localhost/xxl_job?Unicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=qq252865
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.max-active=30
spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.validation-query=SELECT 1
spring.datasource.tomcat.validation-interval=30000
### xxl-job email
spring.mail.host=smtp.****fund.com
spring.mail.port=25
[email protected]****fund.com
spring.mail.password=qq252865.
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
### xxl-job, access token
xxl.job.accessToken=
### xxl-job, i18n (default empty as chinese, "en" as english)
xxl.job.i18n=
## xxl-job, triggerpool max size
xxl.job.triggerpool.fast.max=200
xxl.job.triggerpool.slow.max=100
### xxl-job, log retention days
xxl.job.logretentiondays=30
调度中心访问地址:http://localhost:8080/xxl-job-admin (该地址执行器将会使用到,作为回调地址)
默认登录账号 “admin/123456”, 登录后运行界面如下图所示。
至此“调度中心”项目已经部署成功。
2.任务项目
创建一个自己的项目
pom文件添加
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.1.2</version>
</dependency>
application-local.yml 添加如下配置
xxl:
job:
#是否启用调度平台
enabled: true
#调度中心部署跟地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;
adminAddresses: http://192.168.8.60:52003/xxl-job-admin
#执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册
appName: console-server
# 执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务";
ip: 192.168.8.60
### 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
port: 18000
### 执行器通讯TOKEN [选填]:非空时启用;
accessToken:
### 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
logPath:
### 执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能;
logRetentionDays: 30
新建我的JobHandler
public abstract class BaseJobHandler extends IJobHandler {
protected static final String KEY = "traceNo";
protected static final String SERVER_ID_KEY = "serverId";
public ReturnT<String> execute(String param) throws Exception {
return ReturnT.SUCCESS;
}
/**
* job开始时打印日志
*/
protected void startJob() {
String requestId = UUID.randomUUID().toString().replace("-", "");
MDC.put(KEY, requestId);
MDC.put(SERVER_ID_KEY, "task");
}
/**
* job结束时打印日志
*/
protected void endJob() {
MDC.remove(KEY);
MDC.remove(SERVER_ID_KEY);
}
}
@Component
@Slf4j
public class DailyTradeReportExportStateCheckJobHandler extends BaseJobHandler {
@Resource
private DailyTradeReportExportStateCheckService dailyTradeReportExportStateCheckService;
private final ReentrantLock lock = new ReentrantLock();
@Override
@XxlJob(value = "dailyTradeReportExportStateCheckJobHandler")
public ReturnT<String> execute(String param) throws Exception {
try {
lock.lock();
String exportState=dailyTradeReportExportStateCheckService.getDailyTradeReportExportState();
if("0".equals(exportState)){
String newId = dailyTradeReportExportStateCheckService.getNewId();
String nextVal = dailyTradeReportExportStateCheckService.getNextval();
dailyTradeReportExportStateCheckService.insertMailInfo(newId,nextVal);
}
} catch (Exception ex) {
log.error("日交易报表导出状态检查JOB异常,ex=" + ex.getMessage());
} finally {
lock.unlock();
}
return ReturnT.SUCCESS;
}
}
至此,我的被调用程序也准备好了
然后回到调度中心配置页面,新建一个执行器,OnLine地址为 IP+端口号
然后新建一个对应的任务,手动执行试试能不能成功吧~~
边栏推荐
- Add text watermark to PHP image
- 网站频繁出现mysql等数据库连接失败等信息解决办法
- BOM系列之Navigator对象
- typescript10-commonly used basic types
- 【Yugong Series】July 2022 Go Teaching Course 017-IF of Branch Structure
- Jmeter parameter transfer method (token transfer, interface association, etc.)
- The difference between substring and substr in MySQL
- Typescript14 - (type) of the specified parameters and return values alone
- API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
- DOM系列之 offset 系列
猜你喜欢
ECCV 2022丨轻量级模型架构火了,力压苹果MobileViT(附代码和论文下载)
Understand from the 11 common examples of judging equality of packaging types in the written test: packaging types, the principle of automatic boxing and unboxing, the timing of boxing and unboxing, a
这个项目太有极客范儿了
typescript17 - function optional parameters
ShardingSphere's public table combat (7)
深度学习可以求解特定函数的参数么?
DOM系列之 client 系列
Jmeter parameter transfer method (token transfer, interface association, etc.)
xss bypass: prompt(1)
BOM系列之history对象
随机推荐
解决:Parameter 0 of method ribbonServerList in com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfigu
【c语言课程设计】C语言校园卡管理系统
BOM系列之Navigator对象
Summary of MySQL database interview questions (2022 latest version)
Jmeter参数传递方式(token传递,接口关联等)
(5) fastai application
Restricted character bypass
ShardingSphere之读写分离(八)
响应式布局与px/em/rem的比对
IOT cross-platform component design scheme
The difference between substring and substr in MySQL
typescript15-(同时指定参数和返回值类型)
What is Promise?What is the principle of Promise?How to use Promises?
MySql数据恢复方法个人总结
论文理解:“Designing and training of a dual CNN for image denoising“
typescript12 - union types
【愚公系列】2022年07月 Go教学课程 013-常量、指针
MySQL Series 1: Account Management and Engine
SereTOD2022 Track2 Code Analysis - Task-based Dialogue Systems Challenge for Semi-Supervised and Reinforcement Learning
Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv