|
@@ -32,7 +32,9 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
|
import com.google.common.base.Charsets;
|
|
import com.google.common.base.Charsets;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.io.FileUtils;
|
|
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.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
|
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
@@ -40,10 +42,12 @@ import javax.annotation.PostConstruct;
|
|
import java.io.File;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
+import java.util.Timer;
|
|
|
|
+import java.util.TimerTask;
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@Service
|
|
@Service
|
|
-public class ExampleDataInit {
|
|
|
|
|
|
+public class ExampleDataInit implements SmartInitializingSingleton {
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private IOauthClientData oauthClientData;
|
|
private IOauthClientData oauthClientData;
|
|
@@ -52,6 +56,7 @@ public class ExampleDataInit {
|
|
@Autowired
|
|
@Autowired
|
|
private IDeviceGroupData deviceGroupData;
|
|
private IDeviceGroupData deviceGroupData;
|
|
@Autowired
|
|
@Autowired
|
|
|
|
+ @Qualifier("deviceInfoDataCache")
|
|
private IDeviceInfoData deviceInfoData;
|
|
private IDeviceInfoData deviceInfoData;
|
|
@Autowired
|
|
@Autowired
|
|
private IHomeData homeData;
|
|
private IHomeData homeData;
|
|
@@ -82,53 +87,63 @@ public class ExampleDataInit {
|
|
@Autowired
|
|
@Autowired
|
|
private ElasticsearchRestTemplate restTemplate;
|
|
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 {
|
|
private <T> void initData(String name, ICommonData service, TypeReference<T> type) throws IOException {
|
|
@@ -140,5 +155,4 @@ public class ExampleDataInit {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
}
|
|
}
|