多线程并行初始化数据
This commit is contained in:
@@ -16,14 +16,17 @@ import com.gis.xian.vo.XianHiddenDangerSpotsBasePointVo;
|
|||||||
import com.gis.xian.vo.XianHospitalsBasePointVo;
|
import com.gis.xian.vo.XianHospitalsBasePointVo;
|
||||||
import com.gis.xian.vo.XianRiskSpotsBasePointVo;
|
import com.gis.xian.vo.XianRiskSpotsBasePointVo;
|
||||||
import com.gis.xian.vo.XianStorePointsBasePointVo;
|
import com.gis.xian.vo.XianStorePointsBasePointVo;
|
||||||
import jakarta.annotation.PostConstruct;
|
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
|
import org.springframework.context.event.EventListener;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.scheduling.annotation.Async;
|
import org.springframework.scheduling.annotation.Async;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化数据
|
* 初始化数据
|
||||||
*/
|
*/
|
||||||
@@ -79,64 +82,89 @@ public class InitializeData {
|
|||||||
@Value("${init.data.base-points.store-points}")
|
@Value("${init.data.base-points.store-points}")
|
||||||
private String storePointsBasePointsKey;
|
private String storePointsBasePointsKey;
|
||||||
|
|
||||||
@PostConstruct
|
@EventListener(ApplicationReadyEvent.class)
|
||||||
@Async("xianPool")
|
@Async("xianPool")
|
||||||
public void init() {
|
public void init() {
|
||||||
log.info("开始初始化数据");
|
log.info("开始初始化数据");
|
||||||
|
// 并行执行所有数据库查询和Redis写入
|
||||||
// 加载滑坡、泥石流、山洪、内涝隐患点信息并写入redis
|
CompletableFuture<Void> rainstormFuture = CompletableFuture.runAsync(() -> {
|
||||||
redisTemplate.opsForValue().set(rainstormBasePointsKey, JSON.toJSONString(
|
redisTemplate.opsForValue().set(rainstormBasePointsKey, JSON.toJSONString(
|
||||||
XianHiddenDangerSpotsBasePointVo.entity2Vo(
|
XianHiddenDangerSpotsBasePointVo.entity2Vo(
|
||||||
xianHiddenDangerSpotsMapper.getBasePoints(DisasterTypeEnum.RAINSTORM.getType()))
|
xianHiddenDangerSpotsMapper.getBasePoints(DisasterTypeEnum.RAINSTORM.getType()))
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
log.info("加载暴雨隐患点信息并写入redis完成");
|
||||||
|
});
|
||||||
|
|
||||||
|
CompletableFuture<Void> earthquakeFuture = CompletableFuture.runAsync(() -> {
|
||||||
redisTemplate.opsForValue().set(earthquakeBasePointsKey, JSON.toJSONString(
|
redisTemplate.opsForValue().set(earthquakeBasePointsKey, JSON.toJSONString(
|
||||||
XianHiddenDangerSpotsBasePointVo.entity2Vo(
|
XianHiddenDangerSpotsBasePointVo.entity2Vo(
|
||||||
xianHiddenDangerSpotsMapper.getBasePoints(DisasterTypeEnum.EARTHQUAKE.getType()))
|
xianHiddenDangerSpotsMapper.getBasePoints(DisasterTypeEnum.EARTHQUAKE.getType()))
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
log.info("加载地震隐患点信息并写入redis完成");
|
||||||
|
});
|
||||||
|
|
||||||
// 加载风险点基本信息写入redis
|
CompletableFuture<Void> riskFuture = CompletableFuture.runAsync(() -> {
|
||||||
redisTemplate.opsForValue().set(riskBasePointsKey, JSON.toJSONString(
|
redisTemplate.opsForValue().set(riskBasePointsKey, JSON.toJSONString(
|
||||||
XianRiskSpotsBasePointVo.entity2Vo(
|
XianRiskSpotsBasePointVo.entity2Vo(
|
||||||
xianRiskSpotsMapper.getBasePoints())
|
xianRiskSpotsMapper.getBasePoints())
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
log.info("加载风险点基本信息写入redis完成");
|
||||||
|
});
|
||||||
|
|
||||||
// 加载医院基本信息写入redis
|
CompletableFuture<Void> hospitalsFuture = CompletableFuture.runAsync(() -> {
|
||||||
redisTemplate.opsForValue().set(hospitalsBasePointsKey, JSON.toJSONString(
|
redisTemplate.opsForValue().set(hospitalsBasePointsKey, JSON.toJSONString(
|
||||||
XianHospitalsBasePointVo.entity2Vo(
|
XianHospitalsBasePointVo.entity2Vo(
|
||||||
xianHospitalsMapper.getBasePoints())
|
xianHospitalsMapper.getBasePoints())
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
log.info("加载医院基本信息写入redis完成");
|
||||||
|
});
|
||||||
|
|
||||||
// 加载危险源基本信息写入redis
|
CompletableFuture<Void> dangerousSourceFuture = CompletableFuture.runAsync(() -> {
|
||||||
redisTemplate.opsForValue().set(dangerousSourceBasePointsKey, JSON.toJSONString(
|
redisTemplate.opsForValue().set(dangerousSourceBasePointsKey, JSON.toJSONString(
|
||||||
XianDangerousSourceBasePointVo.entity2Vo(
|
XianDangerousSourceBasePointVo.entity2Vo(
|
||||||
xianDangerousSourceMapper.getBasePoints())
|
xianDangerousSourceMapper.getBasePoints())
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
log.info("加载危险源基本信息写入redis完成");
|
||||||
|
});
|
||||||
|
|
||||||
// 加载应急避难所基本信息写入redis
|
CompletableFuture<Void> emergencyShelterFuture = CompletableFuture.runAsync(() -> {
|
||||||
redisTemplate.opsForValue().set(emergencyShelterBasePointsKey, JSON.toJSONString(
|
redisTemplate.opsForValue().set(emergencyShelterBasePointsKey, JSON.toJSONString(
|
||||||
XianEmergencyShelterBasePointVo.entity2Vo(
|
XianEmergencyShelterBasePointVo.entity2Vo(
|
||||||
xianEmergencyShelterMapper.getBasePoints())
|
xianEmergencyShelterMapper.getBasePoints())
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
log.info("加载应急避难所基本信息写入redis完成");
|
||||||
|
});
|
||||||
|
|
||||||
// 加载消防站基本信息写入redis
|
CompletableFuture<Void> firefighterFuture = CompletableFuture.runAsync(() -> {
|
||||||
redisTemplate.opsForValue().set(firefighterBasePointsKey, JSON.toJSONString(
|
redisTemplate.opsForValue().set(firefighterBasePointsKey, JSON.toJSONString(
|
||||||
XianFirefighterBasePointVo.entity2Vo(
|
XianFirefighterBasePointVo.entity2Vo(
|
||||||
xianFirefighterMapper.getBasePoints())
|
xianFirefighterMapper.getBasePoints())
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
log.info("加载消防站基本信息写入redis完成");
|
||||||
|
});
|
||||||
|
|
||||||
// 加载物资储备点基本信息写入redis
|
CompletableFuture<Void> storePointsFuture = CompletableFuture.runAsync(() -> {
|
||||||
redisTemplate.opsForValue().set(storePointsBasePointsKey, JSON.toJSONString(
|
redisTemplate.opsForValue().set(storePointsBasePointsKey, JSON.toJSONString(
|
||||||
XianStorePointsBasePointVo.entity2Vo(
|
XianStorePointsBasePointVo.entity2Vo(
|
||||||
xianStorePointsMapper.getBasePoints())
|
xianStorePointsMapper.getBasePoints())
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
log.info("加载物资储备点基本信息写入redis完成");
|
||||||
|
});
|
||||||
|
|
||||||
|
// 等待所有任务完成
|
||||||
|
CompletableFuture.allOf(
|
||||||
|
rainstormFuture, earthquakeFuture, riskFuture, hospitalsFuture,
|
||||||
|
dangerousSourceFuture, emergencyShelterFuture, firefighterFuture, storePointsFuture
|
||||||
|
).join();
|
||||||
|
|
||||||
log.info("初始化数据完成");
|
log.info("初始化数据完成");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ spring:
|
|||||||
datasource:
|
datasource:
|
||||||
# Druid 公共配置
|
# Druid 公共配置
|
||||||
druid:
|
druid:
|
||||||
initial-size: 5
|
initial-size: 8
|
||||||
min-idle: 5
|
min-idle: 5
|
||||||
max-active: 20
|
max-active: 20
|
||||||
max-wait: 60000
|
max-wait: 60000
|
||||||
|
|||||||
Reference in New Issue
Block a user