24282 2 yıl önce
ebeveyn
işleme
7b3ee7d518

+ 7 - 8
src/main/java/com/fjhx/email/config/TaskPoolConfig.java

@@ -29,11 +29,13 @@ public class TaskPoolConfig {
                 20,
                 60,
                 TimeUnit.SECONDS,
-                new ArrayBlockingQueue<>(30),
+                new ArrayBlockingQueue<>(20),
                 (runnable, executor) -> {
-                    // 等待5秒之后重新入线程池
-                    ThreadUtil.sleep(3000);
-                    executor.execute(runnable);
+                    if (!executor.isShutdown()) {
+                        // 等待1秒之后重新入线程池
+                        ThreadUtil.sleep(1000);
+                        executor.execute(runnable);
+                    }
                 }
         );
     }
@@ -49,10 +51,7 @@ public class TaskPoolConfig {
                 60,
                 TimeUnit.SECONDS,
                 new ArrayBlockingQueue<>(20),
-                (runnable, executor) -> {
-                    // 本线程执行
-                    runnable.run();
-                }
+                new ThreadPoolExecutor.CallerRunsPolicy()
         );
     }
 

+ 0 - 4
src/main/java/com/fjhx/email/config/mybatis/MybatisConfig.java

@@ -3,7 +3,6 @@ package com.fjhx.email.config.mybatis;
 import com.baomidou.mybatisplus.annotation.DbType;
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
 import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
-import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
 import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.context.annotation.Bean;
@@ -29,9 +28,6 @@ public class MybatisConfig {
         // 分页
         interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
 
-        // 乐观锁
-        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
-
         return interceptor;
     }
 

+ 61 - 66
src/main/java/com/fjhx/email/service/impl/CoreServiceImpl.java

@@ -39,6 +39,7 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Executor;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
 @Slf4j
@@ -56,11 +57,11 @@ public class CoreServiceImpl implements ApplicationRunner {
     /**
      * 累计同步次数
      */
-    private static int num = 0;
+    private static final AtomicInteger numCount = new AtomicInteger();
     /**
      * 单次同步邮件总次数
      */
-    private static int mailCount;
+    private static final AtomicInteger mailCount = new AtomicInteger();
 
     @Autowired
     private PlatformTransactionManager platformTransactionManager;
@@ -87,13 +88,6 @@ public class CoreServiceImpl implements ApplicationRunner {
     @Autowired
     private IMailService mailService;
 
-    /**
-     * 添加同步邮件数量
-     */
-    private static synchronized void addMailCount() {
-        mailCount++;
-    }
-
     @Override
     public void run(ApplicationArguments args) {
 
@@ -111,7 +105,7 @@ public class CoreServiceImpl implements ApplicationRunner {
             run(args);
             return;
         } catch (Exception e) {
-            log.error("解析错误", e);
+            log.error("其他错误", e);
             ThreadUtil.sleep(1000 * 30);
             run(args);
             return;
@@ -140,6 +134,8 @@ public class CoreServiceImpl implements ApplicationRunner {
 
         // 所有需要同步的邮件
         List<MailboxInfo> mailboxInfoList = new ArrayList<>(MailSyncInfo.mailboxInfoList);
+
+        // 所有需要同步的邮件数量
         int mailboxSize = mailboxInfoList.size();
 
         if (mailboxSize == 0) {
@@ -151,12 +147,10 @@ public class CoreServiceImpl implements ApplicationRunner {
         long start = System.currentTimeMillis();
 
         // 清空抓取邮件数
-        mailCount = 0;
+        mailCount.set(0);
 
         // 累计同步次数
-        num++;
-
-        log.info("开始第 {} 伦邮件同步,共同步 {} 个邮箱", num, mailboxSize);
+        log.info("第 {} 伦邮件同步开始,需要同步 {} 个邮箱", numCount.incrementAndGet(), mailboxSize);
 
         // 定义一个线程计数器
         CountDownLatch countDownLatch = new CountDownLatch(mailboxSize);
@@ -192,8 +186,8 @@ public class CoreServiceImpl implements ApplicationRunner {
         // 处理时间
         long handleTime = end - start;
 
-        log.info("第 {} 伦邮件同步完成,共同步 {} 个邮箱,{} 封邮件,总耗时 {}",
-                num, mailboxSize, mailCount, DateUtil.formatBetween(handleTime));
+        log.info("第 {} 伦邮件同步完成,已同步 {} 封邮件,总耗时 {}",
+                numCount.get(), mailCount.get(), DateUtil.formatBetween(handleTime));
 
         // 处理时间是否小于最小处理时间
         if (MailSyncInfo.minWaitingTime > handleTime) {
@@ -228,6 +222,7 @@ public class CoreServiceImpl implements ApplicationRunner {
             // 获取邮件连接
             store = EmailUtil.getIMAPStore(mailboxInfo);
 
+            // 邮件文件夹
             List<MailFolderInfo> mailFolderInfoList = mailboxInfo.getMailFolderInfoList();
 
             // 分别拉取每个文件夹邮件
@@ -238,12 +233,14 @@ public class CoreServiceImpl implements ApplicationRunner {
                     continue;
                 }
 
+                // 获取文件夹
                 IMAPFolder folder = (IMAPFolder) store.getFolder(mailFolder.getName());
 
+                // 只读方式打开收件箱
                 try {
-                    // 只读方式打开收件箱
                     folder.open(Folder.READ_ONLY);
                 } catch (FolderNotFoundException folderNotFoundException) {
+                    // 文件夹不存在跳过邮件同步
                     mailFolder.setSkip(true);
                     continue;
                 }
@@ -280,7 +277,7 @@ public class CoreServiceImpl implements ApplicationRunner {
 
         } catch (AuthenticationFailedException | MailConnectException exception) {
 
-            log.error("连接异常", exception);
+            log.error("连接异常,邮箱信息:{}", JSON.toJSONString(mailboxInfo), exception);
 
             // 添加异常次数
             map.merge(id, 1, Integer::sum);
@@ -293,8 +290,7 @@ public class CoreServiceImpl implements ApplicationRunner {
 
         } catch (Exception e) {
 
-            log.error("邮件同步出错,邮箱信息:{}", JSON.toJSONString(mailboxInfo));
-            e.printStackTrace();
+            log.error("邮件同步出错,邮箱信息:{}", JSON.toJSONString(mailboxInfo), e);
 
         } finally {
 
@@ -334,10 +330,13 @@ public class CoreServiceImpl implements ApplicationRunner {
         List<MailInfo> mailInfoList = new ArrayList<>();
 
         for (int i = messages.length - 1; i >= 0; i--) {
+
+            // 邮件
             IMAPMessage message = (IMAPMessage) messages[i];
 
             // 邮件接收时间
             Date receivedDate = message.getReceivedDate();
+
             // 邮件uid
             long uid = folder.getUID(message);
 
@@ -345,8 +344,9 @@ public class CoreServiceImpl implements ApplicationRunner {
             if (lastUid != null && Objects.equals(uid, lastUid)) {
                 break;
             }
+
             // 收件时间在上次收件时间之前,不继续找之前的邮件了
-            if (receivedDate.getTime() < lastReceivedDate.getTime()) {
+            if (receivedDate.before(lastReceivedDate)) {
                 break;
             }
 
@@ -361,9 +361,9 @@ public class CoreServiceImpl implements ApplicationRunner {
             // 保存发件人信息
             Address[] addresses = message.getFrom();
             if (addresses != null && addresses.length > 0) {
-                InternetAddress internetAddress = (InternetAddress) addresses[0];
-                mailInfo.setFromEmail(internetAddress.getAddress());
-                mailInfo.setFromPersonalName(internetAddress.getPersonal());
+                InternetAddress from = (InternetAddress) addresses[0];
+                mailInfo.setFromEmail(from.getAddress());
+                mailInfo.setFromPersonalName(from.getPersonal());
                 mailInfo.setFromType("from");
             } else {
                 InternetAddress sender = (InternetAddress) message.getSender();
@@ -378,7 +378,8 @@ public class CoreServiceImpl implements ApplicationRunner {
             mailInfoList.add(0, mailInfo);
 
             // 添加同步邮件总次数
-            addMailCount();
+            mailCount.incrementAndGet();
+
         }
 
         return mailInfoList;
@@ -388,51 +389,48 @@ public class CoreServiceImpl implements ApplicationRunner {
      * 保存邮件数据,更新文件夹最后同步邮件时间
      */
     private void saveMailInfo(MailboxInfo mailboxInfo, MailFolderInfo mailFolder, List<MailInfo> mailInfoList) {
-        TransactionStatus transactionStatus = platformTransactionManager.getTransaction(transactionDefinition);
-        try {
 
-            // 个人邮箱
-            if (mailboxInfo.getType() == 1) {
+        // 未抓取邮件直接跳过保存
+        if (mailInfoList.size() == 0) {
+            return;
+        }
 
-                if (mailInfoList.size() > 0) {
-
-                    MailInfo lastMailInfo = mailInfoList.get(mailInfoList.size() - 1);
-
-                    PersonalFolder personalFolder = new PersonalFolder();
-                    personalFolder.setId(mailFolder.getId());
-                    personalFolder.setLastUid(lastMailInfo.getUid());
-                    personalFolder.setLastReceivedDate(lastMailInfo.getReceivedDate());
-                    personalFolderService.updateById(personalFolder);
-
-                    List<PersonalMessage> personalMessageList = mailInfoList.stream().map(mailInfo -> {
-                        PersonalMessage personalMessage = new PersonalMessage();
-                        personalMessage.setUid(mailInfo.getUid());
-                        personalMessage.setMailboxId(mailboxInfo.getId());
-                        personalMessage.setFolderId(mailFolder.getId());
-                        personalMessage.setFolderName(mailFolder.getName());
-                        personalMessage.setSubject(mailInfo.getSubject());
-                        personalMessage.setFlags(mailInfo.getFlags());
-                        personalMessage.setFromEmail(mailInfo.getFromEmail());
-                        personalMessage.setFromPersonalName(mailInfo.getFromPersonalName());
-                        personalMessage.setFromType(mailInfo.getFromType());
-                        personalMessage.setSendDate(mailInfo.getSendDate());
-                        personalMessage.setReceivedDate(mailInfo.getReceivedDate());
-                        personalMessage.setSyncStatus(0);
-                        personalMessage.setDelFlag(0);
-                        return personalMessage;
-                    }).collect(Collectors.toList());
-                    personalMessageService.saveBatch(personalMessageList);
+        // 获取抓取的最后一封邮件
+        MailInfo lastMailInfo = mailInfoList.get(mailInfoList.size() - 1);
 
-                }
+        // 开启事务
+        TransactionStatus transactionStatus = platformTransactionManager.getTransaction(transactionDefinition);
 
+        try {
+            // 个人邮箱
+            if (mailboxInfo.getType() == 1) {
+                PersonalFolder personalFolder = new PersonalFolder();
+                personalFolder.setId(mailFolder.getId());
+                personalFolder.setLastUid(lastMailInfo.getUid());
+                personalFolder.setLastReceivedDate(lastMailInfo.getReceivedDate());
+                personalFolderService.updateById(personalFolder);
+
+                List<PersonalMessage> personalMessageList = mailInfoList.stream().map(mailInfo -> {
+                    PersonalMessage personalMessage = new PersonalMessage();
+                    personalMessage.setUid(mailInfo.getUid());
+                    personalMessage.setMailboxId(mailboxInfo.getId());
+                    personalMessage.setFolderId(mailFolder.getId());
+                    personalMessage.setFolderName(mailFolder.getName());
+                    personalMessage.setSubject(mailInfo.getSubject());
+                    personalMessage.setFlags(mailInfo.getFlags());
+                    personalMessage.setFromEmail(mailInfo.getFromEmail());
+                    personalMessage.setFromPersonalName(mailInfo.getFromPersonalName());
+                    personalMessage.setFromType(mailInfo.getFromType());
+                    personalMessage.setSendDate(mailInfo.getSendDate());
+                    personalMessage.setReceivedDate(mailInfo.getReceivedDate());
+                    personalMessage.setSyncStatus(0);
+                    personalMessage.setDelFlag(0);
+                    return personalMessage;
+                }).collect(Collectors.toList());
+                personalMessageService.saveBatch(personalMessageList);
             }
             // 企业邮箱
             else {
-
-                if (mailInfoList.size() > 0) {
-
-                    MailInfo lastMailInfo = mailInfoList.get(mailInfoList.size() - 1);
-
                     EnterpriseFolder enterpriseFolder = new EnterpriseFolder();
                     enterpriseFolder.setId(mailFolder.getId());
                     enterpriseFolder.setLastUid(lastMailInfo.getUid());
@@ -457,9 +455,6 @@ public class CoreServiceImpl implements ApplicationRunner {
                         return enterpriseMessage;
                     }).collect(Collectors.toList());
                     enterpriseMessageService.saveBatch(enterpriseMessageList);
-
-                }
-
             }
 
             // 提交事务