Browse Source

【功能完善】IoT: 增强插件启动和停止逻辑,添加异常处理,更新错误码,优化配置文件

安浩浩 6 tháng trước cách đây
mục cha
commit
3ab7ad484a

+ 2 - 0
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 {
     ErrorCode PLUGIN_CONFIG_DELETE_FAILED_RUNNING = new ErrorCode(1_050_006_003, "请先停止插件");
     ErrorCode PLUGIN_STATUS_INVALID = new ErrorCode(1_050_006_004, "插件状态无效");
     ErrorCode PLUGIN_CONFIG_KEY_DUPLICATE = new ErrorCode(1_050_006_005, "插件标识已存在");
+    ErrorCode PLUGIN_START_FAILED = new ErrorCode(1_050_006_006, "插件启动失败");
+    ErrorCode PLUGIN_STOP_FAILED = new ErrorCode(1_050_006_007, "插件停止失败");
 
     // ========== 插件实例 1-050-007-000 ==========
 

+ 8 - 8
yudao-module-iot/yudao-module-iot-biz/pom.xml

@@ -65,16 +65,16 @@
         </dependency>
 
         <!-- TODO @haohao:貌似不需要这个? -->
-        <dependency>
-            <groupId>io.vertx</groupId>
-            <artifactId>vertx-web</artifactId>
-        </dependency>
+<!--        <dependency>-->
+<!--            <groupId>io.vertx</groupId>-->
+<!--            <artifactId>vertx-web</artifactId>-->
+<!--        </dependency>-->
 
         <!-- TODO @haohao:貌似 biz 模块,不需要 MQTT? -->
-        <dependency>
-            <groupId>org.eclipse.paho</groupId> <!-- MQTT -->
-            <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
-        </dependency>
+<!--        <dependency>-->
+<!--            <groupId>org.eclipse.paho</groupId> &lt;!&ndash; MQTT &ndash;&gt;-->
+<!--            <artifactId>org.eclipse.paho.client.mqttv3</artifactId>-->
+<!--        </dependency>-->
 
         <dependency>
             <groupId>org.pf4j</groupId>  <!-- PF4J:内置插件机制 -->

+ 12 - 2
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 {
         // 启动插件
         if (status.equals(IotPluginStatusEnum.RUNNING.getStatus())
                 && plugin.getPluginState() != PluginState.STARTED) {
-            pluginManager.startPlugin(pluginKey);
+            try {
+                pluginManager.startPlugin(pluginKey);
+            } catch (Exception e) {
+                log.error("[updatePluginStatus][启动插件({}) 失败]", pluginKey, e);
+                throw exception(ErrorCodeConstants.PLUGIN_START_FAILED, e);
+            }
             log.info("已启动插件: {}", pluginKey);
         }
         // 停止插件
         else if (status.equals(IotPluginStatusEnum.STOPPED.getStatus())
                 && plugin.getPluginState() == PluginState.STARTED) {
-            pluginManager.stopPlugin(pluginKey);
+            try {
+                pluginManager.stopPlugin(pluginKey);
+            } catch (Exception e) {
+                log.error("[updatePluginStatus][停止插件({}) 失败]", pluginKey, e);
+                throw exception(ErrorCodeConstants.PLUGIN_STOP_FAILED, e);
+            }
             log.info("已停止插件: {}", pluginKey);
         }
     }

+ 28 - 6
yudao-module-iot/yudao-module-iot-plugins/yudao-module-iot-plugin-http/pom.xml

@@ -94,6 +94,34 @@
                     </archive>
                 </configuration>
             </plugin>
+<!--            <plugin>-->
+<!--                <groupId>org.apache.maven.plugins</groupId>-->
+<!--                <artifactId>maven-shade-plugin</artifactId>-->
+<!--                <version>3.6.0</version>-->
+<!--                <executions>-->
+<!--                    <execution>-->
+<!--                        <phase>package</phase>-->
+<!--                        <goals>-->
+<!--                            <goal>shade</goal>-->
+<!--                        </goals>-->
+<!--                        <configuration>-->
+<!--                            <minimizeJar>true</minimizeJar>-->
+<!--                        </configuration>-->
+<!--                    </execution>-->
+<!--                </executions>-->
+<!--                <configuration>-->
+<!--                    <archive>-->
+<!--                        <manifestEntries>-->
+<!--                            <Plugin-Id>${plugin.id}</Plugin-Id>-->
+<!--                            <Plugin-Class>${plugin.class}</Plugin-Class>-->
+<!--                            <Plugin-Version>${plugin.version}</Plugin-Version>-->
+<!--                            <Plugin-Provider>${plugin.provider}</Plugin-Provider>-->
+<!--                            <Plugin-Description>${plugin.description}</Plugin-Description>-->
+<!--                            <Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>-->
+<!--                        </manifestEntries>-->
+<!--                    </archive>-->
+<!--                </configuration>-->
+<!--            </plugin>-->
 
             <!-- 独立模式 -->
             <plugin>
@@ -112,12 +140,6 @@
                 </executions>
             </plugin>
 
-            <plugin>
-                <artifactId>maven-deploy-plugin</artifactId>
-                <configuration>
-                    <skip>true</skip>
-                </configuration>
-            </plugin>
         </plugins>
     </build>
 

+ 6 - 15
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 {
 
     @Override
     protected ApplicationContext createApplicationContext() {
-        // TODO @haohao:这个加 deviceDataApi 的目的是啥呀?
-        AnnotationConfigApplicationContext pluginContext = new AnnotationConfigApplicationContext() {
-
-            @Override
-            protected void prepareRefresh() {
-                // 在刷新容器前注册主程序中的 Bean
-                ConfigurableListableBeanFactory beanFactory = this.getBeanFactory();
-                IotDeviceUpstreamApi deviceDataApi = SpringUtil.getBean(IotDeviceUpstreamApi.class);
-                beanFactory.registerSingleton("deviceDataApi", deviceDataApi);
-                super.prepareRefresh();
-            }
-
-        };
-
+        // 创建插件自己的 ApplicationContext
+        AnnotationConfigApplicationContext pluginContext = new AnnotationConfigApplicationContext();
+        // 设置父容器为主应用的 ApplicationContext (确保主应用中提供的类可用)
+        pluginContext.setParent(SpringUtil.getApplicationContext());
+        // 继续使用插件自己的 ClassLoader 以加载插件内部的类
         pluginContext.setClassLoader(getWrapper().getPluginClassLoader());
-        // TODO @芋艿:枚举
+        // 扫描当前插件的自动配置包
         pluginContext.scan("cn.iocoder.yudao.module.iot.plugin.http.config");
         pluginContext.refresh();
         return pluginContext;