You've already forked FrameTour-BE
refactor(clickhouse): 修复日期格式化器线程安全问题
- 移除静态 SimpleDateFormat 实例,避免线程安全问题 - 添加上海时区配置确保日期格式化一致性 - 创建新的日期和日期时间格式化器方法 - 修改格式化方法使用新创建的格式化器实例 - 更新每日扫描统计查询中的日期格式化逻辑
This commit is contained in:
@@ -15,6 +15,7 @@ import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* ClickHouse 统计数据查询服务实现
|
||||
@@ -28,8 +29,25 @@ import java.util.List;
|
||||
@ConditionalOnProperty(prefix = "clickhouse", name = "enabled", havingValue = "true")
|
||||
public class ClickHouseStatsQueryServiceImpl implements StatsQueryService {
|
||||
|
||||
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
|
||||
private static final SimpleDateFormat DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
private static final TimeZone CLICKHOUSE_TIMEZONE = TimeZone.getTimeZone("Asia/Shanghai");
|
||||
|
||||
/**
|
||||
* 创建日期格式化器(SimpleDateFormat 非线程安全,每次创建新实例)
|
||||
*/
|
||||
private SimpleDateFormat createDateFormat() {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
sdf.setTimeZone(CLICKHOUSE_TIMEZONE);
|
||||
return sdf;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建日期时间格式化器
|
||||
*/
|
||||
private SimpleDateFormat createDateTimeFormat() {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
sdf.setTimeZone(CLICKHOUSE_TIMEZONE);
|
||||
return sdf;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
@Qualifier("clickHouseJdbcTemplate")
|
||||
@@ -51,14 +69,14 @@ public class ClickHouseStatsQueryServiceImpl implements StatsQueryService {
|
||||
* 格式化日期时间为 ClickHouse 可识别的字符串
|
||||
*/
|
||||
private String formatDateTime(Date date) {
|
||||
return date != null ? "'" + DATETIME_FORMAT.format(date) + "'" : null;
|
||||
return date != null ? "'" + createDateTimeFormat().format(date) + "'" : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期为 ClickHouse 可识别的字符串
|
||||
*/
|
||||
private String formatDate(Date date) {
|
||||
return date != null ? "'" + DATE_FORMAT.format(date) + "'" : null;
|
||||
return date != null ? "'" + createDateFormat().format(date) + "'" : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -304,8 +322,9 @@ public class ClickHouseStatsQueryServiceImpl implements StatsQueryService {
|
||||
|
||||
@Override
|
||||
public List<HashMap<String, Object>> getDailyScanStats(Long brokerId, Date startTime, Date endTime) {
|
||||
String startDateStr = DATE_FORMAT.format(startTime);
|
||||
String endDateStr = DATE_FORMAT.format(endTime);
|
||||
SimpleDateFormat dateFormat = createDateFormat();
|
||||
String startDateStr = dateFormat.format(startTime);
|
||||
String endDateStr = dateFormat.format(endTime);
|
||||
String startDateTimeStr = "'" + startDateStr + " 00:00:00'";
|
||||
String endDateTimeStr = "'" + endDateStr + " 23:59:59'";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user