Commit 3b54deb989630f21b7313ac398b4e1f417172fe4

Authored by puhui999
1 parent 61ea0948

【代码优化】IoT: 优化数据桥梁的执行器减少子类代码冗余

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/AbstractCacheableDataBridgeExecute.java
@@ -33,7 +33,7 @@ import java.time.Duration; @@ -33,7 +33,7 @@ import java.time.Duration;
33 * @author HUIHUI 33 * @author HUIHUI
34 */ 34 */
35 @Slf4j 35 @Slf4j
36 -public abstract class AbstractCacheableDataBridgeExecute<Config, Producer> implements IotDataBridgeExecute { 36 +public abstract class AbstractCacheableDataBridgeExecute<Config, Producer> implements IotDataBridgeExecute<Config> {
37 37
38 /** 38 /**
39 * Producer 缓存 39 * Producer 缓存
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotDataBridgeExecute.java
@@ -9,9 +9,14 @@ import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage; @@ -9,9 +9,14 @@ import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage;
9 * 9 *
10 * @author HUIHUI 10 * @author HUIHUI
11 */ 11 */
12 -public interface IotDataBridgeExecute { 12 +public interface IotDataBridgeExecute<Config> {
13 13
14 - // TODO @huihui:要不搞个 getType?然后 execute0 由子类实现。这样,子类的 executeRedisStream ,其实就是 execute0 了。 14 + /**
  15 + * 获取数据桥梁类型
  16 + *
  17 + * @return 数据桥梁类型
  18 + */
  19 + Integer getType();
15 20
16 /** 21 /**
17 * 执行数据桥梁操作 22 * 执行数据桥梁操作
@@ -19,6 +24,23 @@ public interface IotDataBridgeExecute { @@ -19,6 +24,23 @@ public interface IotDataBridgeExecute {
19 * @param message 设备消息 24 * @param message 设备消息
20 * @param dataBridge 数据桥梁 25 * @param dataBridge 数据桥梁
21 */ 26 */
22 - void execute(IotDeviceMessage message, IotDataBridgeDO dataBridge); 27 + @SuppressWarnings({"unchecked"})
  28 + default void execute(IotDeviceMessage message, IotDataBridgeDO dataBridge) {
  29 + // 1.1 校验数据桥梁类型
  30 + if (!getType().equals(dataBridge.getType())) {
  31 + return;
  32 + }
  33 +
  34 + // 1.2 执行对应的数据桥梁发送消息
  35 + execute0(message, (Config) dataBridge.getConfig());
  36 + }
  37 +
  38 + /**
  39 + * 【真正】执行数据桥梁操作
  40 + *
  41 + * @param message 设备消息
  42 + * @param config 桥梁配置
  43 + */
  44 + void execute0(IotDeviceMessage message, Config config);
23 45
24 } 46 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotHttpDataBridgeExecute.java
@@ -25,23 +25,19 @@ import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.HEADER_ @@ -25,23 +25,19 @@ import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.HEADER_
25 */ 25 */
26 @Component 26 @Component
27 @Slf4j 27 @Slf4j
28 -public class IotHttpDataBridgeExecute implements IotDataBridgeExecute { 28 +public class IotHttpDataBridgeExecute implements IotDataBridgeExecute<IotDataBridgeDO.HttpConfig> {
29 29
30 @Resource 30 @Resource
31 private RestTemplate restTemplate; 31 private RestTemplate restTemplate;
32 32
33 @Override 33 @Override
34 - public void execute(IotDeviceMessage message, IotDataBridgeDO dataBridge) {  
35 - // 1.1 校验数据桥梁的类型 == HTTP  
36 - if (!IotDataBridgTypeEnum.HTTP.getType().equals(dataBridge.getType())) {  
37 - return;  
38 - }  
39 - // 1.2 执行 HTTP 请求  
40 - executeHttp(message, (IotDataBridgeDO.HttpConfig) dataBridge.getConfig()); 34 + public Integer getType() {
  35 + return IotDataBridgTypeEnum.HTTP.getType();
41 } 36 }
42 37
  38 + @Override
43 @SuppressWarnings({"unchecked", "deprecation"}) 39 @SuppressWarnings({"unchecked", "deprecation"})
44 - private void executeHttp(IotDeviceMessage message, IotDataBridgeDO.HttpConfig config) { 40 + public void execute0(IotDeviceMessage message, IotDataBridgeDO.HttpConfig config) {
45 String url = null; 41 String url = null;
46 HttpMethod method = HttpMethod.valueOf(config.getMethod().toUpperCase()); 42 HttpMethod method = HttpMethod.valueOf(config.getMethod().toUpperCase());
47 HttpEntity<String> requestEntity = null; 43 HttpEntity<String> requestEntity = null;
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotKafkaMQDataBridgeExecute.java
@@ -30,16 +30,12 @@ public class IotKafkaMQDataBridgeExecute extends @@ -30,16 +30,12 @@ public class IotKafkaMQDataBridgeExecute extends
30 private static final Duration SEND_TIMEOUT = Duration.ofMillis(10); 30 private static final Duration SEND_TIMEOUT = Duration.ofMillis(10);
31 31
32 @Override 32 @Override
33 - public void execute(IotDeviceMessage message, IotDataBridgeDO dataBridge) {  
34 - // 1. 校验数据桥梁的类型 == KAFKA  
35 - if (!IotDataBridgTypeEnum.KAFKA.getType().equals(dataBridge.getType())) {  
36 - return;  
37 - }  
38 - // 2. 执行 Kafka 发送消息  
39 - executeKafka(message, (IotDataBridgeDO.KafkaMQConfig) dataBridge.getConfig()); 33 + public Integer getType() {
  34 + return IotDataBridgTypeEnum.KAFKA.getType();
40 } 35 }
41 36
42 - private void executeKafka(IotDeviceMessage message, IotDataBridgeDO.KafkaMQConfig config) { 37 + @Override
  38 + public void execute0(IotDeviceMessage message, IotDataBridgeDO.KafkaMQConfig config) {
43 try { 39 try {
44 // 1. 获取或创建 KafkaTemplate 40 // 1. 获取或创建 KafkaTemplate
45 KafkaTemplate<String, String> kafkaTemplate = getProducer(config); 41 KafkaTemplate<String, String> kafkaTemplate = getProducer(config);
@@ -113,10 +109,10 @@ public class IotKafkaMQDataBridgeExecute extends @@ -113,10 +109,10 @@ public class IotKafkaMQDataBridgeExecute extends
113 109
114 // 4. 执行两次测试,验证缓存 110 // 4. 执行两次测试,验证缓存
115 log.info("[main][第一次执行,应该会创建新的 producer]"); 111 log.info("[main][第一次执行,应该会创建新的 producer]");
116 - action.executeKafka(message, config); 112 + action.execute0(message, config);
117 113
118 log.info("[main][第二次执行,应该会复用缓存的 producer]"); 114 log.info("[main][第二次执行,应该会复用缓存的 producer]");
119 - action.executeKafka(message, config); 115 + action.execute0(message, config);
120 } 116 }
121 117
122 } 118 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotRabbitMQDataBridgeExecute.java
@@ -22,17 +22,14 @@ import java.time.LocalDateTime; @@ -22,17 +22,14 @@ import java.time.LocalDateTime;
22 public class IotRabbitMQDataBridgeExecute extends 22 public class IotRabbitMQDataBridgeExecute extends
23 AbstractCacheableDataBridgeExecute<IotDataBridgeDO.RabbitMQConfig, Channel> { 23 AbstractCacheableDataBridgeExecute<IotDataBridgeDO.RabbitMQConfig, Channel> {
24 24
  25 +
25 @Override 26 @Override
26 - public void execute(IotDeviceMessage message, IotDataBridgeDO dataBridge) {  
27 - // 1.1 校验数据桥梁的类型 == RABBITMQ  
28 - if (!IotDataBridgTypeEnum.RABBITMQ.getType().equals(dataBridge.getType())) {  
29 - return;  
30 - }  
31 - // 1.2 执行 RabbitMQ 发送消息  
32 - executeRabbitMQ(message, (IotDataBridgeDO.RabbitMQConfig) dataBridge.getConfig()); 27 + public Integer getType() {
  28 + return IotDataBridgTypeEnum.RABBITMQ.getType();
33 } 29 }
34 30
35 - private void executeRabbitMQ(IotDeviceMessage message, IotDataBridgeDO.RabbitMQConfig config) { 31 + @Override
  32 + public void execute0(IotDeviceMessage message, IotDataBridgeDO.RabbitMQConfig config) {
36 try { 33 try {
37 // 1. 获取或创建 Channel 34 // 1. 获取或创建 Channel
38 Channel channel = getProducer(config); 35 Channel channel = getProducer(config);
@@ -111,10 +108,10 @@ public class IotRabbitMQDataBridgeExecute extends @@ -111,10 +108,10 @@ public class IotRabbitMQDataBridgeExecute extends
111 108
112 // 4. 执行两次测试,验证缓存 109 // 4. 执行两次测试,验证缓存
113 log.info("[main][第一次执行,应该会创建新的 channel]"); 110 log.info("[main][第一次执行,应该会创建新的 channel]");
114 - action.executeRabbitMQ(message, config); 111 + action.execute0(message, config);
115 112
116 log.info("[main][第二次执行,应该会复用缓存的 channel]"); 113 log.info("[main][第二次执行,应该会复用缓存的 channel]");
117 - action.executeRabbitMQ(message, config); 114 + action.execute0(message, config);
118 } 115 }
119 116
120 } 117 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotRedisStreamMQDataBridgeExecute.java
@@ -33,18 +33,12 @@ public class IotRedisStreamMQDataBridgeExecute extends @@ -33,18 +33,12 @@ public class IotRedisStreamMQDataBridgeExecute extends
33 AbstractCacheableDataBridgeExecute<IotDataBridgeDO.RedisStreamMQConfig, RedisTemplate<String, Object>> { 33 AbstractCacheableDataBridgeExecute<IotDataBridgeDO.RedisStreamMQConfig, RedisTemplate<String, Object>> {
34 34
35 @Override 35 @Override
36 - public void execute(IotDeviceMessage message, IotDataBridgeDO dataBridge) {  
37 - // 1.1 校验数据桥梁类型  
38 - if (!IotDataBridgTypeEnum.REDIS_STREAM.getType().equals(dataBridge.getType())) {  
39 - return;  
40 - }  
41 - // 1.2 执行消息发送  
42 - executeRedisStream(message, (IotDataBridgeDO.RedisStreamMQConfig) dataBridge.getConfig()); 36 + public Integer getType() {
  37 + return IotDataBridgTypeEnum.REDIS_STREAM.getType();
43 } 38 }
44 39
45 - @SuppressWarnings("unchecked")  
46 - // TODO @huihui:try catch 交给父类来做,子类不处理异常  
47 - private void executeRedisStream(IotDeviceMessage message, IotDataBridgeDO.RedisStreamMQConfig config) { 40 + @Override
  41 + public void execute0(IotDeviceMessage message, IotDataBridgeDO.RedisStreamMQConfig config) {
48 try { 42 try {
49 // 1. 获取 RedisTemplate 43 // 1. 获取 RedisTemplate
50 RedisTemplate<String, Object> redisTemplate = getProducer(config); 44 RedisTemplate<String, Object> redisTemplate = getProducer(config);
@@ -133,10 +127,10 @@ public class IotRedisStreamMQDataBridgeExecute extends @@ -133,10 +127,10 @@ public class IotRedisStreamMQDataBridgeExecute extends
133 127
134 // 4. 执行两次测试,验证缓存 128 // 4. 执行两次测试,验证缓存
135 log.info("[main][第一次执行,应该会创建新的 RedisTemplate]"); 129 log.info("[main][第一次执行,应该会创建新的 RedisTemplate]");
136 - action.executeRedisStream(message, config); 130 + action.execute0(message, config);
137 131
138 log.info("[main][第二次执行,应该会复用缓存的 RedisTemplate]"); 132 log.info("[main][第二次执行,应该会复用缓存的 RedisTemplate]");
139 - action.executeRedisStream(message, config); 133 + action.execute0(message, config);
140 } 134 }
141 135
142 } 136 }
143 \ No newline at end of file 137 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotRocketMQDataBridgeExecute.java
@@ -24,16 +24,12 @@ public class IotRocketMQDataBridgeExecute extends @@ -24,16 +24,12 @@ public class IotRocketMQDataBridgeExecute extends
24 AbstractCacheableDataBridgeExecute<IotDataBridgeDO.RocketMQConfig, DefaultMQProducer> { 24 AbstractCacheableDataBridgeExecute<IotDataBridgeDO.RocketMQConfig, DefaultMQProducer> {
25 25
26 @Override 26 @Override
27 - public void execute(IotDeviceMessage message, IotDataBridgeDO dataBridge) {  
28 - // 1.1 校验数据桥梁的类型 == ROCKETMQ  
29 - if (!IotDataBridgTypeEnum.ROCKETMQ.getType().equals(dataBridge.getType())) {  
30 - return;  
31 - }  
32 - // 1.2 执行 RocketMQ 发送消息  
33 - executeRocketMQ(message, (IotDataBridgeDO.RocketMQConfig) dataBridge.getConfig()); 27 + public Integer getType() {
  28 + return IotDataBridgTypeEnum.ROCKETMQ.getType();
34 } 29 }
35 30
36 - private void executeRocketMQ(IotDeviceMessage message, IotDataBridgeDO.RocketMQConfig config) { 31 + @Override
  32 + public void execute0(IotDeviceMessage message, IotDataBridgeDO.RocketMQConfig config) {
37 try { 33 try {
38 // 1. 获取或创建 Producer 34 // 1. 获取或创建 Producer
39 DefaultMQProducer producer = getProducer(config); 35 DefaultMQProducer producer = getProducer(config);
@@ -97,10 +93,10 @@ public class IotRocketMQDataBridgeExecute extends @@ -97,10 +93,10 @@ public class IotRocketMQDataBridgeExecute extends
97 93
98 // 4. 执行两次测试,验证缓存 94 // 4. 执行两次测试,验证缓存
99 log.info("[main][第一次执行,应该会创建新的 producer]"); 95 log.info("[main][第一次执行,应该会创建新的 producer]");
100 - action.executeRocketMQ(message, config); 96 + action.execute0(message, config);
101 97
102 log.info("[main][第二次执行,应该会复用缓存的 producer]"); 98 log.info("[main][第二次执行,应该会复用缓存的 producer]");
103 - action.executeRocketMQ(message, config); 99 + action.execute0(message, config);
104 } 100 }
105 101
106 } 102 }