数据初始化优化

V0.5.x
xiwa 2022-07-27 15:47:04 +08:00
parent fc7510708e
commit a7dfd532d9
4 changed files with 65 additions and 47 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -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);
}

View File

@ -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 {

View File

@ -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;
@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;
}
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);
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);
}
}
private <T> void initData(String name, ICommonData service, TypeReference<T> type) throws IOException {
@ -140,5 +155,4 @@ public class ExampleDataInit {
}
}
}