Browse Source

ly 内存优化

ly 1 month ago
parent
commit
30d127d811

+ 20 - 2
master/src/main/java/com/ruoyi/common/utils/file/ExcelUtils.java

@@ -38,9 +38,10 @@ public class ExcelUtils {
         String fileName = file.getOriginalFilename();
         //创建Workbook工作薄对象,表示整个excel
         Workbook workbook = null;
+        InputStream is = null;
         try {
             //获取excel文件的io流
-            InputStream is = file.getInputStream();
+            is = file.getInputStream();
             //根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象
             if (fileName.endsWith("xls")) {
                 //2003
@@ -51,6 +52,14 @@ public class ExcelUtils {
             }
         } catch (IOException e) {
 //            log.error(e.getMessage());
+        } finally {
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    // 忽略关闭异常
+                }
+            }
         }
         return workbook;
     }
@@ -60,9 +69,10 @@ public class ExcelUtils {
         String fileName = file.getName();
         //创建Workbook工作薄对象,表示整个excel
         Workbook workbook = null;
+        InputStream is = null;
         try {
             //获取excel文件的io流
-            InputStream is = new FileInputStream(file);
+            is = new FileInputStream(file);
             //根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象
             if (fileName.endsWith("xls")) {
                 //2003
@@ -73,6 +83,14 @@ public class ExcelUtils {
             }
         } catch (IOException e) {
 //            log.error(e.getMessage());
+        } finally {
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    // 忽略关闭异常
+                }
+            }
         }
         return workbook;
     }

+ 17 - 4
master/src/main/java/com/ruoyi/project/production/service/impl/TDailyProductionReportServiceImpl.java

@@ -1,6 +1,7 @@
 package com.ruoyi.project.production.service.impl;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.text.SimpleDateFormat;
@@ -54,6 +55,9 @@ public class TDailyProductionReportServiceImpl implements ITDailyProductionRepor
     @Autowired
     private IMailService mailService;
 
+    @Autowired
+    private org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor threadPoolTaskExecutor;
+
     /**
      * 查询日期最新的每日生产报告
      *
@@ -194,9 +198,11 @@ public class TDailyProductionReportServiceImpl implements ITDailyProductionRepor
     public String importData(MultipartFile file) throws IOException {
         // 用户提示信息
         String message = "";
+        InputStream inputStream = null;
         try {
             // 获取用户上传的excel
-            Workbook wb = WorkbookFactory.create(file.getInputStream());
+            inputStream = file.getInputStream();
+            Workbook wb = WorkbookFactory.create(inputStream);
             // 获取第一个sheet
             Sheet sheet = wb.getSheetAt(0);
             // 用户上传的生产日报
@@ -865,10 +871,9 @@ public class TDailyProductionReportServiceImpl implements ITDailyProductionRepor
                         String staffName = staffmgr.getName();
                         String email = sysUser.getEmail();
                         if (email != null) {
-                            // 发送邮件
+                            // 发送邮件 - 使用线程池而非创建新线程,避免线程泄漏
                             DailyProductionReportMailThread mailThread = new DailyProductionReportMailThread(mailService, email, staffName, loginName);
-                            Thread thread = new Thread(mailThread);
-                            thread.start();
+                            threadPoolTaskExecutor.execute(mailThread);
                         }
                     }
                 }
@@ -877,6 +882,14 @@ public class TDailyProductionReportServiceImpl implements ITDailyProductionRepor
             message = "导入失败,请联系管理员!";
             e.printStackTrace();
         } finally {
+            // 关闭输入流,防止内存泄漏
+            if (inputStream != null) {
+                try {
+                    inputStream.close();
+                } catch (IOException e) {
+                    // 忽略关闭异常
+                }
+            }
             return message;
         }
     }

+ 5 - 3
master/src/main/java/com/ruoyi/project/training/spec/controller/scheduler/TStFeedbackScheduler.java

@@ -40,6 +40,9 @@ public class TStFeedbackScheduler {
     @Autowired
     private IMailService mailService;
 
+    @Autowired
+    private org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor threadPoolTaskExecutor;
+
     /**
      * 会议时间一周后给学员发送邮件
      * 定时任务触发条件:每日8:00
@@ -97,12 +100,11 @@ public class TStFeedbackScheduler {
             message.setUserId(successor.getUserId());
             message.setMsgTitle("您有一条系统消息");
             message.setMsgContent("您有一条季度汇报消息待查看");
-            // 邮件通知
+            // 邮件通知 - 使用线程池而非创建新线程,避免线程泄漏
             MeetingFeedbackMailThread meetingFeedbackMailThread = new MeetingFeedbackMailThread(
                     mailService, email, username, usernameEN, feedbackYear, feedbackSeason, meetingDateString,
                     mentorNamesString, sysMessageService, message);
-            Thread thread = new Thread(meetingFeedbackMailThread);
-            thread.start();
+            threadPoolTaskExecutor.execute(meetingFeedbackMailThread);
         }
     }