From 7b5ab4f33889e02bd97ce0994b29b1dd0ed68009 Mon Sep 17 00:00:00 2001
From: 6c <420680616@qq.com>
Date: 星期一, 22 九月 2025 15:48:43 +0800
Subject: [PATCH] 请求历史特殊处理-若是多次维修导致报表库数据重复,那么按照主键取最新数据
---
product-server-data-center/src/main/java/com/product/data/center/utils/CustomLock.java | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 57 insertions(+), 0 deletions(-)
diff --git a/product-server-data-center/src/main/java/com/product/data/center/utils/CustomLock.java b/product-server-data-center/src/main/java/com/product/data/center/utils/CustomLock.java
index 331802b..6336553 100644
--- a/product-server-data-center/src/main/java/com/product/data/center/utils/CustomLock.java
+++ b/product-server-data-center/src/main/java/com/product/data/center/utils/CustomLock.java
@@ -1,11 +1,15 @@
package com.product.data.center.utils;
import cn.hutool.core.collection.ConcurrentHashSet;
+import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ArrayUtil;
import com.product.core.spring.context.SpringMVCContextHolder;
import org.apache.commons.lang3.StringUtils;
+import javax.swing.*;
+import java.text.DateFormat;
+import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@@ -19,6 +23,7 @@
private Map<String, Long> obj = new ConcurrentHashMap<>();
+ private Map<String, Long> threadObj = new ConcurrentHashMap<>();
private long detectionWaitTime = 1000L;
@@ -41,12 +46,64 @@
return this.tryLock(ArrayUtil.join(key, ","));
}
+ public static void main(String[] args) {
+ long timemillis = 1709654400039L;
+ //杞崲涓哄勾鏈堟棩鏃跺垎绉�
+ String format = DateUtil.format(DateUtil.date(timemillis), "yyyy-MM-dd HH:mm:ss");
+
+ System.out.println(format);
+ }
+
+ public boolean tryLock(String key, long timeoutMinute) {
+ if (!StringUtils.isEmpty(key)) {
+ key = key.toUpperCase();
+ synchronized (key.intern()) {
+ if (!this.obj.containsKey(key)) {
+ lock(key);
+ //鑾峰彇褰撳墠绾跨▼
+ long id = Thread.currentThread().getId();
+ threadObj.put(key, id);
+ return true;
+ } else if (timeoutMinute > 0) {
+ //鑾峰彇閿佽繘鍏ュ紑濮嬫椂闂�
+ long startTime = obj.get(key);
+ //鑾峰彇褰撳墠鏃堕棿
+ long currentTime = System.currentTimeMillis();
+
+ //灏嗗垎閽熻浆鎹负姣
+ long timeout = timeoutMinute * 60 * 1000;
+ //濡傛灉褰撳墠鏃堕棿鍑忓幓寮�濮嬫椂闂村ぇ浜庣瓑浜庤秴鏃舵椂闂�
+ if (currentTime - startTime >= timeout) {
+ long threadId = threadObj.get(key);
+ //鏍规嵁绾跨▼id鑾峰彇绾跨▼
+ Thread[] threads = ThreadUtil.getThreads();
+ for (Thread thread : threads) {
+ if (thread.getId() == threadId) {
+ //涓柇绾跨▼
+ ThreadUtil.interrupt(thread, true);
+ SpringMVCContextHolder.getSystemLogger().error("绾跨▼锛�" + thread.getName() + "瓒呮椂琚腑鏂�");
+ return tryLock(key, timeoutMinute);
+ }
+ }
+ return true;
+ }
+
+ }
+ }
+ }
+ return false;
+ }
+
+
public boolean tryLock(String key) {
if (!StringUtils.isEmpty(key)) {
key = key.toUpperCase();
synchronized (key.intern()) {
if (!this.obj.containsKey(key)) {
lock(key);
+ //鑾峰彇褰撳墠绾跨▼
+ long id = Thread.currentThread().getId();
+ threadObj.put(key, id);
return true;
}
}
--
Gitblit v1.9.2