优化代码

This commit is contained in:
谷成伟 2024-12-23 10:33:46 +08:00
parent d008a10f22
commit df61607281
15 changed files with 29 additions and 166 deletions

View File

@ -1,33 +0,0 @@
package com.das.common.log;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import java.util.concurrent.Executor;
/**
* @author chenhaojie
*
*/
@Slf4j
public class MdcExecutor implements Executor {
private final Executor executor;
public MdcExecutor(Executor executor) {
this.executor = executor;
}
@Override
public void execute(Runnable command) {
final String requestId = MDC.get("REQUEST_ID");
executor.execute(() -> {
MDC.put("REQUEST_ID", requestId);
try {
command.run();
} finally {
MDC.remove("REQUEST_ID");
}
});
}
}

View File

@ -1,32 +0,0 @@
package com.das.common.log;
import java.util.UUID;
/**
* @author chenhaojie
*
*/
public class RequestIdUtils {
private static final ThreadLocal<UUID> requestIdHolder = new ThreadLocal<>();
private RequestIdUtils() {
}
public static void generateRequestId() {
requestIdHolder.set(UUID.randomUUID());
}
public static void generateRequestId(UUID uuid) {
requestIdHolder.set(uuid);
}
public static UUID getRequestId() {
return requestIdHolder.get();
}
public static void removeRequestId() {
requestIdHolder.remove();
}
}

View File

@ -1,74 +0,0 @@
package com.das.common.log;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import java.util.UUID;
/**
* @author chenhaojie
*
*/
@Slf4j
public class RequestLogInterceptor implements HandlerInterceptor {
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
String servletPath = request.getServletPath();
log.info("preHandle 后置处理----------");
log.info("servletPath:{}", servletPath);
RequestIdUtils.removeRequestId();
MDC.clear();
}
/**
* 获取RequestId
* 优先从header头获取如果没有则自己生成
* @return RequestId
*/
private String getRequestId(){
// 因为如果有网关则一般会从网关传递过来所以优先从header头获取
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if(attributes != null && StringUtils.hasText(attributes.getRequest().getHeader("x-request-id"))) {
HttpServletRequest request = attributes.getRequest();
String requestId = request.getHeader("x-request-id");
UUID uuid = UUID.fromString(requestId);
RequestIdUtils.generateRequestId(uuid);
return requestId;
}
UUID existUUID = RequestIdUtils.getRequestId();
if(existUUID != null){
return existUUID.toString();
}
RequestIdUtils.generateRequestId();
return RequestIdUtils.getRequestId().toString();
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String servletPath = request.getServletPath();
// 生成RequestId
String requestId = this.getRequestId();
// 配置日志文件打印 REQUEST_ID
MDC.put("REQUEST_ID", requestId);
log.info("servletPath:{}", servletPath);
log.info("preHandle 前置处理----------");
return true;
}
}

View File

@ -15,10 +15,6 @@ import java.util.stream.Collectors;
@Slf4j
public class CommonFunction {
/**
* description:定义一个System.out.println纯属个人习惯
**/
private static int objectNullSystemOutFlag = 0;
/**
* description:判断当前对象是否为空包括所有属性为空
@ -51,6 +47,10 @@ public class CommonFunction {
//得到属性名
fieldName = field.getName();
//打印输出(调试用可忽略)
/**
* description:定义一个System.out.println纯属个人习惯
**/
int objectNullSystemOutFlag = 0;
if (objectNullSystemOutFlag == 1) {
System.out.println("属性类型:" + fieldType + ",属性名:" + fieldName + ",属性值:" + fieldValue);
}

View File

@ -45,7 +45,8 @@ public class IotModelCacheImpl implements IotModelCache {
@PreDestroy
public void destroy(){
iotFieldsMap.clear();
iotModelInfoIdMap.clear();
}
@Override

View File

@ -23,12 +23,10 @@ import java.util.Map;
@Slf4j
public class FunctionWindSpeedFactor extends AbstractFunction {
private DataService dataService = null;
private CacheService cacheService = null;
public FunctionWindSpeedFactor(DataService dataService, CacheService cacheService) {
this.dataService = dataService;
public FunctionWindSpeedFactor(CacheService cacheService) {
this.cacheService = cacheService;
}

View File

@ -136,7 +136,7 @@ public class CalcService {
FunctionOffsetDate offsetDate = new FunctionOffsetDate();
aviator.addFunction(offsetDate);
FunctionWindSpeedFactor windSpeedFactor = new FunctionWindSpeedFactor(dataService,cacheService);
FunctionWindSpeedFactor windSpeedFactor = new FunctionWindSpeedFactor(cacheService);
aviator.addFunction(windSpeedFactor);
FunctionIsOnline isOnline = new FunctionIsOnline(adminRedisTemplate, cacheService);

View File

@ -38,6 +38,7 @@ public class DataController {
public R<Map<String,Map<String,Object>>> querySnapshotValues(@RequestBody @Valid List<SnapshotValueQueryParam> param) {
if (log.isDebugEnabled()){
log.debug("/api/rtdbsvr/snapshot is calling");
log.debug("request params: {}", param);
}
return R.success(dataService.querySnapshotValues(param));
}
@ -50,7 +51,8 @@ public class DataController {
@PostMapping("/history")
public R<Map<String, Map<String, Map<String, Object>>>> queryTimeSeriesValues(@RequestBody @Valid TSValueQueryParam param) {
if (log.isDebugEnabled()){
log.debug("/api/rtdbsvr/timeseries is calling");
log.debug("/api/rtdbsvr/history is calling");
log.debug("request params: {}", param);
}
return R.success(dataService.queryTimeSeriesValues(param));
}
@ -63,7 +65,8 @@ public class DataController {
@PostMapping("/windows")
public R<Map<String, Map<String, Map<String, Object>>>> queryWindowsValues(@RequestBody @Valid WindowValueQueryParam param) {
if (log.isDebugEnabled()){
log.debug("/api/rtdbsvr/timeseries is calling");
log.debug("/api/rtdbsvr/windows is calling");
log.debug("request params: {}", param);
}
return R.success(dataService.queryWindowsValues(param));
}

View File

@ -92,7 +92,7 @@ public class TDEngineService {
log.info(sb.toString());
pstmt.executeUpdate(sb.toString());
} catch (Exception e) {
log.error("创建超级表失败,失败原因{}", e);
log.error("创建超级表失败", e);
}
}
} catch (Exception ignored) {
@ -114,7 +114,7 @@ public class TDEngineService {
log.info(sb.toString());
pstmt.executeUpdate(sb.toString());
} catch (Exception e) {
log.error("创建超级表失败,失败原因{}", e);
log.error("创建[计算量]超级表失败", e);
}
} catch (Exception ignored) {
@ -141,7 +141,7 @@ public class TDEngineService {
try {
pstmt.executeUpdate(sb.toString());
} catch (Exception e) {
log.error("新增超级表列失败:{},失败原因{}", sb, e);
log.error(String.format("新增超级表列失败:%s", sb.toString()), e);
}
}
@ -167,7 +167,7 @@ public class TDEngineService {
try {
pstmt.executeUpdate(sb.toString());
} catch (Exception e) {
log.error("删除超级表列失败:{},失败原因{}", sb, e);
log.error(String.format("删除超级表列失败:%s", sb.toString()), e);
}
} catch (Exception ignored) {
@ -189,7 +189,7 @@ public class TDEngineService {
try {
pstmt.executeUpdate(sb.toString());
} catch (Exception e) {
log.error("删除超级表失败:{},失败原因{}", sb, e);
log.error(String.format("删除超级表失败:%s", sb.toString()), e);
}
} catch (Exception ignored) {
@ -963,7 +963,7 @@ public class TDEngineService {
Statement pstmt = conn.createStatement()) {
pstmt.executeUpdate(sb.toString());
} catch (Exception e) {
log.error("新增超级表列失败:{},失败原因{}", sb, e);
log.error(String.format("新增超级表列失败:%s", sb.toString()), e);
}
}

View File

@ -123,7 +123,7 @@ public class DataServiceImpl implements DataService {
}
});
long end = System.currentTimeMillis();
log.debug("读取快照{}个,耗时: {}秒", paramList.size(), (end - start) / 1000.0);
log.debug("querySnapshotValues {}个,耗时: {}秒", paramList.size(), (end - start) / 1000.0);
return result;
}
@ -149,7 +149,7 @@ public class DataServiceImpl implements DataService {
result.putAll(values);
}
Long end = System.currentTimeMillis();
log.debug("读取快照{}个,耗时: {}秒", param.getDevices().size(), (end-start)/ 1000.0);
log.debug("queryTimeSeriesValues {}个,耗时: {}秒", param.getDevices().size(), (end-start)/ 1000.0);
return result;
}
@ -178,7 +178,7 @@ public class DataServiceImpl implements DataService {
result.putAll(values);
}
Long end = System.currentTimeMillis();
log.debug("读取快照{}个,耗时: {}秒", param.getDevices().size(), (end-start)/ 1000.0);
log.debug("queryTimeSeriesValues {}个,耗时: {}秒", param.getDevices().size(), (end-start)/ 1000.0);
return result;
}

View File

@ -123,7 +123,7 @@ public class MinioViewsServcie {
.stream(inputStream, inputStream.available(), -1)
.build());
}catch (Exception e){
log.error("minio文件上传失败{}", e);
log.error("minio文件上传失败", e);
}
}

View File

@ -194,7 +194,7 @@ public class FaultRecorderServiceImpl implements FaultRecorderService {
resultMap = parseFile(fileStream, fdrFormatVo.getTimeFormat(), fdrFormatVo.getDelimiter(), fdrFormatVo.getValidStartLine());
} catch (Exception e) {
log.error("文件解析异常{}",e);
log.error("文件解析异常",e);
throw new ServiceException("文件解析异常,请检查配置");
}
return resultMap;
@ -238,7 +238,7 @@ public class FaultRecorderServiceImpl implements FaultRecorderService {
}
stringListMap = parseDataCurve(result, timeFormat);
} catch (Exception e) {
log.error("文件解析失败{}", e);
log.error("文件解析失败", e);
throw new ServiceException("文件解析失败");
}
return stringListMap;

View File

@ -407,7 +407,7 @@ public class NodeMessageServiceImpl extends TextWebSocketHandler implements Node
try {
tdEngineService.updateDeviceEventValues(valueList);
} catch (Exception e) {
log.error("事件信息存入Td失败,失败原因{}", e);
log.error("事件信息存入Td失败,失败原因", e);
}
}

View File

@ -551,7 +551,7 @@ public class SysNodeServiceImpl implements SysNodeService {
})
.anyMatch(order -> !orderSet.add(order));
}catch (Exception e){
log.error("校验order不重复失败:{}",e);
log.error("校验order不重复失败",e);
}
return orderRepeated;
}

View File

@ -86,7 +86,7 @@ public class PlcLogsServiceImpl implements PlcLogService {
resultMap = parseFile(fileStream, fdrFormatVo.getTimeFormat(), fdrFormatVo.getDelimiter(), fdrFormatVo.getValidStartLine());
} catch (Exception e) {
log.error("文件解析异常{}",e);
log.error("文件解析异常",e);
throw new ServiceException("文件解析异常,请检查配置");
}
return resultMap;
@ -162,7 +162,7 @@ public class PlcLogsServiceImpl implements PlcLogService {
}
stringListMap = parseDataCurve(result, timeFormat);
} catch (Exception e) {
log.error("文件解析失败{}", e);
log.error("文件解析失败", e);
throw new ServiceException("文件解析失败");
}
return stringListMap;