-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(runner): 增加 AbstractRunner 类以方便扩展
1. 增加 AbstractRunner 类,springboot启动后会执行AbstractRunner子类的duRun方法 2. 重构以前的Running类以继承AbstractRunner
- Loading branch information
Showing
4 changed files
with
118 additions
and
27 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/main/java/com/xclhove/xnote/runner/AbstractRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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连接成功!"); | ||
} | ||
} |