初始化仓库
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package xtools.boot.job.xxl;
|
||||
|
||||
import org.springframework.context.annotation.Import;
|
||||
import xtools.boot.core.utils.ModuleLoadUtils;
|
||||
import xtools.boot.job.xxl.selector.BootXxlJobImportSelector;
|
||||
|
||||
/**
|
||||
* <p>Title : BootXxlJobConfiguration</p>
|
||||
* <p>Description : BootXxlJobConfiguration</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/01/01 09:30
|
||||
*/
|
||||
@Import(BootXxlJobImportSelector.class)
|
||||
public class BootXxlJobConfiguration {
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
public BootXxlJobConfiguration() {
|
||||
ModuleLoadUtils.loadSuccess(BootXxlJobConfiguration.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package xtools.boot.job.xxl.config;
|
||||
|
||||
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import xtools.core.StringUtils;
|
||||
|
||||
/**
|
||||
* <p>Title : XxlJobConfig</p>
|
||||
* <p>Description : XxlJobConfig</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/3/10 14:06
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class XxlJobConfig {
|
||||
|
||||
/**
|
||||
* 调度中心部署根地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔.执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;
|
||||
*/
|
||||
@Value("${xxl.job.admin.addresses}")
|
||||
private String adminAddresses;
|
||||
|
||||
/**
|
||||
* 调度中心通讯TOKEN [选填]:非空时启用;
|
||||
*/
|
||||
@Value("${xxl.job.admin.accessToken}")
|
||||
private String accessToken;
|
||||
|
||||
/**
|
||||
* 调度中心通讯超时时间[选填],单位秒;默认3s;
|
||||
*/
|
||||
@Value("${xxl.job.admin.timeout}")
|
||||
private int timeout;
|
||||
|
||||
/**
|
||||
* 执行器启用开关 [选填]:默认开启,关闭时不进行执行器初始化;
|
||||
*/
|
||||
@Getter
|
||||
@Value("${xxl.job.executor.enabled:false}")
|
||||
private Boolean enabled;
|
||||
|
||||
/**
|
||||
* 执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册
|
||||
*/
|
||||
@Value("${xxl.job.executor.appName}")
|
||||
private String appName;
|
||||
|
||||
/**
|
||||
* 执行器注册 [选填]:优先使用该配置作为注册地址,为空时使用内嵌服务 ”IP:PORT“ 作为注册地址.从而更灵活支持容器类型执行器动态IP和动态映射端口问题.
|
||||
*/
|
||||
@Value("${xxl.job.executor.address}")
|
||||
private String address;
|
||||
|
||||
/**
|
||||
* 执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯使用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务";
|
||||
*/
|
||||
@Value("${xxl.job.executor.ip}")
|
||||
private String ip;
|
||||
|
||||
/**
|
||||
* 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
|
||||
*/
|
||||
@Value("${xxl.job.executor.port}")
|
||||
private int port;
|
||||
|
||||
/**
|
||||
* 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
|
||||
*/
|
||||
@Value("${xxl.job.executor.logPath}")
|
||||
private String logPath;
|
||||
|
||||
/**
|
||||
* 执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能;
|
||||
*/
|
||||
@Value("${xxl.job.executor.logRetentionDays}")
|
||||
private int logRetentionDays;
|
||||
|
||||
/**
|
||||
* 任务扫描排除路径 [选填] :任务扫描时忽略指定包路径下的Bean;支持配置包路径前缀,多个逗号分隔;
|
||||
*/
|
||||
@Value("${xxl.job.executor.excludedPackage}")
|
||||
private String excludedPackage;
|
||||
|
||||
/**
|
||||
* xxl-job 执行器
|
||||
*
|
||||
* @return xxl-job 执行器
|
||||
*/
|
||||
@Bean
|
||||
public XxlJobSpringExecutor xxlJobExecutor() {
|
||||
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
|
||||
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
|
||||
xxlJobSpringExecutor.setAccessToken(accessToken);
|
||||
xxlJobSpringExecutor.setTimeout(timeout);
|
||||
xxlJobSpringExecutor.setEnabled(enabled);
|
||||
xxlJobSpringExecutor.setAppname(appName);
|
||||
if (StringUtils.isNotBlank(address)) {
|
||||
xxlJobSpringExecutor.setAddress(address);
|
||||
}
|
||||
if (StringUtils.isNotBlank(ip)) {
|
||||
xxlJobSpringExecutor.setIp(ip);
|
||||
}
|
||||
if (port > 0) {
|
||||
xxlJobSpringExecutor.setPort(port);
|
||||
}
|
||||
if (StringUtils.isNotBlank(logPath)) {
|
||||
xxlJobSpringExecutor.setLogPath(logPath);
|
||||
}
|
||||
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
|
||||
if (StringUtils.isNotBlank(excludedPackage)) {
|
||||
xxlJobSpringExecutor.setExcludedPackage(excludedPackage);
|
||||
}
|
||||
return xxlJobSpringExecutor;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package xtools.boot.job.xxl.init;
|
||||
|
||||
import com.xxl.job.core.executor.XxlJobExecutor;
|
||||
import com.xxl.job.core.handler.IJobHandler;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jspecify.annotations.NonNull;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xtools.base.config.BaseParams;
|
||||
import xtools.boot.core.interfaces.JobInterface;
|
||||
import xtools.boot.core.utils.SpringContextUtils;
|
||||
import xtools.boot.job.xxl.config.XxlJobConfig;
|
||||
import xtools.core.CollectionUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* <p>Title : InitIp</p>
|
||||
* <p>Description : InitIp</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/2/16 10:21
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Order(BaseParams.CP_NUM90)
|
||||
public class InitXxlJob implements ApplicationRunner, BaseParams {
|
||||
|
||||
private final XxlJobConfig xxlJobConfig;
|
||||
|
||||
@Override
|
||||
public void run(@NonNull ApplicationArguments args) {
|
||||
init();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
private void init() {
|
||||
Boolean enabled = xxlJobConfig.getEnabled();
|
||||
if (Objects.isNull(enabled) || !enabled) {
|
||||
log.info("xxl-job未启用");
|
||||
return;
|
||||
}
|
||||
Collection<JobInterface> beanList = SpringContextUtils.getBeanList(JobInterface.class);
|
||||
if (CollectionUtils.isEmpty(beanList)) {
|
||||
log.info("没有需要执行xxl-job的任务");
|
||||
return;
|
||||
}
|
||||
beanList.forEach(item -> {
|
||||
try {
|
||||
String name = item.getClass().getSimpleName();
|
||||
name = Character.toLowerCase(name.charAt(CP_NUM0)) + name.substring(CP_NUM1);
|
||||
registerXxlJob(name, item);
|
||||
log.info("注册xxl-job任务成功:{}", name);
|
||||
} catch (Exception e) {
|
||||
log.error("注册xxl-job任务异常", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册任务
|
||||
*
|
||||
* @param name 任务名称
|
||||
* @param job 任务
|
||||
*/
|
||||
private void registerXxlJob(String name, JobInterface job) {
|
||||
IJobHandler handler = new IJobHandler() {
|
||||
@Override
|
||||
public void execute() throws Exception {
|
||||
job.execute();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
job.init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
job.destroy();
|
||||
}
|
||||
};
|
||||
// 注册到XXL-JOB
|
||||
XxlJobExecutor.registryJobHandler(name, handler);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package xtools.boot.job.xxl.selector;
|
||||
|
||||
import org.jspecify.annotations.NonNull;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
/**
|
||||
* <p>Title : BootXxlJobImportSelector</p>
|
||||
* <p>Description : BootXxlJobImportSelector</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/01/01 09:30
|
||||
*/
|
||||
public class BootXxlJobImportSelector implements ImportBeanDefinitionRegistrar {
|
||||
|
||||
/**
|
||||
* 根据给定的注释元数据,根据需要注册bean
|
||||
*
|
||||
* @param importingClassMetadata AnnotationMetadata
|
||||
* @param registry BeanDefinitionRegistry
|
||||
*/
|
||||
@Override
|
||||
public void registerBeanDefinitions(@NonNull AnnotationMetadata importingClassMetadata, @NonNull BeanDefinitionRegistry registry) {
|
||||
// 构建扫描对象
|
||||
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, true);
|
||||
// 扫描包下路径
|
||||
scanner.scan("xtools.boot.job.xxl");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
xtools.boot.job.xxl.BootXxlJobConfiguration
|
||||
@@ -0,0 +1,26 @@
|
||||
xxl:
|
||||
job:
|
||||
admin:
|
||||
# 调度中心部署根地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔.执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;
|
||||
addresses: http://10.1.0.12:8080/xxl-job-admin
|
||||
# 调度中心通讯TOKEN [选填]:非空时启用;
|
||||
accessToken: xtools-app-xxl-job
|
||||
# 调度中心通讯超时时间[选填],单位秒;默认3s;
|
||||
timeout: 3
|
||||
executor:
|
||||
# 执行器启用开关 [选填]:默认开启,关闭时不进行执行器初始化;
|
||||
enabled: true
|
||||
# 执行器AppName [选填]:执行器心跳注册分组依据;为空则关闭自动注册
|
||||
appName: xtools-cloud
|
||||
# 执行器注册 [选填]:优先使用该配置作为注册地址,为空时使用内嵌服务 ”IP:PORT“ 作为注册地址.从而更灵活支持容器类型执行器动态IP和动态映射端口问题.
|
||||
address: ""
|
||||
# 执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯使用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务";
|
||||
ip: ""
|
||||
# 执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
|
||||
port: 9999
|
||||
# 执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
|
||||
logPath: /Users/xujun/Downloads/xtools
|
||||
# 执行器日志文件保存天数 [选填] : 过期日志自动清理, 限制值大于等于3时生效; 否则, 如-1, 关闭自动清理功能;
|
||||
logRetentionDays: 30
|
||||
# 任务扫描排除路径 [选填] :任务扫描时忽略指定包路径下的Bean;支持配置包路径前缀,多个逗号分隔;
|
||||
excludedPackage: ""
|
||||
Reference in New Issue
Block a user