feat(job): 集成XXL-JOB分布式定时任务
- 添加XXL-JOB核心依赖 - 创建任务配置类XxlJobConfig - 定义任务属性类XxlJobProperties - 新增创建表任务示例CreateTableXxlJob - 实现任务执行器初始化逻辑 - 配置任务日志路径及保留天数 - 注册任务组件到Spring容器 - 添加任务处理器注解及日志记录
This commit is contained in:
@@ -77,6 +77,12 @@
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- XXL-JOB 分布式定时任务调度 -->
|
||||
<dependency>
|
||||
<groupId>com.xuxueli</groupId>
|
||||
<artifactId>xxl-job-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.hanserwei.hannote.data.align.config;
|
||||
|
||||
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class XxlJobConfig {
|
||||
|
||||
@Resource
|
||||
private XxlJobProperties xxlJobProperties;
|
||||
|
||||
/**
|
||||
* 初始化执行器
|
||||
*
|
||||
* @return xxlJobSpringExecutor
|
||||
*/
|
||||
@Bean
|
||||
public XxlJobSpringExecutor xxlJobExecutor() {
|
||||
log.info(">>>>>>>>>>> xxl-job config init.");
|
||||
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
|
||||
xxlJobSpringExecutor.setAdminAddresses(xxlJobProperties.getAdminAddresses());
|
||||
xxlJobSpringExecutor.setAppname(xxlJobProperties.getAppName());
|
||||
xxlJobSpringExecutor.setIp(xxlJobProperties.getIp());
|
||||
xxlJobSpringExecutor.setPort(xxlJobProperties.getPort());
|
||||
xxlJobSpringExecutor.setAccessToken(xxlJobProperties.getAccessToken());
|
||||
xxlJobSpringExecutor.setLogPath(xxlJobProperties.getLogPath());
|
||||
xxlJobSpringExecutor.setLogRetentionDays(xxlJobProperties.getLogRetentionDays());
|
||||
return xxlJobSpringExecutor;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.hanserwei.hannote.data.align.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@ConfigurationProperties(prefix = XxlJobProperties.PREFIX)
|
||||
@Component
|
||||
@Data
|
||||
public class XxlJobProperties {
|
||||
|
||||
public static final String PREFIX = "xxl.job";
|
||||
|
||||
private String adminAddresses;
|
||||
|
||||
private String accessToken;
|
||||
|
||||
private String appName;
|
||||
|
||||
private String ip;
|
||||
|
||||
private int port;
|
||||
|
||||
private String logPath;
|
||||
|
||||
private int logRetentionDays = 30;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.hanserwei.hannote.data.align.job;
|
||||
|
||||
import com.xxl.job.core.context.XxlJobHelper;
|
||||
import com.xxl.job.core.handler.annotation.XxlJob;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CreateTableXxlJob {
|
||||
|
||||
/**
|
||||
* 1、简单任务示例(Bean模式)
|
||||
*/
|
||||
@XxlJob("createTableJobHandler")
|
||||
public void createTableJobHandler() throws Exception {
|
||||
XxlJobHelper.log("## 开始初始化明日增量数据表...");
|
||||
|
||||
// TODO
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user