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

This commit is contained in:
安浩浩 2025-02-14 09:34:25 +08:00
parent ec71cd94e8
commit 3ab7ad484a
5 changed files with 56 additions and 31 deletions

View File

@ -46,6 +46,8 @@ public interface ErrorCodeConstants {
ErrorCode PLUGIN_CONFIG_DELETE_FAILED_RUNNING = new ErrorCode(1_050_006_003, "请先停止插件"); ErrorCode PLUGIN_CONFIG_DELETE_FAILED_RUNNING = new ErrorCode(1_050_006_003, "请先停止插件");
ErrorCode PLUGIN_STATUS_INVALID = new ErrorCode(1_050_006_004, "插件状态无效"); ErrorCode PLUGIN_STATUS_INVALID = new ErrorCode(1_050_006_004, "插件状态无效");
ErrorCode PLUGIN_CONFIG_KEY_DUPLICATE = new ErrorCode(1_050_006_005, "插件标识已存在"); 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 ========== // ========== 插件实例 1-050-007-000 ==========

View File

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

View File

@ -191,13 +191,23 @@ public class IotPluginInstanceServiceImpl implements IotPluginInstanceService {
// 启动插件 // 启动插件
if (status.equals(IotPluginStatusEnum.RUNNING.getStatus()) if (status.equals(IotPluginStatusEnum.RUNNING.getStatus())
&& plugin.getPluginState() != PluginState.STARTED) { && plugin.getPluginState() != PluginState.STARTED) {
try {
pluginManager.startPlugin(pluginKey); pluginManager.startPlugin(pluginKey);
} catch (Exception e) {
log.error("[updatePluginStatus][启动插件({}) 失败]", pluginKey, e);
throw exception(ErrorCodeConstants.PLUGIN_START_FAILED, e);
}
log.info("已启动插件: {}", pluginKey); log.info("已启动插件: {}", pluginKey);
} }
// 停止插件 // 停止插件
else if (status.equals(IotPluginStatusEnum.STOPPED.getStatus()) else if (status.equals(IotPluginStatusEnum.STOPPED.getStatus())
&& plugin.getPluginState() == PluginState.STARTED) { && plugin.getPluginState() == PluginState.STARTED) {
try {
pluginManager.stopPlugin(pluginKey); pluginManager.stopPlugin(pluginKey);
} catch (Exception e) {
log.error("[updatePluginStatus][停止插件({}) 失败]", pluginKey, e);
throw exception(ErrorCodeConstants.PLUGIN_STOP_FAILED, e);
}
log.info("已停止插件: {}", pluginKey); log.info("已停止插件: {}", pluginKey);
} }
} }

View File

@ -94,6 +94,34 @@
</archive> </archive>
</configuration> </configuration>
</plugin> </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> <plugin>
@ -112,12 +140,6 @@
</executions> </executions>
</plugin> </plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>

View File

@ -59,22 +59,13 @@ public class IotHttpVertxPlugin extends SpringPlugin {
@Override @Override
protected ApplicationContext createApplicationContext() { protected ApplicationContext createApplicationContext() {
// TODO @haohao这个加 deviceDataApi 的目的是啥呀 // 创建插件自己的 ApplicationContext
AnnotationConfigApplicationContext pluginContext = new AnnotationConfigApplicationContext() { AnnotationConfigApplicationContext pluginContext = new AnnotationConfigApplicationContext();
// 设置父容器为主应用的 ApplicationContext 确保主应用中提供的类可用
@Override pluginContext.setParent(SpringUtil.getApplicationContext());
protected void prepareRefresh() { // 继续使用插件自己的 ClassLoader 以加载插件内部的类
// 在刷新容器前注册主程序中的 Bean
ConfigurableListableBeanFactory beanFactory = this.getBeanFactory();
IotDeviceUpstreamApi deviceDataApi = SpringUtil.getBean(IotDeviceUpstreamApi.class);
beanFactory.registerSingleton("deviceDataApi", deviceDataApi);
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;