XXJ-JOB官网
https://www.xuxueli.com/xxl-job/
项目端
执行器自动初始化
个人优化的模块,原xxl-job没有这样的功能 项目启动时执行器会自动初始化,初始化失败后会默认重试5(可自行配置)次,每次重试间隔多累加1分钟,(如果初始化失败请自行再管理端添加执行器,初始化时依赖的管理端必须已启动)
/**
* 执行器初始化
*/
@Slf4j
@Component
public class Initexecutor {
@Autowired
private XxlJobConfig xxlJobConfig;
private int errCount = 0;
@PostConstruct
public void init() {
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder() .setNamePrefix("executo-init-pool-%d").build();
//Common Thread Pool
ExecutorService pool = new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
pool.execute(()-> {
try {
// 调用注册方法
XxlJobGroup xxlJobGroup = new XxlJobGroup();
xxlJobGroup.setAppname(xxlJobConfig.getAppname());
xxlJobGroup.setTitle(xxlJobConfig.getTitle());
xxlJobGroup.setAddressType(0);
//调用地址
// xxlJobConfig.getRegisterAddresses()
ReturnT<RegisterRes> returnT = XxlJobRemotingUtil.postBody(xxlJobConfig.getRegisterAddresses(), null, 15, xxlJobGroup, RegisterRes.class);
log.info("执行器自动注册响应: {}", JSONObject.toJSONString(returnT));
if (RegisterResEnum.SUCCESS.getCode().equals(Integer.valueOf(returnT.getCode()))) {
errCount = 0;
log.info("执行器自动注册{},appName = {},title = {}", RegisterResEnum.SUCCESS.getName(), xxlJobConfig.getAppname(), xxlJobConfig.getTitle());
return;
}
log.error("执行器自动注册失败,appName = {},title = {}", xxlJobConfig.getAppname(), xxlJobConfig.getTitle());
retryInit();
} catch (Exception e) {
log.error("执行器自动注册失败!appName = {},title = {}", xxlJobConfig.getAppname(), xxlJobConfig.getTitle(), e);
retryInit();
}
});
pool.shutdown();//gracefully shutdown
}
/**
* 报错后进行重试
*/
private void retryInit() {
if (errCount >= xxlJobConfig.getRetryCount()) {
log.error("已重试{}次,不再进行重试",errCount);
return;
}
try {
//重试间隔每次累加1分钟
Thread.sleep(errCount * 60 * 1000);
log.info("休息10秒钟再试!");
errCount += 1;
log.info("第{}次重试", errCount);
} catch (InterruptedException ex) {
log.error("", ex);
}
init();
}
}
项目中如创建job
/**
* XxlJob开发示例(Bean模式)
*
* 开发步骤:
* 1、任务开发:在Spring Bean实例中,开发Job方法;
* 2、注解配置:为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")",注解value值对应的是调度中心新建任务的JobHandler属性的值。
* 3、执行日志:需要通过 "XxlJobHelper.log" 打印执行日志;
* 4、任务结果:默认任务结果为 "成功" 状态,不需要主动设置;如有诉求,比如设置任务结果为失败,可以通过 "XxlJobHelper.handleFail/handleSuccess" 自主设置任务结果;
*
*/
@Slf4j
@Component
public class HsfpJobHandler {
private static Logger logger = LoggerFactory.getLogger(HsfpJobHandler.class);
/**
* 1、简单任务示例(Bean模式)
*/
@XxlJob("userJobHandler")
public void userJobHandler(String param) throws Exception {
long start = System.currentTimeMillis();
//========================== 开始业务逻辑 ==========================
XxlJobHelper.log("开始执行 userJobHandler...");
XxlJobHelper.log("入参:{}",XxlJobHelper.getJobParam());
InetAddress ip4 = Inet4Address.getLocalHost();
XxlJobHelper.log("当前服务ip:{}", ip4);
XxlJobHelper.log("XXL-JOB, Hello World.");
log.info("=================================================");
log.info("哈哈哈哈哈 {}", DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
log.info("=================================================");
for (int i = 0; i < 5; i++) {
XxlJobHelper.log("beat at:" + i);
TimeUnit.SECONDS.sleep(1);
}
XxlJobHelper.log("执行 userJobHandler结束,耗时:{}", System.currentTimeMillis() - start);
//========================== 业务逻辑结束 ==========================
// default success
}
}
Job配置
xxl:
job:
admin:
addresses: http://192.168.20.216:8099/hspf-job-admin
accessToken:
executor:
registerAddresses: ${xxl.job.admin.addresses}/jobgroup/autoRegistration
title: 执行器自动注册
appname: taskJob
address:
ip:
port: 7097
logpath: /data/applogs/xxl-job/jobhandler
logretentiondays: 30
retryCount: 5
管理端
登录
访问地址 http://{ip}:{port}/hspf-job-admin/joblog 用户名密码:admin/123456
执行报表
任务管理
编辑任务自动生效
上图中的JobHandler与3、项目中如何创建job模块中的@XxlJob注解中的值必须一致 如:@XxlJob(“userJobHandler”) 此步骤中JobHandler必须为userJobHandler
执行器管理
执行器初始化配置
/**
*执行器自动注册
* */
if(request.getRequestURI().endsWith("/jobgroup/autoRegistration") && request.getMethod().equals(HttpMethod.POST.name())){
return super.preHandle(request, response, handler);
}
执行器列表
如果部署多个服务这里的注册节点会显示多个
执行器添加
说明: AppName、名称指的是 项目端的Job配置模块下的appname和title,如果选择自动注册则不需要写机器地址
|