Skip to content

Commit

Permalink
refactor(runner): 增加 AbstractRunner 类以方便扩展
Browse files Browse the repository at this point in the history
1. 增加 AbstractRunner 类,springboot启动后会执行AbstractRunner子类的duRun方法
2. 重构以前的Running类以继承AbstractRunner
  • Loading branch information
XCLHove committed Jun 16, 2024
1 parent 896496e commit 2f431a5
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 27 deletions.
44 changes: 44 additions & 0 deletions src/main/java/com/xclhove/xnote/runner/AbstractRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.xclhove.xnote.runner;

import com.xclhove.xnote.config.RunnerConfig;
import com.xclhove.xnote.util.SubclassFinder;
import lombok.Data;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;

/**
* @author xclhove
*/
@Component
@Data
public abstract class AbstractRunner implements ApplicationRunner {
@Resource
private final ApplicationContext applicationContext;
@Resource
protected RunnerConfig runnerConfig;

@Override
public void run(ApplicationArguments args) {
// 获取AbstractRunner的子类
Set<BeanDefinition> subclasses = SubclassFinder.findSubclasses(AbstractRunner.class);

// 执行AbstractRunner子类的doRun方法
subclasses.forEach(beanDefinition -> {
try {
Class<?> aClass = Class.forName(beanDefinition.getBeanClassName());
AbstractRunner runner = (AbstractRunner) applicationContext.getBean(aClass);
runner.doRun(args);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}

public abstract void doRun(ApplicationArguments args);
}
25 changes: 17 additions & 8 deletions src/main/java/com/xclhove/xnote/runner/MinioRunner.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
package com.xclhove.xnote.runner;

import com.xclhove.xnote.config.MinioConfig;
import com.xclhove.xnote.tool.MinioTool;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
* 启动时检查minio是否连接成功
*
* @author xclhove
*/
@Component
@RequiredArgsConstructor
@Slf4j
public class MinioRunner implements ApplicationRunner {
private final MinioTool minioTool;
public class MinioRunner extends AbstractRunner {
@Resource
private MinioTool minioTool;

public MinioRunner(ApplicationContext applicationContext) {
super(applicationContext);
}

@Override
public void run(ApplicationArguments args) {
public void doRun(ApplicationArguments args) {
checkMinio();
}

/**
* 检查minio是否连接成功
*/
public void checkMinio() {
private void checkMinio() {
if (!runnerConfig.getEnableMinioStatusCheckRunner()) {
return;
}

try {
boolean bucketExist = minioTool.bucketExist();
if (bucketExist) {
log.info("minio连接成功!");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import com.xclhove.xnote.entity.attribute.NoteKeyword;
import com.xclhove.xnote.entity.table.Note;
import com.xclhove.xnote.service.NoteService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -21,20 +21,29 @@
* @author xclhove
*/
@Component
@RequiredArgsConstructor
@Slf4j
public class XnoteRunner implements ApplicationRunner {
private final NoteService noteService;
@Value("${xnote.keywords.convert-enable: false}")
private boolean enableKeywordsConvert;
@Order
public class NoteRunner extends AbstractRunner {
@Resource
private NoteService noteService;

public NoteRunner(ApplicationContext applicationContext) {
super(applicationContext);
}

@Override
public void run(ApplicationArguments args) {
public void doRun(ApplicationArguments args) {
convertKeywords();
replaceImageUrl();
}

/**
* 转换关键词的存储结构
*/
private void convertKeywords() {
if (!enableKeywordsConvert) return;
if (!runnerConfig.getEnableKeywordsConvertRunner()) {
return;
}

try {
log.info("关键词转化……");
Expand Down Expand Up @@ -68,8 +77,24 @@ private void convertKeywords() {
log.info("关键词转化已经完成!");
System.exit(0);
} catch (Exception e) {
log.info("无需转化!");
log.info("关键词无需转化!");
}
}

/**
* 替换笔记中的图片地址
*/
private void replaceImageUrl() {
if (!runnerConfig.getEnableImageUrlReplaceRunner()) {
return;
}

List<Note> list = new ArrayList<>();
noteService.list().forEach(note -> {
note.setContent(note.getContent().replaceAll("xnote.xclhove.top/api", "api.xclhove.top/xnote"));
list.add(note);
});
noteService.updateBatchById(list);
log.info("替换图片地址成功!");
}
}
29 changes: 21 additions & 8 deletions src/main/java/com/xclhove/xnote/runner/RedisRunner.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
package com.xclhove.xnote.runner;

import com.xclhove.xnote.tool.RedisTool;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
* 启动时检查redis是否连接成功
*
* @author xclhove
*/
@Component
@RequiredArgsConstructor
@Slf4j
public class RedisRunner implements ApplicationRunner {
private final RedisTool redisTool;
@Order(1)
public class RedisRunner extends AbstractRunner {
@Resource
private RedisTool redisTool;

public RedisRunner(ApplicationContext applicationContext) {
super(applicationContext);
}

@Override
public void run(ApplicationArguments args) {
checkRedis();
public void doRun(ApplicationArguments args) {
checkRedisStatus();
}

/**
* 检查redis是否连接成功
*/
public void checkRedis() {
private void checkRedisStatus() {
if (!runnerConfig.getEnableRedisStatusCheckRunner()) {
return;
}

if (!redisTool.connected()) {
log.error("redis连接失败!请检查配置文件和redis是否启动。");
System.exit(1);
return;
}
log.info("redis连接成功!");
}
}

0 comments on commit 2f431a5

Please sign in to comment.