【代码评审】IoT:插件体系
This commit is contained in:
parent
7bfa830628
commit
f4ad3e9d2d
|
@ -18,7 +18,6 @@ import java.time.Duration;
|
||||||
@AutoConfiguration
|
@AutoConfiguration
|
||||||
public class YudaoDeviceDataApiAutoConfiguration {
|
public class YudaoDeviceDataApiAutoConfiguration {
|
||||||
|
|
||||||
|
|
||||||
// TODO @haohao:这个要不搞个配置类哈
|
// TODO @haohao:这个要不搞个配置类哈
|
||||||
@Value("${iot.device-data.url}")
|
@Value("${iot.device-data.url}")
|
||||||
private String deviceDataUrl;
|
private String deviceDataUrl;
|
||||||
|
|
|
@ -20,8 +20,7 @@ public class HttpPluginSpringbootApplication {
|
||||||
ConfigurableApplicationContext context = application.run(args);
|
ConfigurableApplicationContext context = application.run(args);
|
||||||
|
|
||||||
// 手动获取 VertxService 并启动
|
// 手动获取 VertxService 并启动
|
||||||
// TODO @haohao:可以放在 bean 的 init 里么?
|
// TODO @haohao:可以放在 bean 的 init 里么?回复:会和插件模式冲突 @芋艿,测试下
|
||||||
// 会和插件模式冲突
|
|
||||||
VertxService vertxService = context.getBean(VertxService.class);
|
VertxService vertxService = context.getBean(VertxService.class);
|
||||||
vertxService.startServer();
|
vertxService.startServer();
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package cn.iocoder.yudao.module.iot.plugin.http.config;
|
package cn.iocoder.yudao.module.iot.plugin.http.config;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.extra.spring.SpringUtil;
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi;
|
import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -21,62 +22,60 @@ public class HttpVertxPlugin extends SpringPlugin {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
log.info("[HttpVertxPlugin][start][begin] 开始启动 HttpVertxPlugin 插件...");
|
log.info("[HttpVertxPlugin][HttpVertxPlugin 插件启动开始...]");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 1. 获取插件上下文
|
// 1. 获取插件上下文
|
||||||
ApplicationContext pluginContext = getApplicationContext();
|
ApplicationContext pluginContext = getApplicationContext();
|
||||||
if (pluginContext == null) {
|
Assert.notNull(pluginContext, "pluginContext 不能为空");
|
||||||
log.error("[HttpVertxPlugin][start][fail] pluginContext is null, 启动失败!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. 启动 Vert.x
|
// 2. 启动 Vert.x
|
||||||
VertxService vertxService = pluginContext.getBean(VertxService.class);
|
VertxService vertxService = pluginContext.getBean(VertxService.class);
|
||||||
vertxService.startServer();
|
vertxService.startServer();
|
||||||
|
|
||||||
log.info("[HttpVertxPlugin][start][end] 启动完成");
|
log.info("[HttpVertxPlugin][HttpVertxPlugin 插件启动成功...]");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("[HttpVertxPlugin][start][exception] 启动过程出现异常!", e);
|
log.error("[HttpVertxPlugin][HttpVertxPlugin 插件开启动异常...]", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
log.info("[HttpVertxPlugin][stop][begin] 开始停止 HttpVertxPlugin 插件...");
|
log.info("[HttpVertxPlugin][HttpVertxPlugin 插件停止开始...]");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// 停止服务器
|
||||||
ApplicationContext pluginContext = getApplicationContext();
|
ApplicationContext pluginContext = getApplicationContext();
|
||||||
if (pluginContext != null) {
|
if (pluginContext != null) {
|
||||||
// 停止服务器
|
|
||||||
VertxService vertxService = pluginContext.getBean(VertxService.class);
|
VertxService vertxService = pluginContext.getBean(VertxService.class);
|
||||||
vertxService.stopServer();
|
vertxService.stopServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("[HttpVertxPlugin][stop][end] 停止完成");
|
log.info("[HttpVertxPlugin][HttpVertxPlugin 插件停止成功...]");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("[HttpVertxPlugin][stop][exception] 停止过程出现异常!", e);
|
log.error("[HttpVertxPlugin][HttpVertxPlugin 插件停止异常...]", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ApplicationContext createApplicationContext() {
|
protected ApplicationContext createApplicationContext() {
|
||||||
|
// TODO @haohao:这个加 deviceDataApi 的目的是啥呀?
|
||||||
AnnotationConfigApplicationContext pluginContext = new AnnotationConfigApplicationContext() {
|
AnnotationConfigApplicationContext pluginContext = new AnnotationConfigApplicationContext() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void prepareRefresh() {
|
protected void prepareRefresh() {
|
||||||
// 在刷新容器前注册主程序中的 Bean
|
// 在刷新容器前注册主程序中的 Bean
|
||||||
ConfigurableListableBeanFactory beanFactory = this.getBeanFactory();
|
ConfigurableListableBeanFactory beanFactory = this.getBeanFactory();
|
||||||
DeviceDataApi deviceDataApi = SpringUtil.getBean(DeviceDataApi.class);
|
DeviceDataApi deviceDataApi = SpringUtil.getBean(DeviceDataApi.class);
|
||||||
beanFactory.registerSingleton("deviceDataApi", deviceDataApi);
|
beanFactory.registerSingleton("deviceDataApi", deviceDataApi);
|
||||||
|
|
||||||
super.prepareRefresh();
|
super.prepareRefresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pluginContext.setClassLoader(getWrapper().getPluginClassLoader());
|
pluginContext.setClassLoader(getWrapper().getPluginClassLoader());
|
||||||
|
// TODO @芋艿:枚举
|
||||||
pluginContext.scan("cn.iocoder.yudao.module.iot.plugin.http.config");
|
pluginContext.scan("cn.iocoder.yudao.module.iot.plugin.http.config");
|
||||||
pluginContext.refresh();
|
pluginContext.refresh();
|
||||||
|
|
||||||
return pluginContext;
|
return pluginContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue