feat(集成xxljob): 集成xxljob

This commit is contained in:
ovo 2024-12-24 17:04:39 +08:00
parent f6651db2b8
commit ce9823c369
4 changed files with 134 additions and 9 deletions

View File

@ -329,6 +329,13 @@
<version>4.1.2</version>
</dependency>
<!--xxljob 许雪里 job-->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,40 @@
package com.guwan.backend.config;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class XxlJobConfig {
@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() {
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;
}
}

View File

@ -0,0 +1,69 @@
package com.guwan.backend.xxljob.job;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.stats.CacheStats;
import com.guwan.backend.common.BusinessException;
import com.guwan.backend.constant.CacheConstants;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Component
@RequiredArgsConstructor
public class SimpleXxlJob {
private final CacheManager cacheManager;
@XxlJob("demoGuwanJobHandler")
public void demoGuwanJobHandler() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("执行定时任务,执行时间:"+sdf.format(new Date()));
for (String cache : CacheConstants.CACHE_LIST) {
Map<String, Object> stats = getStats(cache);
for (Map.Entry<String, Object> entry : stats.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
log.info("缓存使用情况{}",stats);
}
}
public Map<String, Object> getStats(String cacheName) {
CaffeineCacheManager caffeineCacheManager = (CaffeineCacheManager) cacheManager;
Cache<Object, Object> nativeCache = null;
try {
nativeCache = (Cache<Object, Object>)
caffeineCacheManager.getCache(cacheName).getNativeCache();
} catch (Exception e) {
// throw new RuntimeException(e);
throw new BusinessException("没有此本地缓存");
}
CacheStats stats = nativeCache.stats();
Map<String, Object> result = new HashMap<>();
result.put("hitCount 命中次数(成功从缓存中获取数据的次数)", stats.hitCount());
result.put("missCount 未命中次数(缓存中没有找到数据的次数)", stats.missCount());
result.put("loadCount 加载次数(需要从数据源重新加载数据的次数)", stats.loadCount());
result.put("evictionCount 驱逐次数(由于空间限制而被清除的缓存项数量)", stats.evictionCount());
result.put("hitRate 命中率", String.format("%.2f%%", stats.hitRate() * 100));
return result;
}
}

View File

@ -19,6 +19,7 @@ spring:
multipart:
max-file-size: 500MB
max-request-size: 500MB
# 数据库配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
@ -70,8 +71,6 @@ spring:
profiles:
active: dev # 确保使用开发环境配置
# MyBatis-Plus配置
mybatis-plus:
global-config:
@ -107,10 +106,6 @@ file:
upload:
path: D:/upload # Windows路径示例根据实际情况修改
# Swagger配置
springdoc:
swagger-ui:
@ -191,8 +186,6 @@ mongo-plus:
authenticationDatabase: admin #验证数据库
connectTimeoutMS: 50000 #在超时之前等待连接打开的最长时间(以毫秒为单位)
# 已有配置...
# 日志配置
logging:
# 日志文件路径
@ -212,4 +205,20 @@ logging:
file: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n"
logstash:
host: localhost
port: 5044
port: 5044
xxl:
job:
admin:
addresses: http://192.168.1.127:9001/xxl-job-admin
accessToken: GuwanTest
executor:
appname: xxl-job-executor-guwan
address: ""
ip:
port: 9999
logpath: logs/xxljob-logs
logretentiondays: 7