fix(framework):修复异常处理和日期转换中的线程安全问题

- 移除 BadRequestException 中不必要的构造函数注释
- 在 ExecutionTimeStatisticsFaceAspect 中使用 error 级别记录异常日志
- 将 DateStringArraysToListDateConverter 和 DateStringToDateConverter 中的 SimpleDateFormat 改为 ThreadLocal以确保线程安全
- 简化 CsWsServer 中 stopTask 方法的异常处理逻辑
- 修正 CsFileUtil 中时间格式化字符串,将小写 hh 改为大写 HH 以正确表示24小时制
This commit is contained in:
2025-10-27 14:30:07 +08:00
parent 40c363e17d
commit 9492dd5e43
6 changed files with 7 additions and 12 deletions
@@ -39,7 +39,6 @@ public class BadRequestException extends RuntimeException {
} }
public BadRequestException(Throwable cause) { public BadRequestException(Throwable cause) {
// FIXME 20250827 修复完整异常链暴露给用户的问题
super(cause.getMessage()); super(cause.getMessage());
this.status = BAD_REQUEST.value(); this.status = BAD_REQUEST.value();
} }
@@ -42,7 +42,7 @@ public class ExecutionTimeStatisticsFaceAspect {
return proceed; return proceed;
} catch (Exception e) { } catch (Exception e) {
if (executionTimeStatisticsFace.showErrLog()) { if (executionTimeStatisticsFace.showErrLog()) {
log.info("[{}] 接口执行失败,耗时: {} ms", executionTimeStatisticsFace.description(), timeInterval.intervalMs()); log.error("[{}] 接口执行失败,耗时: {} ms", executionTimeStatisticsFace.description(), timeInterval.intervalMs(), e);
} }
throw e; throw e;
} }
@@ -29,7 +29,7 @@ import java.util.stream.Collectors;
@Component @Component
public class DateStringArraysToListDateConverter implements Converter<String[], List<Date>> { public class DateStringArraysToListDateConverter implements Converter<String[], List<Date>> {
private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static final ThreadLocal<SimpleDateFormat> SDF = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
@Override @Override
public List<Date> convert(String[] source) { public List<Date> convert(String[] source) {
@@ -38,7 +38,7 @@ public class DateStringArraysToListDateConverter implements Converter<String[],
} }
return Arrays.stream(source).map(s -> { return Arrays.stream(source).map(s -> {
try { try {
return sdf.parse(s); return SDF.get().parse(s);
} catch (ParseException e) { } catch (ParseException e) {
throw new IllegalArgumentException("Invalid date string: " + s); throw new IllegalArgumentException("Invalid date string: " + s);
} }
@@ -26,7 +26,7 @@ import java.util.Date;
@Component @Component
public class DateStringToDateConverter implements Converter<String, Date> { public class DateStringToDateConverter implements Converter<String, Date> {
private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static final ThreadLocal<SimpleDateFormat> SDF = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
@Override @Override
public Date convert(String source) { public Date convert(String source) {
@@ -34,7 +34,7 @@ public class DateStringToDateConverter implements Converter<String, Date> {
return null; return null;
} }
try { try {
return sdf.parse(source); return SDF.get().parse(source);
} catch (ParseException e) { } catch (ParseException e) {
throw new IllegalArgumentException("Invalid date string: " + source); throw new IllegalArgumentException("Invalid date string: " + source);
} }
@@ -169,11 +169,7 @@ public class CsWsServer {
public void stopTask(String sid) { public void stopTask(String sid) {
Thread thread = getTaskThread(this.sid); Thread thread = getTaskThread(this.sid);
if (thread != null) { if (thread != null) {
try {
thread.stop(); thread.stop();
} catch (Exception e) {
// ignore
}
} }
} }
} }
@@ -152,7 +152,7 @@ public final class CsFileUtil extends cn.hutool.core.io.FileUtil {
*/ */
public static File upload(MultipartFile file, String filePath) { public static File upload(MultipartFile file, String filePath) {
Date date = new Date(); Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmssS"); SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssS");
// 过滤非法文件名 // 过滤非法文件名
String name = getPrefix(verifyFilename(file.getOriginalFilename())); String name = getPrefix(verifyFilename(file.getOriginalFilename()));
String suffix = getSuffix(file.getOriginalFilename()); String suffix = getSuffix(file.getOriginalFilename());