Commit 3ab7ad484a2be9ad9d632ea8147d2ba30814d077
1 parent
ec71cd94
【功能完善】IoT: 增强插件启动和停止逻辑,添加异常处理,更新错误码,优化配置文件
Showing
5 changed files
with
56 additions
and
31 deletions
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/ErrorCodeConstants.java
| ... | ... | @@ -46,6 +46,8 @@ public interface ErrorCodeConstants { |
| 46 | 46 | ErrorCode PLUGIN_CONFIG_DELETE_FAILED_RUNNING = new ErrorCode(1_050_006_003, "请先停止插件"); |
| 47 | 47 | ErrorCode PLUGIN_STATUS_INVALID = new ErrorCode(1_050_006_004, "插件状态无效"); |
| 48 | 48 | ErrorCode PLUGIN_CONFIG_KEY_DUPLICATE = new ErrorCode(1_050_006_005, "插件标识已存在"); |
| 49 | + ErrorCode PLUGIN_START_FAILED = new ErrorCode(1_050_006_006, "插件启动失败"); | |
| 50 | + ErrorCode PLUGIN_STOP_FAILED = new ErrorCode(1_050_006_007, "插件停止失败"); | |
| 49 | 51 | |
| 50 | 52 | // ========== 插件实例 1-050-007-000 ========== |
| 51 | 53 | ... | ... |
yudao-module-iot/yudao-module-iot-biz/pom.xml
| ... | ... | @@ -65,16 +65,16 @@ |
| 65 | 65 | </dependency> |
| 66 | 66 | |
| 67 | 67 | <!-- TODO @haohao:貌似不需要这个? --> |
| 68 | - <dependency> | |
| 69 | - <groupId>io.vertx</groupId> | |
| 70 | - <artifactId>vertx-web</artifactId> | |
| 71 | - </dependency> | |
| 68 | +<!-- <dependency>--> | |
| 69 | +<!-- <groupId>io.vertx</groupId>--> | |
| 70 | +<!-- <artifactId>vertx-web</artifactId>--> | |
| 71 | +<!-- </dependency>--> | |
| 72 | 72 | |
| 73 | 73 | <!-- TODO @haohao:貌似 biz 模块,不需要 MQTT? --> |
| 74 | - <dependency> | |
| 75 | - <groupId>org.eclipse.paho</groupId> <!-- MQTT --> | |
| 76 | - <artifactId>org.eclipse.paho.client.mqttv3</artifactId> | |
| 77 | - </dependency> | |
| 74 | +<!-- <dependency>--> | |
| 75 | +<!-- <groupId>org.eclipse.paho</groupId> <!– MQTT –>--> | |
| 76 | +<!-- <artifactId>org.eclipse.paho.client.mqttv3</artifactId>--> | |
| 77 | +<!-- </dependency>--> | |
| 78 | 78 | |
| 79 | 79 | <dependency> |
| 80 | 80 | <groupId>org.pf4j</groupId> <!-- PF4J:内置插件机制 --> | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/plugin/IotPluginInstanceServiceImpl.java
| ... | ... | @@ -191,13 +191,23 @@ public class IotPluginInstanceServiceImpl implements IotPluginInstanceService { |
| 191 | 191 | // 启动插件 |
| 192 | 192 | if (status.equals(IotPluginStatusEnum.RUNNING.getStatus()) |
| 193 | 193 | && plugin.getPluginState() != PluginState.STARTED) { |
| 194 | - pluginManager.startPlugin(pluginKey); | |
| 194 | + try { | |
| 195 | + pluginManager.startPlugin(pluginKey); | |
| 196 | + } catch (Exception e) { | |
| 197 | + log.error("[updatePluginStatus][启动插件({}) 失败]", pluginKey, e); | |
| 198 | + throw exception(ErrorCodeConstants.PLUGIN_START_FAILED, e); | |
| 199 | + } | |
| 195 | 200 | log.info("已启动插件: {}", pluginKey); |
| 196 | 201 | } |
| 197 | 202 | // 停止插件 |
| 198 | 203 | else if (status.equals(IotPluginStatusEnum.STOPPED.getStatus()) |
| 199 | 204 | && plugin.getPluginState() == PluginState.STARTED) { |
| 200 | - pluginManager.stopPlugin(pluginKey); | |
| 205 | + try { | |
| 206 | + pluginManager.stopPlugin(pluginKey); | |
| 207 | + } catch (Exception e) { | |
| 208 | + log.error("[updatePluginStatus][停止插件({}) 失败]", pluginKey, e); | |
| 209 | + throw exception(ErrorCodeConstants.PLUGIN_STOP_FAILED, e); | |
| 210 | + } | |
| 201 | 211 | log.info("已停止插件: {}", pluginKey); |
| 202 | 212 | } |
| 203 | 213 | } | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/pom.xml
| ... | ... | @@ -94,6 +94,34 @@ |
| 94 | 94 | </archive> |
| 95 | 95 | </configuration> |
| 96 | 96 | </plugin> |
| 97 | +<!-- <plugin>--> | |
| 98 | +<!-- <groupId>org.apache.maven.plugins</groupId>--> | |
| 99 | +<!-- <artifactId>maven-shade-plugin</artifactId>--> | |
| 100 | +<!-- <version>3.6.0</version>--> | |
| 101 | +<!-- <executions>--> | |
| 102 | +<!-- <execution>--> | |
| 103 | +<!-- <phase>package</phase>--> | |
| 104 | +<!-- <goals>--> | |
| 105 | +<!-- <goal>shade</goal>--> | |
| 106 | +<!-- </goals>--> | |
| 107 | +<!-- <configuration>--> | |
| 108 | +<!-- <minimizeJar>true</minimizeJar>--> | |
| 109 | +<!-- </configuration>--> | |
| 110 | +<!-- </execution>--> | |
| 111 | +<!-- </executions>--> | |
| 112 | +<!-- <configuration>--> | |
| 113 | +<!-- <archive>--> | |
| 114 | +<!-- <manifestEntries>--> | |
| 115 | +<!-- <Plugin-Id>${plugin.id}</Plugin-Id>--> | |
| 116 | +<!-- <Plugin-Class>${plugin.class}</Plugin-Class>--> | |
| 117 | +<!-- <Plugin-Version>${plugin.version}</Plugin-Version>--> | |
| 118 | +<!-- <Plugin-Provider>${plugin.provider}</Plugin-Provider>--> | |
| 119 | +<!-- <Plugin-Description>${plugin.description}</Plugin-Description>--> | |
| 120 | +<!-- <Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>--> | |
| 121 | +<!-- </manifestEntries>--> | |
| 122 | +<!-- </archive>--> | |
| 123 | +<!-- </configuration>--> | |
| 124 | +<!-- </plugin>--> | |
| 97 | 125 | |
| 98 | 126 | <!-- 独立模式 --> |
| 99 | 127 | <plugin> |
| ... | ... | @@ -112,12 +140,6 @@ |
| 112 | 140 | </executions> |
| 113 | 141 | </plugin> |
| 114 | 142 | |
| 115 | - <plugin> | |
| 116 | - <artifactId>maven-deploy-plugin</artifactId> | |
| 117 | - <configuration> | |
| 118 | - <skip>true</skip> | |
| 119 | - </configuration> | |
| 120 | - </plugin> | |
| 121 | 143 | </plugins> |
| 122 | 144 | </build> |
| 123 | 145 | ... | ... |
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/src/main/java/cn/iocoder/yudao/module/iot/plugin/http/config/IotHttpVertxPlugin.java
| ... | ... | @@ -59,22 +59,13 @@ public class IotHttpVertxPlugin extends SpringPlugin { |
| 59 | 59 | |
| 60 | 60 | @Override |
| 61 | 61 | protected ApplicationContext createApplicationContext() { |
| 62 | - // TODO @haohao:这个加 deviceDataApi 的目的是啥呀? | |
| 63 | - AnnotationConfigApplicationContext pluginContext = new AnnotationConfigApplicationContext() { | |
| 64 | - | |
| 65 | - @Override | |
| 66 | - protected void prepareRefresh() { | |
| 67 | - // 在刷新容器前注册主程序中的 Bean | |
| 68 | - ConfigurableListableBeanFactory beanFactory = this.getBeanFactory(); | |
| 69 | - IotDeviceUpstreamApi deviceDataApi = SpringUtil.getBean(IotDeviceUpstreamApi.class); | |
| 70 | - beanFactory.registerSingleton("deviceDataApi", deviceDataApi); | |
| 71 | - super.prepareRefresh(); | |
| 72 | - } | |
| 73 | - | |
| 74 | - }; | |
| 75 | - | |
| 62 | + // 创建插件自己的 ApplicationContext | |
| 63 | + AnnotationConfigApplicationContext pluginContext = new AnnotationConfigApplicationContext(); | |
| 64 | + // 设置父容器为主应用的 ApplicationContext (确保主应用中提供的类可用) | |
| 65 | + pluginContext.setParent(SpringUtil.getApplicationContext()); | |
| 66 | + // 继续使用插件自己的 ClassLoader 以加载插件内部的类 | |
| 76 | 67 | pluginContext.setClassLoader(getWrapper().getPluginClassLoader()); |
| 77 | - // TODO @芋艿:枚举 | |
| 68 | + // 扫描当前插件的自动配置包 | |
| 78 | 69 | pluginContext.scan("cn.iocoder.yudao.module.iot.plugin.http.config"); |
| 79 | 70 | pluginContext.refresh(); |
| 80 | 71 | return pluginContext; | ... | ... |