Commit f4ad3e9d2d66ba93303ce3c7f70ff0b90527da87
1 parent
7bfa8306
【代码评审】IoT:插件体系
Showing
4 changed files
with
16 additions
and
19 deletions
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-common/src/main/java/cn/iocoder/yudao/module/iot/plugin/common/config/YudaoDeviceDataApiAutoConfiguration.java
| @@ -18,7 +18,6 @@ import java.time.Duration; | @@ -18,7 +18,6 @@ import java.time.Duration; | ||
| 18 | @AutoConfiguration | 18 | @AutoConfiguration |
| 19 | public class YudaoDeviceDataApiAutoConfiguration { | 19 | public class YudaoDeviceDataApiAutoConfiguration { |
| 20 | 20 | ||
| 21 | - | ||
| 22 | // TODO @haohao:这个要不搞个配置类哈 | 21 | // TODO @haohao:这个要不搞个配置类哈 |
| 23 | @Value("${iot.device-data.url}") | 22 | @Value("${iot.device-data.url}") |
| 24 | private String deviceDataUrl; | 23 | private String deviceDataUrl; |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/HttpPluginSpringbootApplication.java
| @@ -20,8 +20,7 @@ public class HttpPluginSpringbootApplication { | @@ -20,8 +20,7 @@ public class HttpPluginSpringbootApplication { | ||
| 20 | ConfigurableApplicationContext context = application.run(args); | 20 | ConfigurableApplicationContext context = application.run(args); |
| 21 | 21 | ||
| 22 | // 手动获取 VertxService 并启动 | 22 | // 手动获取 VertxService 并启动 |
| 23 | - // TODO @haohao:可以放在 bean 的 init 里么? | ||
| 24 | - // 会和插件模式冲突 | 23 | + // TODO @haohao:可以放在 bean 的 init 里么?回复:会和插件模式冲突 @芋艿,测试下 |
| 25 | VertxService vertxService = context.getBean(VertxService.class); | 24 | VertxService vertxService = context.getBean(VertxService.class); |
| 26 | vertxService.startServer(); | 25 | vertxService.startServer(); |
| 27 | 26 |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/config/HttpVertxPlugin.java
| 1 | package cn.iocoder.yudao.module.iot.plugin.http.config; | 1 | package cn.iocoder.yudao.module.iot.plugin.http.config; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.lang.Assert; | ||
| 3 | import cn.hutool.extra.spring.SpringUtil; | 4 | import cn.hutool.extra.spring.SpringUtil; |
| 4 | import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi; | 5 | import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi; |
| 5 | import lombok.extern.slf4j.Slf4j; | 6 | import lombok.extern.slf4j.Slf4j; |
| @@ -21,62 +22,60 @@ public class HttpVertxPlugin extends SpringPlugin { | @@ -21,62 +22,60 @@ public class HttpVertxPlugin extends SpringPlugin { | ||
| 21 | 22 | ||
| 22 | @Override | 23 | @Override |
| 23 | public void start() { | 24 | public void start() { |
| 24 | - log.info("[HttpVertxPlugin][start][begin] 开始启动 HttpVertxPlugin 插件..."); | ||
| 25 | - | 25 | + log.info("[HttpVertxPlugin][HttpVertxPlugin 插件启动开始...]"); |
| 26 | try { | 26 | try { |
| 27 | // 1. 获取插件上下文 | 27 | // 1. 获取插件上下文 |
| 28 | ApplicationContext pluginContext = getApplicationContext(); | 28 | ApplicationContext pluginContext = getApplicationContext(); |
| 29 | - if (pluginContext == null) { | ||
| 30 | - log.error("[HttpVertxPlugin][start][fail] pluginContext is null, 启动失败!"); | ||
| 31 | - return; | ||
| 32 | - } | 29 | + Assert.notNull(pluginContext, "pluginContext 不能为空"); |
| 33 | 30 | ||
| 34 | // 2. 启动 Vert.x | 31 | // 2. 启动 Vert.x |
| 35 | VertxService vertxService = pluginContext.getBean(VertxService.class); | 32 | VertxService vertxService = pluginContext.getBean(VertxService.class); |
| 36 | vertxService.startServer(); | 33 | vertxService.startServer(); |
| 37 | 34 | ||
| 38 | - log.info("[HttpVertxPlugin][start][end] 启动完成"); | 35 | + log.info("[HttpVertxPlugin][HttpVertxPlugin 插件启动成功...]"); |
| 39 | } catch (Exception e) { | 36 | } catch (Exception e) { |
| 40 | - log.error("[HttpVertxPlugin][start][exception] 启动过程出现异常!", e); | 37 | + log.error("[HttpVertxPlugin][HttpVertxPlugin 插件开启动异常...]", e); |
| 41 | } | 38 | } |
| 42 | } | 39 | } |
| 43 | 40 | ||
| 44 | @Override | 41 | @Override |
| 45 | public void stop() { | 42 | public void stop() { |
| 46 | - log.info("[HttpVertxPlugin][stop][begin] 开始停止 HttpVertxPlugin 插件..."); | ||
| 47 | - | 43 | + log.info("[HttpVertxPlugin][HttpVertxPlugin 插件停止开始...]"); |
| 48 | try { | 44 | try { |
| 45 | + // 停止服务器 | ||
| 49 | ApplicationContext pluginContext = getApplicationContext(); | 46 | ApplicationContext pluginContext = getApplicationContext(); |
| 50 | if (pluginContext != null) { | 47 | if (pluginContext != null) { |
| 51 | - // 停止服务器 | ||
| 52 | VertxService vertxService = pluginContext.getBean(VertxService.class); | 48 | VertxService vertxService = pluginContext.getBean(VertxService.class); |
| 53 | vertxService.stopServer(); | 49 | vertxService.stopServer(); |
| 54 | } | 50 | } |
| 55 | 51 | ||
| 56 | - log.info("[HttpVertxPlugin][stop][end] 停止完成"); | 52 | + log.info("[HttpVertxPlugin][HttpVertxPlugin 插件停止成功...]"); |
| 57 | } catch (Exception e) { | 53 | } catch (Exception e) { |
| 58 | - log.error("[HttpVertxPlugin][stop][exception] 停止过程出现异常!", e); | 54 | + log.error("[HttpVertxPlugin][HttpVertxPlugin 插件停止异常...]", e); |
| 59 | } | 55 | } |
| 60 | } | 56 | } |
| 61 | 57 | ||
| 62 | @Override | 58 | @Override |
| 63 | protected ApplicationContext createApplicationContext() { | 59 | protected ApplicationContext createApplicationContext() { |
| 60 | + // TODO @haohao:这个加 deviceDataApi 的目的是啥呀? | ||
| 64 | AnnotationConfigApplicationContext pluginContext = new AnnotationConfigApplicationContext() { | 61 | AnnotationConfigApplicationContext pluginContext = new AnnotationConfigApplicationContext() { |
| 62 | + | ||
| 65 | @Override | 63 | @Override |
| 66 | protected void prepareRefresh() { | 64 | protected void prepareRefresh() { |
| 67 | // 在刷新容器前注册主程序中的 Bean | 65 | // 在刷新容器前注册主程序中的 Bean |
| 68 | ConfigurableListableBeanFactory beanFactory = this.getBeanFactory(); | 66 | ConfigurableListableBeanFactory beanFactory = this.getBeanFactory(); |
| 69 | DeviceDataApi deviceDataApi = SpringUtil.getBean(DeviceDataApi.class); | 67 | DeviceDataApi deviceDataApi = SpringUtil.getBean(DeviceDataApi.class); |
| 70 | beanFactory.registerSingleton("deviceDataApi", deviceDataApi); | 68 | beanFactory.registerSingleton("deviceDataApi", deviceDataApi); |
| 71 | - | ||
| 72 | super.prepareRefresh(); | 69 | super.prepareRefresh(); |
| 73 | } | 70 | } |
| 71 | + | ||
| 74 | }; | 72 | }; |
| 75 | 73 | ||
| 76 | pluginContext.setClassLoader(getWrapper().getPluginClassLoader()); | 74 | pluginContext.setClassLoader(getWrapper().getPluginClassLoader()); |
| 75 | + // TODO @芋艿:枚举 | ||
| 77 | pluginContext.scan("cn.iocoder.yudao.module.iot.plugin.http.config"); | 76 | pluginContext.scan("cn.iocoder.yudao.module.iot.plugin.http.config"); |
| 78 | pluginContext.refresh(); | 77 | pluginContext.refresh(); |
| 79 | - | ||
| 80 | return pluginContext; | 78 | return pluginContext; |
| 81 | } | 79 | } |
| 80 | + | ||
| 82 | } | 81 | } |
| 83 | \ No newline at end of file | 82 | \ No newline at end of file |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/resources/application.yml