@Configuration
public class XxlJobConfig {
private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.accessToken}")
private String accessToken;
@Value("${xxl.job.executor.appname}")
private String appname;
@Value("${xxl.job.executor.address}")
private String address;
@Value("${xxl.job.executor.ip}")
private String ip;
@Value("${xxl.job.executor.port}")
private int port;
@Value("${xxl.job.executor.logpath}")
private String logPath;
@Value("${xxl.job.executor.logretentiondays}")
private int logRetentionDays;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
logger.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
xxlJobSpringExecutor.setAppname(appname);
xxlJobSpringExecutor.setAddress(address);
xxlJobSpringExecutor.setIp(ip);
xxlJobSpringExecutor.setPort(port);
xxlJobSpringExecutor.setAccessToken(accessToken);
xxlJobSpringExecutor.setLogPath(logPath);
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
return xxlJobSpringExecutor;
}
# web port
server.port=8081
# no web
#spring.main.web-environment=false
# log config
logging.config=classpath:logback.xml
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
### xxl-job, access token
xxl.job.accessToken=default_token
### xxl-job executor appname
xxl.job.executor.appname=xxl-job-executor-sample
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address=
### xxl-job executor server-info
xxl.job.executor.ip=
xxl.job.executor.port=9999
### xxl-job executor log-path
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
### xxl-job executor log-retention-days
xxl.job.executor.logretentiondays=30
spring容器带动xxl执行器
一、主要逻辑
public class XxlJobSpringExecutor extends XxlJobExecutor implements ApplicationContextAware, SmartInitializingSingleton, DisposableBean {
private static final Logger logger = LoggerFactory.getLogger(XxlJobSpringExecutor.class);
// start
//利用spring容器,带动启动。
//具体执行时机:spring容器单例bean执行完成之后
@Override
public void afterSingletonsInstantiated() {
// init JobHandler Repository
/*initJobHandlerRepository(applicationContext);*/
//进行业务系统中,定时任务对应的方法注册
initJobHandlerMethodRepository(applicationContext);
// refresh GlueFactory
GlueFactory.refreshInstance(1);
// super start
try {
super.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void start() throws Exception {
//1.初始化业务系统执行日志存放目录。
// 配置:xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
XxlJobFileAppender.initLogPath(logPath);
// init invoker, admin-client
// 2.解析配置的管理端地址(xxl-job-admin),封装为AdminBiz对象。缓存到adminBizList(List)
// 配置:xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
initAdminBizList(adminAddresses, accessToken);
// 3.初始化日志清理线程,并设置清理周期
// 配置:xxl.job.executor.logretentiondays=30
JobLogFileCleanThread.getInstance().start(logRetentionDays);
// 4.初始化触发器回调线程、回调重试线程
TriggerCallbackThread.getInstance().start();
// i5.初始化定时任务执行器服务。接收管理端调用执行器
// 配置:xxl.job.executor.ip:执行器所在服务器ip(不配置,获取业务服务器ip)
// xxl.job.executor.port:执行器端口(不配置,在0-65535中查找可用端口)
// xxl.job.executor.address:管理端通过此地址调用执行器(不配置会利用上面ip、port拼接)
// xxl.job.executor.appname:执行器名称(必输)
initEmbedServer(address, ip, port, appname, accessToken);
}