Bladeren bron

数据初始化优化

xiwa 2 jaren geleden
bovenliggende
commit
a7dfd532d9

BIN
.DS_Store


+ 2 - 0
iot-data/iot-rdb-data-service/src/main/java/cc/iotkit/data/dao/VirtualDeviceMappingRepository.java

@@ -12,12 +12,14 @@ package cc.iotkit.data.dao;
 import cc.iotkit.data.model.TbVirtualDeviceMapping;
 import org.springframework.data.jpa.repository.JpaRepository;
 
+import javax.transaction.Transactional;
 import java.util.List;
 
 public interface VirtualDeviceMappingRepository extends JpaRepository<TbVirtualDeviceMapping, String> {
 
     List<TbVirtualDeviceMapping> findByVirtualId(String virtualId);
 
+    @Transactional
     void deleteByVirtualId(String virtualId);
 
 }

+ 2 - 0
iot-standalone/src/main/java/cc/iotkit/Application.java

@@ -14,10 +14,12 @@ import cc.iotkit.manager.config.EmbeddedRedisConfig;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.transaction.annotation.EnableTransactionManagement;
 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 
 @Slf4j
 @SpringBootApplication(scanBasePackages = {"cc.iotkit"})
+@EnableTransactionManagement
 @EnableWebMvc
 public class Application {
 

+ 60 - 46
iot-standalone/src/main/java/cc/iotkit/manager/service/ExampleDataInit.java

@@ -32,7 +32,9 @@ import com.fasterxml.jackson.core.type.TypeReference;
 import com.google.common.base.Charsets;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.FileUtils;
+import org.springframework.beans.factory.SmartInitializingSingleton;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
 import org.springframework.stereotype.Service;
 
@@ -40,10 +42,12 @@ import javax.annotation.PostConstruct;
 import java.io.File;
 import java.io.IOException;
 import java.util.List;
+import java.util.Timer;
+import java.util.TimerTask;
 
 @Slf4j
 @Service
-public class ExampleDataInit {
+public class ExampleDataInit implements SmartInitializingSingleton {
 
     @Autowired
     private IOauthClientData oauthClientData;
@@ -52,6 +56,7 @@ public class ExampleDataInit {
     @Autowired
     private IDeviceGroupData deviceGroupData;
     @Autowired
+    @Qualifier("deviceInfoDataCache")
     private IDeviceInfoData deviceInfoData;
     @Autowired
     private IHomeData homeData;
@@ -82,53 +87,63 @@ public class ExampleDataInit {
     @Autowired
     private ElasticsearchRestTemplate restTemplate;
 
-    @PostConstruct
-    public void init() {
-        try {
-            File initFile = new File(".init");
-            if (initFile.exists()) {
-                return;
-            }
 
-            initData("category", categoryData, new TypeReference<List<Category>>() {
-            });
-            initData("deviceGroup", deviceGroupData, new TypeReference<List<DeviceGroup>>() {
-            });
-            initData("deviceInfo", deviceInfoData, new TypeReference<List<DeviceInfo>>() {
-            });
-            initData("home", homeData, new TypeReference<List<Home>>() {
-            });
-            initData("oauthClient", oauthClientData, new TypeReference<List<OauthClient>>() {
-            });
-            initData("product", productData, new TypeReference<List<Product>>() {
-            });
-            initData("productModel", productModelData, new TypeReference<List<ProductModel>>() {
-            });
-            initData("protocolComponent", protocolComponentData, new TypeReference<List<ProtocolComponent>>() {
-            });
-            initData("protocolConverter", protocolConverterData, new TypeReference<List<ProtocolConverter>>() {
-            });
-            initData("ruleInfo", ruleInfoData, new TypeReference<List<RuleInfo>>() {
-            });
-            initData("space", spaceData, new TypeReference<List<Space>>() {
-            });
-            initData("spaceDevice", spaceDeviceData, new TypeReference<List<SpaceDevice>>() {
-            });
-            initData("taskInfo", taskInfoData, new TypeReference<List<TaskInfo>>() {
-            });
-            initData("thingModel", thingModelData, new TypeReference<List<ThingModel>>() {
-            });
-            initData("userInfo", userInfoData, new TypeReference<List<UserInfo>>() {
-            });
-            initData("virtualDevice", virtualDeviceData, new TypeReference<List<VirtualDevice>>() {
-            });
+    @Override
+    public void afterSingletonsInstantiated() {
+        //等redis实例化后再执行
+        Timer timer = new Timer();
+        timer.schedule(new TimerTask() {
+            @Override
+            public void run() {
+                try {
+                    File initFile = new File(".init");
+                    if (initFile.exists()) {
+                        return;
+                    }
 
-            log.info("init data finished.");
+                    initData("category", categoryData, new TypeReference<List<Category>>() {
+                    });
+                    initData("deviceGroup", deviceGroupData, new TypeReference<List<DeviceGroup>>() {
+                    });
+                    initData("deviceInfo", deviceInfoData, new TypeReference<List<DeviceInfo>>() {
+                    });
+                    initData("home", homeData, new TypeReference<List<Home>>() {
+                    });
+                    initData("oauthClient", oauthClientData, new TypeReference<List<OauthClient>>() {
+                    });
+                    initData("product", productData, new TypeReference<List<Product>>() {
+                    });
+                    initData("productModel", productModelData, new TypeReference<List<ProductModel>>() {
+                    });
+                    initData("protocolComponent", protocolComponentData, new TypeReference<List<ProtocolComponent>>() {
+                    });
+                    initData("protocolConverter", protocolConverterData, new TypeReference<List<ProtocolConverter>>() {
+                    });
+                    initData("ruleInfo", ruleInfoData, new TypeReference<List<RuleInfo>>() {
+                    });
+                    initData("space", spaceData, new TypeReference<List<Space>>() {
+                    });
+                    initData("spaceDevice", spaceDeviceData, new TypeReference<List<SpaceDevice>>() {
+                    });
+                    initData("taskInfo", taskInfoData, new TypeReference<List<TaskInfo>>() {
+                    });
+                    initData("thingModel", thingModelData, new TypeReference<List<ThingModel>>() {
+                    });
+                    initData("userInfo", userInfoData, new TypeReference<List<UserInfo>>() {
+                    });
+                    initData("virtualDevice", virtualDeviceData, new TypeReference<List<VirtualDevice>>() {
+                    });
+
+                    log.info("init data finished.");
+
+                    FileUtils.write(initFile, "", Charsets.UTF_8);
+                } catch (
+                        Throwable e) {
+                    log.error("init error", e);
+                }
+            }
+        }, 100);
 
-            FileUtils.write(initFile, "", Charsets.UTF_8);
-        } catch (Throwable e) {
-            log.error("init error", e);
-        }
     }
 
     private <T> void initData(String name, ICommonData service, TypeReference<T> type) throws IOException {
@@ -140,5 +155,4 @@ public class ExampleDataInit {
         }
     }
 
-
 }