Commit f76843573ee25546b829b5a1209bba9b2ffcb12f
1 parent
672247db
【代码评审】IoT:数据桥梁的执行器抽离
Showing
4 changed files
with
12 additions
and
11 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/IotRuleSceneDataBridgeAction.java
| ... | ... | @@ -7,7 +7,7 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotRuleSceneDO; |
| 7 | 7 | import cn.iocoder.yudao.module.iot.enums.rule.IotRuleSceneActionTypeEnum; |
| 8 | 8 | import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage; |
| 9 | 9 | import cn.iocoder.yudao.module.iot.service.rule.IotDataBridgeService; |
| 10 | -import cn.iocoder.yudao.module.iot.service.rule.execute.IotDataBridgeExecute; | |
| 10 | +import cn.iocoder.yudao.module.iot.service.rule.action.databridge.IotDataBridgeExecute; | |
| 11 | 11 | import jakarta.annotation.Resource; |
| 12 | 12 | import lombok.extern.slf4j.Slf4j; |
| 13 | 13 | import org.springframework.stereotype.Component; |
| ... | ... | @@ -19,9 +19,6 @@ import java.util.List; |
| 19 | 19 | * |
| 20 | 20 | * @author 芋道源码 |
| 21 | 21 | */ |
| 22 | -// TODO @芋艿:【优化】因为 bridge 会比较多,所以可以考虑在 rule 下,新建一个 bridge 的 package,然后定义一个 bridgehandler,它有: | |
| 23 | -// 1. input 方法、output 方法 | |
| 24 | -// 2. build 方法,用于有状态的连接,例如说 mq、tcp、websocket | |
| 25 | 22 | @Component |
| 26 | 23 | @Slf4j |
| 27 | 24 | public class IotRuleSceneDataBridgeAction implements IotRuleSceneAction { |
| ... | ... | @@ -49,7 +46,7 @@ public class IotRuleSceneDataBridgeAction implements IotRuleSceneAction { |
| 49 | 46 | return; |
| 50 | 47 | } |
| 51 | 48 | |
| 52 | - // 2.1 执行数据桥接操作 | |
| 49 | + // 2. 执行数据桥接操作 | |
| 53 | 50 | dataBridgeExecutes.forEach(execute -> execute.execute(message, dataBridge)); |
| 54 | 51 | } |
| 55 | 52 | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/execute/IotDataBridgeExecute.java renamed to yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotDataBridgeExecute.java
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/execute/IotHttpDataBridgeExecute.java renamed to yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotHttpDataBridgeExecute.java
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/execute/IotRocketMQDataBridgeExecute.java renamed to yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotRocketMQDataBridgeExecute.java
| 1 | -package cn.iocoder.yudao.module.iot.service.rule.execute; | |
| 1 | +package cn.iocoder.yudao.module.iot.service.rule.action.databridge; | |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.module.iot.dal.dataobject.rule.IotDataBridgeDO; |
| 4 | 4 | import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgTypeEnum; |
| ... | ... | @@ -30,12 +30,13 @@ public class IotRocketMQDataBridgeExecute implements IotDataBridgeExecute { |
| 30 | 30 | /** |
| 31 | 31 | * 针对 {@link IotDataBridgeDO.RocketMQConfig} 的 DefaultMQProducer 缓存 |
| 32 | 32 | */ |
| 33 | + // TODO @puhui999:因为 kafka 之类也存在这个情况,是不是得搞个抽象类。提供一个 initProducer,和 closeProducer 方法 | |
| 33 | 34 | private final LoadingCache<IotDataBridgeDO.RocketMQConfig, DefaultMQProducer> PRODUCER_CACHE = CacheBuilder.newBuilder() |
| 34 | - // 只阻塞当前数据加载线程,其他线程返回旧值 | |
| 35 | - .refreshAfterWrite(Duration.ofMinutes(10)) | |
| 35 | + .refreshAfterWrite(Duration.ofMinutes(10)) // TODO puhui999:应该是 read 30 分钟哈 | |
| 36 | 36 | // 增加移除监听器,自动关闭 producer |
| 37 | 37 | .removalListener(notification -> { |
| 38 | 38 | DefaultMQProducer producer = (DefaultMQProducer) notification.getValue(); |
| 39 | + // TODO puhui999:if return,更简短哈 | |
| 39 | 40 | if (producer != null) { |
| 40 | 41 | try { |
| 41 | 42 | producer.shutdown(); |
| ... | ... | @@ -45,8 +46,10 @@ public class IotRocketMQDataBridgeExecute implements IotDataBridgeExecute { |
| 45 | 46 | } |
| 46 | 47 | } |
| 47 | 48 | }) |
| 49 | + // TODO @puhui999:就同步哈,不用异步处理。 | |
| 48 | 50 | // 通过 asyncReloading 实现全异步加载,包括 refreshAfterWrite 被阻塞的加载线程 |
| 49 | 51 | .build(CacheLoader.asyncReloading(new CacheLoader<IotDataBridgeDO.RocketMQConfig, DefaultMQProducer>() { |
| 52 | + | |
| 50 | 53 | @Override |
| 51 | 54 | public DefaultMQProducer load(IotDataBridgeDO.RocketMQConfig config) throws Exception { |
| 52 | 55 | DefaultMQProducer producer = new DefaultMQProducer(config.getGroup()); |
| ... | ... | @@ -55,6 +58,7 @@ public class IotRocketMQDataBridgeExecute implements IotDataBridgeExecute { |
| 55 | 58 | log.info("[PRODUCER_CACHE][配置({}) 对应的 producer 已创建并启动]", config); |
| 56 | 59 | return producer; |
| 57 | 60 | } |
| 61 | + | |
| 58 | 62 | }, Executors.newCachedThreadPool())); |
| 59 | 63 | |
| 60 | 64 | @Override |
| ... | ... | @@ -120,7 +124,7 @@ public class IotRocketMQDataBridgeExecute implements IotDataBridgeExecute { |
| 120 | 124 | log.info("[main][第一次执行,应该会创建新的 producer]"); |
| 121 | 125 | action.executeRocketMQ(message, config); |
| 122 | 126 | |
| 123 | - log.info("[main][第二次执行,应该会复用缓存的 producer]"); | |
| 127 | + log.info("[main][第二次执行,应该会复用缓存的 producer]"); | |
| 124 | 128 | action.executeRocketMQ(message, config); |
| 125 | 129 | } |
| 126 | 130 | ... | ... |