|
@@ -14,14 +14,12 @@ import com.fjhx.service.MailboxService;
|
|
|
import com.fjhx.utils.EmailUtil;
|
|
|
import com.sun.mail.imap.IMAPMessage;
|
|
|
import com.sun.mail.imap.IMAPStore;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springblade.core.tool.utils.ThreadUtil;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.PlatformTransactionManager;
|
|
|
-import org.springframework.transaction.TransactionDefinition;
|
|
|
-import org.springframework.transaction.TransactionStatus;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import javax.mail.Folder;
|
|
@@ -32,12 +30,12 @@ import javax.mail.internet.InternetAddress;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
-// @AllArgsConstructor
|
|
|
public class CoreServiceImpl {
|
|
|
|
|
|
@Autowired
|
|
@@ -46,6 +44,7 @@ public class CoreServiceImpl {
|
|
|
private MailFolderService mailFolderService;
|
|
|
@Autowired
|
|
|
private MailInfoService mailInfoService;
|
|
|
+
|
|
|
// private final MailAddressService mailAddressService;
|
|
|
// private final MailMessageService mailMessageService;
|
|
|
// private final MailAttachmentService mailAttachmentService;
|
|
@@ -54,11 +53,11 @@ public class CoreServiceImpl {
|
|
|
@Qualifier(TaskPoolConfig.emailTaskExecutor)
|
|
|
private Executor emailTaskExecutor;
|
|
|
|
|
|
- @Autowired
|
|
|
- private PlatformTransactionManager platformTransactionManager;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private TransactionDefinition transactionDefinition;
|
|
|
+ // @Autowired
|
|
|
+ // private PlatformTransactionManager platformTransactionManager;
|
|
|
+ //
|
|
|
+ // @Autowired
|
|
|
+ // private TransactionDefinition transactionDefinition;
|
|
|
|
|
|
|
|
|
// 累计同步次数
|
|
@@ -70,6 +69,25 @@ public class CoreServiceImpl {
|
|
|
// 初始化同步邮件天数
|
|
|
private static final int initDay = 7;
|
|
|
|
|
|
+ @AllArgsConstructor
|
|
|
+ class Task implements Runnable {
|
|
|
+
|
|
|
+ private CountDownLatch countDownLatch;
|
|
|
+ private Mailbox mailbox;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ synchronousMail(mailbox);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("这里出错了", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ countDownLatch.countDown();
|
|
|
+ System.out.println("countDownLatch.getCount() = " + countDownLatch.getCount());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@PostConstruct
|
|
|
public void monitorMail() {
|
|
|
|
|
@@ -92,22 +110,20 @@ public class CoreServiceImpl {
|
|
|
|
|
|
// 开启抓取邮件任务
|
|
|
for (Mailbox mailbox : list) {
|
|
|
- // 开启异步操作
|
|
|
- emailTaskExecutor.execute(() -> {
|
|
|
- try {
|
|
|
- synchronousMail(mailbox);
|
|
|
- } finally {
|
|
|
- countDownLatch.countDown();
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
+ // System.out.println("循环了" + i++ + "次");
|
|
|
+ // Task task = new Task(countDownLatch, mailbox);
|
|
|
+ // // 开启异步操作
|
|
|
+ // emailTaskExecutor.execute(task);
|
|
|
|
|
|
- try {
|
|
|
- countDownLatch.await();
|
|
|
- } catch (InterruptedException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ synchronousMail(mailbox);
|
|
|
}
|
|
|
|
|
|
+ // try {
|
|
|
+ // countDownLatch.await();
|
|
|
+ // } catch (InterruptedException e) {
|
|
|
+ // throw new RuntimeException(e);
|
|
|
+ // }
|
|
|
+
|
|
|
// 结束处理时间
|
|
|
long end = System.currentTimeMillis();
|
|
|
|
|
@@ -133,7 +149,8 @@ public class CoreServiceImpl {
|
|
|
private void synchronousMail(Mailbox mailbox) {
|
|
|
|
|
|
// 获取所有文件夹
|
|
|
- List<MailFolder> mailFolderList = mailFolderService.list(Wrappers.<MailFolder>lambdaQuery().eq(MailFolder::getMailboxId, mailbox));
|
|
|
+ List<MailFolder> mailFolderList = mailFolderService.list(
|
|
|
+ Wrappers.<MailFolder>lambdaQuery().eq(MailFolder::getMailboxId, mailbox.getId()));
|
|
|
|
|
|
IMAPStore store = null;
|
|
|
|
|
@@ -143,7 +160,11 @@ public class CoreServiceImpl {
|
|
|
// 文件夹为空,代表初始化,从官方邮箱重新拉取文件夹
|
|
|
if (mailFolderList.size() == 0) {
|
|
|
mailFolderList = EmailUtil.createMailFolderList(store, mailbox.getId());
|
|
|
- mailFolderService.saveBatch(mailFolderList);
|
|
|
+ try {
|
|
|
+ mailFolderService.saveBatch(mailFolderList);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 分别拉取每个文件夹邮件
|
|
@@ -160,26 +181,35 @@ public class CoreServiceImpl {
|
|
|
}
|
|
|
try {
|
|
|
Message[] messages = folder.getMessages();
|
|
|
- List<MailInfo> mailInfoList = getMailInfoList(mailFolder, messages);
|
|
|
+ if (messages.length == 0) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
+ IMAPMessage message = (IMAPMessage) messages[messages.length - 1];
|
|
|
+ if (Objects.equals(message.getMessageID(), mailFolder.getLastMessageId())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- IMAPMessage message = (IMAPMessage) messages[0];
|
|
|
+ List<MailInfo> mailInfoList = getMailInfoList(mailFolder, messages);
|
|
|
mailFolder.setLastMessageId(message.getMessageID());
|
|
|
mailFolder.setLastReceivedDate(message.getReceivedDate());
|
|
|
-
|
|
|
saveMailInfo(mailFolder, mailInfoList);
|
|
|
|
|
|
} finally {
|
|
|
- folder.close();
|
|
|
+ if (folder.isOpen()) {
|
|
|
+ folder.close();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
- log.error("链接imap出错,邮箱信息:{}", JSON.toJSONString(mailbox));
|
|
|
+ log.error("链接imap出错,邮箱信息:{}", JSON.toJSONString(mailbox), e);
|
|
|
} finally {
|
|
|
if (store != null) {
|
|
|
try {
|
|
|
- store.close();
|
|
|
+ if (store.isConnected()) {
|
|
|
+ store.close();
|
|
|
+ }
|
|
|
} catch (MessagingException e) {
|
|
|
log.error("store关闭失败", e);
|
|
|
}
|
|
@@ -207,7 +237,7 @@ public class CoreServiceImpl {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- if (lastReceivedDate.before(receivedDate)) {
|
|
|
+ if (receivedDate.before(lastReceivedDate)) {
|
|
|
break;
|
|
|
}
|
|
|
|
|
@@ -221,9 +251,11 @@ public class CoreServiceImpl {
|
|
|
mailInfo.setFolderName(mailFolder.getName());
|
|
|
|
|
|
InternetAddress internetAddress = (InternetAddress) message.getSender();
|
|
|
- mailInfo.setFromEmail(internetAddress.getAddress());
|
|
|
- mailInfo.setFromPersonalName(internetAddress.getPersonal());
|
|
|
- mailInfo.setFromType(internetAddress.getType());
|
|
|
+ if (internetAddress != null) {
|
|
|
+ mailInfo.setFromEmail(internetAddress.getAddress());
|
|
|
+ mailInfo.setFromPersonalName(internetAddress.getPersonal());
|
|
|
+ mailInfo.setFromType(internetAddress.getType());
|
|
|
+ }
|
|
|
mailInfo.setSendDate(receivedDate);
|
|
|
mailInfo.setCreateTime(new Date());
|
|
|
mailInfoList.add(0, mailInfo);
|
|
@@ -238,19 +270,19 @@ public class CoreServiceImpl {
|
|
|
* 保存邮件数据
|
|
|
*/
|
|
|
private void saveMailInfo(MailFolder mailFolder, List<MailInfo> mailInfoList) {
|
|
|
- TransactionStatus transactionStatus = platformTransactionManager.getTransaction(transactionDefinition);
|
|
|
- try {
|
|
|
-
|
|
|
- mailFolderService.updateById(mailFolder);
|
|
|
- mailInfoService.saveBatch(mailInfoList);
|
|
|
-
|
|
|
- // 事务提交
|
|
|
- platformTransactionManager.commit(transactionStatus);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("同步邮箱发生错误", e);
|
|
|
- // 事务回滚
|
|
|
- platformTransactionManager.rollback(transactionStatus);
|
|
|
- }
|
|
|
+ // TransactionStatus transactionStatus = platformTransactionManager.getTransaction(transactionDefinition);
|
|
|
+ // try {
|
|
|
+
|
|
|
+ mailFolderService.updateById(mailFolder);
|
|
|
+ mailInfoService.saveBatch(mailInfoList);
|
|
|
+
|
|
|
+ // 事务提交
|
|
|
+ // platformTransactionManager.commit(transactionStatus);
|
|
|
+ // } catch (Exception e) {
|
|
|
+ // log.error("同步邮箱发生错误", e);
|
|
|
+ // // 事务回滚
|
|
|
+ // platformTransactionManager.rollback(transactionStatus);
|
|
|
+ // }
|
|
|
}
|
|
|
|
|
|
// 添加同步邮件数量
|