Commit a85890d958417e90564fe4ab075fdf6216cd1c9f
1 parent
55243245
【功能完善】IoT: 添加插件目录配置,重构 SpringPluginManager 实例化逻辑,删除不再使用的 ExampleService 类以优化代码结构。
Showing
2 changed files
with
5 additions
and
44 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/UnifiedConfiguration.java
| @@ -5,11 +5,13 @@ import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi; | @@ -5,11 +5,13 @@ import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi; | ||
| 5 | import cn.iocoder.yudao.module.iot.framework.plugin.listener.CustomPluginStateListener; | 5 | import cn.iocoder.yudao.module.iot.framework.plugin.listener.CustomPluginStateListener; |
| 6 | import lombok.extern.slf4j.Slf4j; | 6 | import lombok.extern.slf4j.Slf4j; |
| 7 | import org.pf4j.spring.SpringPluginManager; | 7 | import org.pf4j.spring.SpringPluginManager; |
| 8 | +import org.springframework.beans.factory.annotation.Value; | ||
| 8 | import org.springframework.context.annotation.Bean; | 9 | import org.springframework.context.annotation.Bean; |
| 9 | import org.springframework.context.annotation.Configuration; | 10 | import org.springframework.context.annotation.Configuration; |
| 10 | import org.springframework.context.annotation.DependsOn; | 11 | import org.springframework.context.annotation.DependsOn; |
| 11 | 12 | ||
| 12 | import javax.annotation.Resource; | 13 | import javax.annotation.Resource; |
| 14 | +import java.nio.file.Paths; | ||
| 13 | 15 | ||
| 14 | @Slf4j | 16 | @Slf4j |
| 15 | @Configuration | 17 | @Configuration |
| @@ -19,6 +21,8 @@ public class UnifiedConfiguration { | @@ -19,6 +21,8 @@ public class UnifiedConfiguration { | ||
| 19 | 21 | ||
| 20 | @Resource | 22 | @Resource |
| 21 | private DeviceDataApi deviceDataApi; | 23 | private DeviceDataApi deviceDataApi; |
| 24 | + @Value("${pf4j.pluginsDir:pluginsDir}") | ||
| 25 | + private String pluginsDir; | ||
| 22 | 26 | ||
| 23 | @Bean(SERVICE_REGISTRY_INITIALIZED_MARKER) | 27 | @Bean(SERVICE_REGISTRY_INITIALIZED_MARKER) |
| 24 | public Object serviceRegistryInitializedMarker() { | 28 | public Object serviceRegistryInitializedMarker() { |
| @@ -31,7 +35,7 @@ public class UnifiedConfiguration { | @@ -31,7 +35,7 @@ public class UnifiedConfiguration { | ||
| 31 | @DependsOn(SERVICE_REGISTRY_INITIALIZED_MARKER) | 35 | @DependsOn(SERVICE_REGISTRY_INITIALIZED_MARKER) |
| 32 | public SpringPluginManager pluginManager() { | 36 | public SpringPluginManager pluginManager() { |
| 33 | log.info("[init][实例化 SpringPluginManager]"); | 37 | log.info("[init][实例化 SpringPluginManager]"); |
| 34 | - SpringPluginManager springPluginManager = new SpringPluginManager() { | 38 | + SpringPluginManager springPluginManager = new SpringPluginManager(Paths.get(pluginsDir)) { |
| 35 | @Override | 39 | @Override |
| 36 | public void startPlugins() { | 40 | public void startPlugins() { |
| 37 | // 禁用插件启动,避免插件启动时,启动所有插件 | 41 | // 禁用插件启动,避免插件启动时,启动所有插件 |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/plugin/ExampleService.java deleted
| 1 | -package cn.iocoder.yudao.module.iot.service.plugin; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.module.iot.mqttrpc.server.RpcServer; | ||
| 4 | -import lombok.RequiredArgsConstructor; | ||
| 5 | -import org.springframework.stereotype.Service; | ||
| 6 | - | ||
| 7 | -import javax.annotation.PostConstruct; | ||
| 8 | - | ||
| 9 | -@Service | ||
| 10 | -@RequiredArgsConstructor | ||
| 11 | -public class ExampleService { | ||
| 12 | - | ||
| 13 | - private final RpcServer rpcServer; | ||
| 14 | - | ||
| 15 | - @PostConstruct | ||
| 16 | - public void registerMethods() { | ||
| 17 | - rpcServer.registerMethod("add", params -> { | ||
| 18 | - if (params.length != 2) { | ||
| 19 | - throw new IllegalArgumentException("add方法需要两个参数"); | ||
| 20 | - } | ||
| 21 | - int a = ((Number) params[0]).intValue(); | ||
| 22 | - int b = ((Number) params[1]).intValue(); | ||
| 23 | - return add(a, b); | ||
| 24 | - }); | ||
| 25 | - | ||
| 26 | - rpcServer.registerMethod("concat", params -> { | ||
| 27 | - if (params.length != 2) { | ||
| 28 | - throw new IllegalArgumentException("concat方法需要两个参数"); | ||
| 29 | - } | ||
| 30 | - String str1 = params[0].toString(); | ||
| 31 | - String str2 = params[1].toString(); | ||
| 32 | - return concat(str1, str2); | ||
| 33 | - }); | ||
| 34 | - } | ||
| 35 | - | ||
| 36 | - private int add(int a, int b) { | ||
| 37 | - return a + b; | ||
| 38 | - } | ||
| 39 | - | ||
| 40 | - private String concat(String a, String b) { | ||
| 41 | - return a + b; | ||
| 42 | - } | ||
| 43 | -} | ||
| 44 | \ No newline at end of file | 0 | \ No newline at end of file |