perf(feature/DM/nflg-bom): 同步虚拟物料到OA改为异步,http请求添加日志

This commit is contained in:
曹鹏飞 2024-03-27 21:15:52 +08:00
parent 83a94ead23
commit ffcf1d6991
3 changed files with 41 additions and 7 deletions

View File

@ -28,4 +28,17 @@ public class TaskPoolConfig {
taskExecutor.setThreadFactory(new ThreadFactoryBuilder().setNamePrefix("plm-searcher-thread-").build());
return taskExecutor;
}
@Bean("syncOAThreadPool")
public ThreadPoolTaskExecutor syncOAThreadPool() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(10);
taskExecutor.setMaxPoolSize(30);
taskExecutor.setQueueCapacity(2000);
taskExecutor.setKeepAliveSeconds(100);
taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//设置线程池的线程名称方便日志追踪
taskExecutor.setThreadFactory(new ThreadFactoryBuilder().setNamePrefix("sync-oa-").build());
return taskExecutor;
}
}

View File

@ -25,6 +25,8 @@ import lombok.extern.slf4j.Slf4j;
import nflg.product.common.constant.STATE;
import nflg.product.common.vo.ResultVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -32,6 +34,7 @@ import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
@Service
@ -44,6 +47,10 @@ public class MaterialService {
@Resource
MaterialMainService materialMainService;
@Resource
@Qualifier("syncOAThreadPool")
ThreadPoolTaskExecutor syncOAThreadPool;
/**
* 申请物料
*
@ -202,7 +209,7 @@ public class MaterialService {
materialMainService.saveOrUpdateBatch(resultList);
initCategoryInfo(syncOaEnts);
//同步OA
sysnToOa(syncOaEnts);
CompletableFuture.runAsync(() -> sysnToOa(syncOaEnts),syncOAThreadPool);
return result;
}

View File

@ -1,10 +1,13 @@
package com.nflg.product.bomnew.util;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.nflg.product.base.core.exception.NflgBusinessException;
import lombok.extern.slf4j.Slf4j;
import nflg.product.common.constant.STATE;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import java.io.File;
@ -23,6 +26,8 @@ public class HttpUtils {
//请求类
private OkHttpClient okHttpClient;
private static final Logger LOGGER = LoggerFactory.getLogger(HttpUtils.class);
//请求头类型
private final MediaType jsonMediaType = MediaType.parse("application/json;charset=utf-8");
@ -210,13 +215,22 @@ public class HttpUtils {
* @throws IOException e
*/
public String doPost(String url, String json) throws IOException {
System.out.println(url);
System.out.println(json);
Response response = doPostRtnRsp(url, json);
if (response == null || response.body() == null) {
return null;
String ret = null;
String traceId = IdWorker.getIdStr();
LOGGER.info(traceId + "http请求地址" + url + ",参数:" + json);
long start = System.currentTimeMillis();
try (Response response = doPostRtnRsp(url, json)) {
if (response == null || response.body() == null) {
return null;
}
ret = response.body().string();
} catch (Exception ex) {
LOGGER.error(traceId + "http请求出错", ex);
} finally {
long end = System.currentTimeMillis();
LOGGER.info(traceId + "http请求耗时" + (end - start) + "毫秒,返回:" + ret);
}
return response.body().string();
return ret;
}
/**