From 9ac9e60b40629cf3b3744c5fd36a307e90a98060 Mon Sep 17 00:00:00 2001 From: fengyuan_yang <1976974459@qq.com> Date: Wed, 18 Nov 2026 11:18:19 +0800 Subject: [PATCH] =?UTF-8?q?2026-11-18=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/schedule/SplitRollWaitProbe.java | 133 ------------------ .../service/impl/ScheduleServiceImpl.java | 15 +- .../mapper/schedule/SchedulingMapper.xml | 10 +- 3 files changed, 7 insertions(+), 151 deletions(-) delete mode 100644 src/main/java/com/gaotao/modules/schedule/SplitRollWaitProbe.java diff --git a/src/main/java/com/gaotao/modules/schedule/SplitRollWaitProbe.java b/src/main/java/com/gaotao/modules/schedule/SplitRollWaitProbe.java deleted file mode 100644 index 08c9436..0000000 --- a/src/main/java/com/gaotao/modules/schedule/SplitRollWaitProbe.java +++ /dev/null @@ -1,133 +0,0 @@ -package com.gaotao.modules.schedule; - -import org.springframework.jdbc.datasource.DataSourceUtils; - -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.Statement; -import java.util.concurrent.Callable; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; - -/** - * 创建分卷的查询若超过 1 秒,用另一条连接记下当前会话在等什么。 - * 业务连接自己被堵住时,看不到自己的等待。 - */ -public final class SplitRollWaitProbe { - - private static final ScheduledExecutorService EXECUTOR = Executors.newScheduledThreadPool(2, runnable -> { - Thread thread = new Thread(runnable, "split-roll-wait-probe"); - thread.setDaemon(true); - return thread; - }); - - private static final String WAIT_SQL = - "SELECT TOP 1 r.wait_type, r.wait_time, r.blocking_session_id, w.wait_resource, " + - "LEFT(REPLACE(REPLACE(ISNULL(bt.text, N''), CHAR(13), N' '), CHAR(10), N' '), 180) AS blocking_sql " + - "FROM sys.dm_exec_requests r " + - "LEFT JOIN sys.dm_os_waiting_tasks w ON w.session_id = r.session_id " + - "LEFT JOIN sys.dm_exec_requests b ON b.session_id = NULLIF(r.blocking_session_id, 0) " + - "OUTER APPLY sys.dm_exec_sql_text(b.sql_handle) bt " + - "WHERE r.session_id = ? " + - "ORDER BY CASE WHEN r.blocking_session_id > 0 THEN 0 ELSE 1 END, r.wait_time DESC"; - - private SplitRollWaitProbe() { - } - - public static T call(DataSource dataSource, Callable action, StringBuilder waitInfo) { - int spid = currentSpid(dataSource); - AtomicReference sample = new AtomicReference<>(); - ScheduledFuture future = null; - if (spid > 0 && dataSource != null) { - future = EXECUTOR.scheduleWithFixedDelay(() -> capture(dataSource, spid, sample), 1, 1, TimeUnit.SECONDS); - } - try { - return action.call(); - } catch (RuntimeException ex) { - throw ex; - } catch (Exception ex) { - throw new IllegalStateException(ex); - } finally { - if (future != null) { - future.cancel(false); - } - Sample found = sample.get(); - if (waitInfo != null && found != null && found.text != null && !found.text.isEmpty()) { - waitInfo.append(found.text); - } - } - } - - private static int currentSpid(DataSource dataSource) { - if (dataSource == null) { - return 0; - } - Connection connection = null; - try { - connection = DataSourceUtils.getConnection(dataSource); - try (Statement statement = connection.createStatement(); - ResultSet rs = statement.executeQuery("SELECT @@SPID")) { - return rs.next() ? rs.getInt(1) : 0; - } - } catch (Exception ex) { - return 0; - } finally { - DataSourceUtils.releaseConnection(connection, dataSource); - } - } - - private static void capture(DataSource dataSource, int spid, AtomicReference sample) { - try (Connection connection = dataSource.getConnection(); - PreparedStatement statement = connection.prepareStatement(WAIT_SQL)) { - statement.setInt(1, spid); - try (ResultSet rs = statement.executeQuery()) { - if (!rs.next()) { - return; - } - int waitTime = rs.getInt("wait_time"); - Sample previous = sample.get(); - if (previous != null && previous.waitTime >= waitTime) { - return; - } - String waitType = rs.getString("wait_type"); - int blockingSessionId = rs.getInt("blocking_session_id"); - String waitResource = rs.getString("wait_resource"); - String blockingSql = rs.getString("blocking_sql"); - StringBuilder text = new StringBuilder(); - text.append("等待=").append(waitType == null ? "" : waitType.trim()); - text.append(" 等待时长=").append(waitTime).append("毫秒"); - if (blockingSessionId > 0) { - text.append(" 阻塞会话=").append(blockingSessionId); - } - if (waitResource != null && !waitResource.trim().isEmpty()) { - text.append(" 等待资源=").append(waitResource.trim()); - } - if (blockingSql != null && !blockingSql.trim().isEmpty()) { - text.append(" 阻塞语句=").append(blockingSql.trim()); - } - Sample next = new Sample(); - next.waitTime = waitTime; - next.text = text.toString(); - sample.set(next); - } - } catch (Exception ex) { - Sample previous = sample.get(); - if (previous == null) { - Sample failed = new Sample(); - failed.waitTime = 0; - failed.text = "等待信息读取失败=" + ex.getClass().getSimpleName(); - sample.set(failed); - } - } - } - - private static final class Sample { - private int waitTime; - private String text; - } -} diff --git a/src/main/java/com/gaotao/modules/schedule/service/impl/ScheduleServiceImpl.java b/src/main/java/com/gaotao/modules/schedule/service/impl/ScheduleServiceImpl.java index aadccbc..d11679b 100644 --- a/src/main/java/com/gaotao/modules/schedule/service/impl/ScheduleServiceImpl.java +++ b/src/main/java/com/gaotao/modules/schedule/service/impl/ScheduleServiceImpl.java @@ -11,7 +11,6 @@ import com.gaotao.common.utils.R; import com.gaotao.modules.inboundNotification.service.impl.InboundNotificationServiceImpl; import com.gaotao.modules.rollNoReview.data.SFDCMaterialHistOutData; import com.gaotao.modules.schedule.CreateSplitRollTiming; -import com.gaotao.modules.schedule.SplitRollWaitProbe; import com.gaotao.modules.schedule.data.*; import com.gaotao.modules.schedule.data.dto.ScheduleDateShifDto; import com.gaotao.modules.schedule.data.dto.SfdcNewsfdcDto; @@ -59,7 +58,6 @@ import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; -import javax.sql.DataSource; import java.math.BigDecimal; import java.math.RoundingMode; import java.nio.charset.StandardCharsets; @@ -94,8 +92,6 @@ public class ScheduleServiceImpl implements ScheduleService { private static final String TODO_DETAIL_STAGE_TRIGGER = "TRIGGER"; private static final String TODO_DETAIL_STAGE_REPORT = "REPORT"; - @Autowired - private DataSource dataSource; @Autowired private ScheduleMapper scheduleMapper; @Autowired @@ -5011,15 +5007,8 @@ public class ScheduleServiceImpl implements ScheduleService { tempData.setRollNo(rollNoArr[i]); //查询当前的打印的数据 stepStart = System.currentTimeMillis(); - StringBuilder waitInfo = new StringBuilder(); - Map printMap = SplitRollWaitProbe.call(dataSource, - () -> schedulingMapper.getSfdcFlowLabelData(tempData), waitInfo); - long labelMs = System.currentTimeMillis() - stepStart; - String labelStep = "查询流转标签数据"; - if (labelMs >= 3000) { - labelStep = labelStep + " " + (waitInfo.length() > 0 ? waitInfo : "未采到等待信息"); - } - CreateSplitRollTiming.record(labelStep, labelMs); + Map printMap = schedulingMapper.getSfdcFlowLabelData(tempData); + CreateSplitRollTiming.record("查询流转标签数据", System.currentTimeMillis() - stepStart); //添加打印的数据 printList.add(printMap); diff --git a/src/main/resources/mapper/schedule/SchedulingMapper.xml b/src/main/resources/mapper/schedule/SchedulingMapper.xml index f294e52..6db2bb7 100644 --- a/src/main/resources/mapper/schedule/SchedulingMapper.xml +++ b/src/main/resources/mapper/schedule/SchedulingMapper.xml @@ -463,15 +463,15 @@ pt.DefaultLocationID locationId, st.SiteName siteName FROM SFDC_Rolls sfr - INNER JOIN ShopOrder so ON sfr.Site = so.Site AND sfr.OrderNo = so.OrderNo - INNER JOIN Part pt ON so.Site = pt.Site AND so.PartNo = pt.PartNo + INNER JOIN ShopOrder so ON so.Site = sfr.Site AND so.OrderNo = sfr.OrderNo + INNER JOIN Part pt ON pt.Site = so.Site AND pt.PartNo = CONVERT(varchar(30), so.PartNo) LEFT JOIN Site st ON st.SiteID = pt.Site - AND sfr.Site = #{site} + AND sfr.Site = CONVERT(varchar(4), #{site}) - AND sfr.OrderNo = #{orderNo} + AND sfr.OrderNo = CONVERT(varchar(20), #{orderNo}) - AND sfr.RollNo = #{rollNo} + AND sfr.RollNo = CONVERT(varchar(50), #{rollNo}) AND sfr.VirtualRollFlag = 'N'