|
@@ -16,14 +16,14 @@ import com.fjhx.utils.EmailUtil;
|
|
|
import com.sun.mail.imap.IMAPMessage;
|
|
|
import com.sun.mail.imap.IMAPStore;
|
|
|
import com.sun.mail.util.MailConnectException;
|
|
|
-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.boot.ApplicationArguments;
|
|
|
+import org.springframework.boot.ApplicationRunner;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
import javax.mail.*;
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
import java.util.*;
|
|
@@ -33,7 +33,7 @@ import java.util.concurrent.Executor;
|
|
|
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
-public class CoreServiceImpl {
|
|
|
+public class CoreServiceImpl implements ApplicationRunner {
|
|
|
|
|
|
@Autowired
|
|
|
private MailboxService mailboxService;
|
|
@@ -59,27 +59,8 @@ public class CoreServiceImpl {
|
|
|
// 缓存错误次数
|
|
|
private static final Map<String, Integer> map = new ConcurrentHashMap<>();
|
|
|
|
|
|
- @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();
|
|
|
- // log.info("第 {} 伦同步还剩余 {} 封邮箱需要同步", num, countDownLatch.getCount());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @PostConstruct
|
|
|
- public void init() {
|
|
|
+ @Override
|
|
|
+ public void run(ApplicationArguments args) {
|
|
|
// 发起邮件同步
|
|
|
new Thread(this::monitorMail).start();
|
|
|
}
|
|
@@ -121,11 +102,16 @@ public class CoreServiceImpl {
|
|
|
// 开启抓取邮件任务
|
|
|
for (Mailbox mailbox : list) {
|
|
|
|
|
|
- // 创建异步任务
|
|
|
- Task task = new Task(countDownLatch, mailbox);
|
|
|
-
|
|
|
// 开启异步操作
|
|
|
- emailTaskExecutor.execute(task);
|
|
|
+ emailTaskExecutor.execute(() -> {
|
|
|
+ try {
|
|
|
+ synchronousMail(mailbox);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("发生未知异常", e);
|
|
|
+ }
|
|
|
+ countDownLatch.countDown();
|
|
|
+ // log.info("第 {} 伦同步还剩余 {} 封邮箱需要同步", num, countDownLatch.getCount());
|
|
|
+ });
|
|
|
|
|
|
}
|
|
|
|