From 3c2ee852994cc3c4d861484f12d972b42cb0ffee Mon Sep 17 00:00:00 2001
From: eight <641137800@qq.com>
Date: 星期三, 21 八月 2024 16:05:01 +0800
Subject: [PATCH] 并发问题 修改
---
jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/listener/MySpringEventListener.java | 9 ++--
jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/service/queue/queueServiceImpl.java | 66 ++++++++++++++++++++-------------
jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/dal/mysql/queue/queueMapper.java | 8 ++++
3 files changed, 53 insertions(+), 30 deletions(-)
diff --git a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/dal/mysql/queue/queueMapper.java b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/dal/mysql/queue/queueMapper.java
index eb6ac45..e121bac 100644
--- a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/dal/mysql/queue/queueMapper.java
+++ b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/dal/mysql/queue/queueMapper.java
@@ -79,4 +79,12 @@
" order by seq_num" +
"</script>")
List<QueueDO> getDoctorQueueByStatus(@Param("roomId")Long roomId, @Param("bedNo")String bedNo, @Param("statusList")List<Byte> statusList);
+
+ @Update("update lihu.queue set status = #{newStatus}, room_id = #{roomId}, room_name = #{roomName}, \n" +
+ " bed_no = #{bedNo}, seq_num = #{seqNum} where id = \n" +
+ " (select a.id from \n" +
+ " (select id from lihu.queue where status = #{curStatus} order by book_timeslot, create_time limit 1) a)")
+ Integer preemptPatient(@Param("roomId")Long roomId, @Param("roomName")String roomName, @Param("bedNo")String bedNo,
+ @Param("seqNum")Integer seqNum, @Param("curStatus")Byte curStatus, @Param("newStatus")Byte newStatus);
+
}
diff --git a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/MySpringEventLister.java b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/listener/MySpringEventListener.java
similarity index 71%
rename from jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/MySpringEventLister.java
rename to jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/listener/MySpringEventListener.java
index 045f666..c0acc02 100644
--- a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/MySpringEventLister.java
+++ b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/listener/MySpringEventListener.java
@@ -1,21 +1,22 @@
-package cn.lihu.jh.module.ecg;
+package cn.lihu.jh.module.ecg.listener;
import cn.lihu.jh.module.ecg.service.queue.QueueService;
import org.springframework.boot.context.event.ApplicationStartedEvent;
-import org.springframework.context.ApplicationListener;
+import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
-public class MySpringEventLister implements ApplicationListener<ApplicationStartedEvent> {
+public class MySpringEventListener {
@Resource
private QueueService queueService;
- @Override
+ @EventListener
public void onApplicationEvent(ApplicationStartedEvent event) {
System.out.println("搴旂敤鍚姩瀹屾垚锛岄�氱煡鐩戝惉鍣ㄦ墽琛岀紦瀛橀鍔犺浇鎿嶄綔");
queueService.initQueue();
+ queueService.hurryup();
}
}
diff --git a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/service/queue/queueServiceImpl.java b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/service/queue/queueServiceImpl.java
index dd6ba85..ceb0e55 100644
--- a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/service/queue/queueServiceImpl.java
+++ b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/service/queue/queueServiceImpl.java
@@ -17,10 +17,8 @@
import javax.annotation.Resource;
import java.util.*;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.PriorityBlockingQueue;
+import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
-import java.util.concurrent.atomic.AtomicReference;
import static cn.lihu.jh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.lihu.jh.module.ecg.enums.ErrorCodeConstants.*;
@@ -40,6 +38,8 @@
PriorityBlockingQueue<BedQueueBO> priorityQueue = new PriorityBlockingQueue<>();
ConcurrentHashMap<String, BedQueueBO > mapBedVsQueue = new ConcurrentHashMap<>();
+
+ ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
@Resource
private queueMapper queueMapper;
@@ -116,9 +116,7 @@
});
Integer num = queueMapper.getMaxSeqNum();
- curSeqNum = new AtomicInteger(null == num ? 1 : ++num);
-
- hurryup();
+ curSeqNum = new AtomicInteger(null == num ? 0 : num);
}
public void reorderQueue() {
@@ -146,34 +144,50 @@
if (null == bedQueueBO)
return;
- if (bedQueueBO.queueNum.get() == bedQueueBO.maxQueueNum)
+ int curQueueNum = bedQueueBO.queueNum.get();
+ if (curQueueNum > bedQueueBO.maxQueueNum)
+ throw new RuntimeException("exceed max queue number!");
+
+ if (curQueueNum == bedQueueBO.maxQueueNum)
return;
- QueueDO queue = queueMapper.getFirstInQueueByStatus(QueueStatusEnum.WAITING.getStatus());
- if (null == queue)
- return;
+ Integer updateNum = queueMapper.preemptPatient(
+ bedQueueBO.getRoomId(),
+ bedQueueBO.getRoomName(),
+ bedQueueBO.getBedNo(),
+ curSeqNum.get() + 1,
+ QueueStatusEnum.WAITING.getStatus(),
+ QueueStatusEnum.READY.getStatus());
- queue.setStatus(QueueStatusEnum.READY.getStatus()); //鍊欒瘖鍑嗗涓�
- queue.setRoomId(bedQueueBO.getRoomId());
- queue.setRoomName(bedQueueBO.getRoomName());
- queue.setBedNo(bedQueueBO.getBedNo());
- queue.setSeqNum(curSeqNum.get());
- queueMapper.updateById(queue); // queue.getId();
+ // 娌℃湁鎶㈠埌鎺掗槦鎮h��
+ if (null == updateNum || 0 == updateNum) {
+ return;
+ }
curSeqNum.getAndIncrement();
- bedQueueBO.queueNum.getAndIncrement();
- BedQueueBO bedQueueBO2 = priorityQueue.poll();
- priorityQueue.offer(bedQueueBO2);
+
+ // 鍙兘宸茬粡銆愬苟鍙戠殑銆戝湪 nextPatient 涓敼鍙樹簡鍊�
+ bedQueueBO.queueNum.incrementAndGet();
+
+ // 鍙兘宸茬粡銆愬苟鍙戠殑銆戝湪 nextPatient 涓敼鍙樹簡浼樺厛闃熷垪椤哄簭
+ priorityQueue.remove(bedQueueBO);
+ priorityQueue.offer(bedQueueBO);
}
}
+ /**
+ * 棰勭害纭鍚庣殑鎺掗槦
+ * @param queueSaveReqVO
+ */
@Override
public void queue(QueueSaveReqVO queueSaveReqVO) {
queueSaveReqVO.setStatus(QueueStatusEnum.WAITING.getStatus()); //鎺掗槦涓�
QueueDO queue = BeanUtils.toBean(queueSaveReqVO, QueueDO.class);
- queueMapper.insert(queue); // queue.getId();
+ queueMapper.insert(queue);
- hurryup();
+ singleThreadExecutor.submit( () -> {
+ hurryup();
+ });
}
private void nextPatient(Long roomId, String bedNo) {
@@ -183,18 +197,19 @@
// 浼樺厛闃熷垪涓� 璇ュ伐浣� 灏辫瘖鍑嗗涓汉鐨勬暟閲� 鍑忎竴
BedQueueBO bo = mapBedVsQueue.get(String.format("%09d%s", roomId, bedNo));
- boolean breturn = priorityQueue.remove(bo);
- bo.queueNum.getAndDecrement();
+ bo.queueNum.getAndDecrement(); // 鍙兘宸茬粡銆愬苟鍙戠殑銆戝湪 hurry-up 涓敼鍙樹簡鍊�
+ priorityQueue.remove(bo);
priorityQueue.offer(bo);
- hurryup();
+ singleThreadExecutor.submit( () -> {
+ hurryup();
+ });
}
public void finishNextPatient(Long roomId, String bedNo) {
// 浠� DB 鎶� 灏辫瘖涓殑浜� 璁剧疆涓哄氨璇婂畬鎴�
Integer ret = queueMapper.updateQueueStatus(roomId, bedNo,
QueueStatusEnum.ONSTAGE.getStatus(), QueueStatusEnum.FINISH.getStatus());
- System.out.println("瀹屾垚鏁伴噺: " + ret);
nextPatient(roomId, bedNo);
}
@@ -203,7 +218,6 @@
// 浠� DB 鎶� 灏辫瘖涓殑浜� 璁剧疆涓鸿繃鍙�
Integer ret = queueMapper.updateQueueStatus(roomId, bedNo,
QueueStatusEnum.ONSTAGE.getStatus(), QueueStatusEnum.PASSED.getStatus());
- System.out.println("瀹屾垚鏁伴噺: " + ret);
nextPatient(roomId, bedNo);
}
--
Gitblit v1.9.3