一、为什么需要任务调度 在Java中,传统的定时任务实现方案,比如Timer,Quartz等都或多或少存在一些问题:不支持集群、不支持统计、没有管理平台、没有失败报警、没有监控等等 而且在现在分布式的架构中,有一些场景需要分布式任务调度:同一个服务多个实例的任务存在互斥时,需要统一的调度。任务调度需要支持高可用、监控、故障告警。需要统一管理和追踪各个服务节点任务调度的结果,需要记录保存任务属性信息等。 显然传统的定时任务已经不满足现在的分布式架构,所以需要一个分布式任务调度平台,目前比较主流的是elasticjob和xxljob 二、xxljob的下载 https:github。comxuxuelixxljob 三、启动xxljob 打包xxlJobAdmin。jar,使用命令javajarxxlJobAdmin。jar监控任务的情况对任务的管理查询调度的日志执行器的相关管理 四、SpringBoot中配置xxljobimportcom。xxl。job。core。executor。impl。XxlJobSpringExecutor;importorg。springframework。beans。factory。annotation。Value;importorg。springframework。boot。autoconfigure。condition。ConditionalOnProperty;importorg。springframework。context。annotation。Bean;importorg。springframework。context。annotation。Configuration;ConfigurationConditionalOnProperty(namexxl。job。admin。enable,havingValuetrue)publicclassXxlJobConfig{Value({xxl。job。admin。addresses})privateStringadminAddresses;Value({xxl。job。executor。appname})privateStringappName;Value({xxl。job。executor。ip})privateStringip;Value({xxl。job。executor。port})privateintport;Value({xxl。job。accessToken})privateStringaccessToken;Value({xxl。job。executor。logpath})privateStringlogPath;Value({xxl。job。executor。logretentiondays})privateintlogRetentionDays;XXLJOB,分布式任务调度平台升级v2。1。2后遇到的坑XXLJOB,分布式任务调度平台升级v2。1。2后遇到的坑把Bean(initMethodstart,destroyMethoddestroy)改成Bean即可。就不会出现以上端口占用的问题!Bean(initMethodstart,destroyMethoddestroy)BeanpublicXxlJobSpringExecutorxxlJobExecutor(){XxlJobSpringExecutorxxlJobSpringExecutornewXxlJobSpringExecutor();xxlJobSpringExecutor。setAdminAddresses(adminAddresses);xxlJobSpringExecutor。setAppname(appName);xxlJobSpringExecutor。setIp(ip);xxlJobSpringExecutor。setPort(port);xxlJobSpringExecutor。setAccessToken(accessToken);xxlJobSpringExecutor。setLogPath(logPath);xxlJobSpringExecutor。setLogRetentionDays(logRetentionDays);returnxxlJobSpringExecutor;}} 五、配置文件中往xxljob中注册是否开启xxljobxxl。job。admin。enabletruexxljobadminaddresslist:调度中心部署跟地址:如调度中心集群部署存在多个地址则用逗号分隔。执行器将会使用该地址进行执行器心跳注册和任务结果回调。xxl。job。admin。addresseshttp:127。0。0。1:9090xxljobadminxxljobexecutoraddress:执行器AppName和地址信息配置:AppName执行器心跳注册分组依据;地址信息用于调度中心请求并触发任务和执行器注册。执行器默认端口为9999,执行器IP默认为空表示自动获取IP,多网卡时可手动设置指定IP,手动设置IP时将会绑定Host。单机部署多个执行器时,注意要配置不同执行器端口;xxl。job。executor。appnamexxljobexecutorsamplexxl。job。executor。ipxxl。job。executor。port9999xxljob,accesstoken:执行器通讯TOKEN,非空时启用xxl。job。accessTokenxxljoblogpath:执行器运行ahrefhttps:www。bs178。comrizhitargetblankclassinfotextkey日志a文件存储的磁盘位置,需要对该路径拥有读写权限xxl。job。executor。logpathxxljoblogretentiondays:执行器Log文件定期清理功能,指定ahrefhttps:www。bs178。comrizhitargetblankclassinfotextkey日志a保存天数,ahrefhttps:www。bs178。comrizhitargetblankclassinfotextkey日志a文件过期自动删除。限制至少保持3天,否则功能不生效;xxl。job。executor。logretentiondays1 六、SpringBoot中配置xxljob的JobHandlerahrefhttps:www。bs178。comrizhitargetblankclassinfotextkey日志a文件Slf4jComponentpublicclassScheduleLog{XxlJob(logJobHandlelr)publicReturnTStringexecute(Stringparam){此处处理定时调度的任务逻辑returnReturnT。SUCCESS;}}