官网博客地址:https://www.cnblogs.com/xuxueli/p/5021979.html 有详细的操作流程和介绍
环境要求:
JDK:1.7+
Servlet/JSP Spec:3.1/2.3
Tomcat:8.5.x/Jetty9.2.x
Spring-boot:1.5.x/Spring4.x
Mysql:5.6+
Maven:3+
项目分为xxl-job-admin(调度中心) 和 执行器(分布在子项目中的定时任务)
从官网下载下来目录结构如图
适应我司运维系统,将父子maven项目修改成一个gradle项目,也就是xxl-job-admin 这一个调度中心项目,执行器分布在各个子系统中.调度中心项目基本不需要修改,需修改的是子项目.
执行器项目配置:
config.properties:
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
#正式环境我这里用的域名,nginx分发
xxl.job.admin.addresses=http://IP:8080/xxl-job-admin,http://IP:8081/xxl-job-admin
### xxl-job executor address
xxl.job.executor.appname=xxl-job-tradeserver
xxl.job.executor.ip=
xxl.job.executor.port=9998
### xxl-job, access token
xxl.job.accessToken=
### xxl-job log path
xxl.job.executor.logpath=/data/Logs/tradeserver.cfaoe.local
### xxl-job log retention days
xxl.job.executor.logretentiondays=-1
build.gradle 增加如下引用:
//xxl-job star
compile ('com.xuxueli:xxl-job-core:2.0.1')
compile ('com.xuxueli:xxl-rpc-core:1.2.1')
compile ('org.codehaus.groovy:groovy-all:2.4.15')
compile ('org.apache.commons:commons-exec:1.3')
compile ('org.eclipse.jetty:jetty-server:9.2.26.v20180806')
compile ('org.eclipse.jetty:jetty-util:9.2.26.v20180806')
compile ('org.eclipse.jetty:jetty-http:9.2.26.v20180806')
compile ('org.eclipse.jetty:jetty-io:9.2.26.v20180806')
compile ('org.eclipse.jetty:jetty-client:9.2.26.v20180806')
compile ('com.caucho:hessian:4.0.51')
//xxl-job end
如发现使用不方便的地方可自己修改源码,如:
1.如果定时任务多,间隔短,日志是存库的,数据库日志表数据会非常多.
修改AdminBizImpl.java这个类的callback方法
// success, save log
log.setHandleTime(new Date());
log.setHandleCode(handleCallbackParam.getExecuteResult().getCode());
log.setHandleMsg(handleMsg.toString());
/**
* 任务执行成功就删除此任务之前的日志,防止数据量过大,只保留最后一次成功日志+错误日志
* 全部日志去本地的log文件查看
* songhe.zhang
* 2018年12月28日11:33:35
*/
if(log.getHandleCode() == 200 && log.getTriggerCode() == 200) {
xxlJobLogDao.deleteById(log.getId(),log.getJobGroup(),log.getJobId());
}
xxlJobLogDao.updateHandleInfo(log);
return ReturnT.SUCCESS;
sql
delete from XXL_JOB_QRTZ_TRIGGER_LOG
WHERE job_group = #{jobGroup} AND job_id = #{jobId} and handle_code = 200 and trigger_code = 200 AND id != #{id}
2.使用路由策略(一致hash)+阻塞处理策略(丢弃后续调度) 来预防上次任务未执行完成,就丢弃下次任务,防止重复执行,如果任务调度间隔很短,频繁丢弃,又配置了发邮件功能.那么邮箱就会爆炸了.
主要错误信息是: msg:block strategy effect:Discard Later
修改发邮件类:
JobFailMonitorHelper.java 里的failAlarm方法:
private void failAlarm(XxlJobInfo info, XxlJobLog jobLog){
// send monitor email
if (info!=null && info.getAlarmEmail()!=null && info.getAlarmEmail().trim().length()>0) {
String alarmContent = "Alarm Job LogId=" + jobLog.getId();
if (jobLog.getTriggerCode() != ReturnT.SUCCESS_CODE) {
//如果是Discard Later 不发邮件
if(jobLog.getTriggerMsg() != null && jobLog.getTriggerMsg().contains("block strategy effect:Discard Later")) {
return;
}
alarmContent += "
TriggerMsg=" + jobLog.getTriggerMsg();
}
if (jobLog.getHandleCode()>0 && jobLog.getHandleCode() != ReturnT.SUCCESS_CODE) {
alarmContent += "
HandleCode=" + jobLog.getHandleMsg();
}
Set emailSet = new HashSet(Arrays.asList(info.getAlarmEmail().split(",")));
for (String email: emailSet) {
XxlJobGroup group = XxlJobAdminConfig.getAdminConfig().getXxlJobGroupDao().load(Integer.valueOf(info.getJobGroup()));
String title = I18nUtil.getString("jobconf_monitor");
String content = MessageFormat.format(mailBodyTemplate,
group!=null?group.getTitle():"null",
info.getId(),
info.getJobDesc(),
alarmContent);
MailUtil.sendMail(email, title, content);
}
}
// TODO, custom alarm strategy, such as sms
}