1,背景: 这是一个解析Txt 保存更新内容接口
@PostMapping("txt")
@Transactional(rollbackFor = Exception.class)
@ApiOperation(value = "解析接口")
@ApiImplicitParams({
@ApiImplicitParam(name="dispatchBid",dataType="Long",required=true,value="调度任务Bid",defaultValue="0"),
@ApiImplicitParam(name="executionBid",dataType="Long",required=true,value="执行任务bid",defaultValue="0")
})
public InvokeResultUtil<Object> analysisTxt(@RequestBody MultipartFile multipartFile,
@RequestParam("dispatchBid") Long dispatchBid,
@RequestParam("executionBid") Long executionBid,
@RequestParam("sourceType") Integer sourceType
) {
try {
log.info("analysisTxt 入参 MultipartFile:{},dispatchBid:{},executionBid:{},", multipartFile, dispatchBid, executionBid);
if (multipartFile.isEmpty() || judgeFormat(arr, multipartFile)) {
return InvokeResultUtil.failure(BizStatusCodeEnum.MISSING_PARAMS.getCode(), "上传文件为空或文件格式不匹配");
}
MsDispatchTask task = taskService.getMsDispatchTaskByBid(dispatchBid);
if (task == null)
return InvokeResultUtil.failure(BizStatusCodeEnum.FAILED.getCode(),"未查询到调度任务");
if (queryAnalysisList(multipartFile , sourceType)){
return InvokeResultUtil.failure(BizStatusCodeEnum.FAILED.getCode(),"该名称的物料已经存在");
}
int i = ZERO;
int y = ZERO;
int nThreads = TEN;
String lineTxt;
Long mainBid = sequenceNumUtil.getNum("");
List<MsAnalysisMaterial> list = new LinkedList<>();
InputStreamReader read = new InputStreamReader(multipartFile.getInputStream(), ENCODING);
BufferedReader br = new BufferedReader(read);
while ((lineTxt = br.readLine()) != null) {
if (i == ZERO) {
i++;
continue;
}else if (StringUtils.isEmpty(lineTxt.trim())){
continue;
}
list.add(MsAnalysisMaterial.generateMsAnalysisMaterial(sequenceNumUtil.getNum(""),mainBid,sourceType,
dispatchBid, executionBid, i, task.getProjectId(), multipartFile.getOriginalFilename(), lineTxt));
i++;
}
int size = list.size();
ExecutorService executorService = Executors.newFixedThreadPool(nThreads + ONE);
List<Future<Integer>> futureList = new ArrayList<>(nThreads);
for (int x = 0; x < nThreads; x++) {
final List<MsAnalysisMaterial> finalList = list.subList(size / nThreads * x, size / nThreads * (x + 1));
Callable<Integer> task1 = () -> {
msAnalysisMaterialService.saveBatch(finalList);
return ONE;
};
y = y + finalList.size();
futureList.add(executorService.submit(task1));
}
final List<MsAnalysisMaterial> finalList = list.subList(y, size);
if (!CollectionUtils.isEmpty(finalList)){
Callable<Integer> task2 = () -> {
msAnalysisMaterialService.saveBatch(finalList);
return ONE;
};
futureList.add(executorService.submit(task2));
}
executorService.shutdown();
read.close();
Integer integer = insertFailRecords(sourceType);
msAnalysisMaterialMainService.save(MsAnalysisMaterialMain.generateMsAnalysisMaterialMain(mainBid,sourceType,
multipartFile.getOriginalFilename(), integer));
return InvokeResultUtil.success("文件解析成功");
} catch (Exception e) {
log.error("读取文件内容出错", e);
e.printStackTrace();
return InvokeResultUtil.failure(BizStatusCodeEnum.ERROR_SAVE.getCode(), "读取文件内容异常");
}
}
数据库异常:
Caused by : java.sql.SQLTransientConnectionException: HikariPool-1
Connection is not available, request timed out after 30000ms
修改后数据库配置:
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xx.xx.xx.169:3306/yyyy?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useAffectedRows=true&serverTimezone=Asia/Shanghai
username: adminV1
password: ENC(AK7UALIt1/5wqk62OIYtw+1JJN2FgaLE)
hikari:
minimum-idle: 20
idle-timeout: 1800000
maximum-pool-size: 80
auto-commit: true
max-lifetime: 1800000
connection-timeout: 60000
validation-timeout: 30000
connection-test-query: SELECT 1
connection-init-sql: SET NAMES utf8mb4
|