24282 2 anni fa
parent
commit
bdfb0e682f

+ 1 - 1
src/main/java/com/fjhx/service/IEmailMailboxService.java

@@ -1,7 +1,7 @@
 package com.fjhx.service;
 
-import com.fjhx.entity.EmailMailbox;
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.fjhx.entity.EmailMailbox;
 
 import java.util.List;
 

+ 0 - 3
src/main/java/com/fjhx/service/IEmailMessageContentService.java

@@ -1,11 +1,8 @@
 package com.fjhx.service;
 
 import com.fjhx.base.BaseService;
-import com.fjhx.entity.EmailMessageAttachment;
 import com.fjhx.entity.EmailMessageContent;
 
-import java.util.List;
-
 /**
  * <p>
  * 邮件正文 服务类

+ 1 - 1
src/main/java/com/fjhx/service/IEmailMessageSendService.java

@@ -1,7 +1,7 @@
 package com.fjhx.service;
 
-import com.fjhx.entity.EmailMessageSend;
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.fjhx.entity.EmailMessageSend;
 
 /**
  * <p>

+ 11 - 5
src/main/java/com/fjhx/service/impl/AccountServiceImpl.java

@@ -174,11 +174,13 @@ public class AccountServiceImpl implements IAccountService, ServletContextAware
                     String path = split[2];
 
                     // 下载附件
-                    RetryUtil.execute(() -> EmailEngineUtil.downloadAttachment(email, attachmentId, path));
+                    EmailEngineUtil.downloadAttachment(email, attachmentId, path);
+
                     EmailMessageAttachment attachment = new EmailMessageAttachment();
                     attachment.setAttachmentId(attachmentId);
                     attachment.setIsDownload(true);
                     emailMessageAttachmentService.updateById(attachment);
+
                     log.info("下载附件成功");
                 }
             } catch (Exception e) {
@@ -206,11 +208,12 @@ public class AccountServiceImpl implements IAccountService, ServletContextAware
             // 保存账号信息到数据库
             emailInfo = this.saveEmailInfo(bindingVo);
             // 添加账号
-            EmailEngineUtil.createAccount(bindingVo);
+            RetryUtil.execute(() -> EmailEngineUtil.createAccount(bindingVo), 3, 1000L);
+
             // redis添加同步进度
             this.progressInitialization(bindingVo);
         } catch (Exception e) {
-            EmailEngineUtil.deleteAccount(bindingVo.getEmail());
+            RetryUtil.execute(() -> EmailEngineUtil.deleteAccount(bindingVo.getEmail()), 5, 1000L);
             throw e;
         }
 
@@ -262,7 +265,7 @@ public class AccountServiceImpl implements IAccountService, ServletContextAware
         String email = bindingVo.getEmail();
         int pages = bindingVo.getPages();
 
-        MessageVo messageVo = EmailEngineUtil.getMessageList(email, bindingVo.getPath(), 0, 1);
+        MessageVo messageVo = RetryUtil.executeT(() -> EmailEngineUtil.getMessageList(email, bindingVo.getPath(), 0, 1), 5, 1000L);
 
         ProgressVo progressVo = new ProgressVo();
         progressVo.setEmail(email);
@@ -284,8 +287,9 @@ public class AccountServiceImpl implements IAccountService, ServletContextAware
             int page = pageFlag;
             try {
                 while (page < pages) {
+                    int itemPage = page;
                     // 分页获取文件夹邮件
-                    MessageVo result = EmailEngineUtil.getMessageList(email, mailbox, page, SIZE);
+                    MessageVo result = RetryUtil.executeT(() -> EmailEngineUtil.getMessageList(email, mailbox, itemPage, SIZE), 5, 5000L);
                     // 邮件信息
                     List<MessageVo.MessagesDTO> messagesDTOList = result.getMessages();
                     // 批量保存邮件信息
@@ -293,6 +297,8 @@ public class AccountServiceImpl implements IAccountService, ServletContextAware
                     // 更新同步邮件进度
                     this.synchronizationProgress(email, messagesDTOList);
                     page++;
+
+                    ThreadUtil.sleep(1000L);
                 }
             } catch (EmailEngineException e) {
                 log.error("下载失败,等待10分钟");

+ 16 - 25
src/main/java/com/fjhx/utils/EmailEngineUtil.java

@@ -6,6 +6,7 @@ import cn.hutool.http.HttpUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.fjhx.config.exception.EmailEngineException;
+import com.fjhx.config.exception.ServiceException;
 import com.fjhx.vo.*;
 import com.sun.istack.internal.NotNull;
 import lombok.extern.slf4j.Slf4j;
@@ -46,12 +47,11 @@ public class EmailEngineUtil {
                 body.read(bytes);
                 String result = new String(bytes);
 
-                HttpStatus.Series series = response.getStatusCode().series();
-                if (HttpStatus.Series.CLIENT_ERROR.equals(series)) {
-                    throw new RuntimeException(result);
+                if (HttpStatus.Series.SERVER_ERROR.equals(response.getStatusCode().series())) {
+                    throw new EmailEngineException(result);
                 }
 
-                throw new EmailEngineException(result);
+                throw new ServiceException(result);
             }
 
         });
@@ -93,7 +93,7 @@ public class EmailEngineUtil {
     }
 
     /**
-     * 查看文件夹的所有邮件
+     * 查看文件夹的邮件
      */
     public static MessageVo getMessageList(String email, String path, int page, int size) {
         String url = "/v1/account/" + email + "/messages?path=" + path + "&page=" + page + "&pageSize=" + size + "&documentStore=true";
@@ -111,21 +111,15 @@ public class EmailEngineUtil {
     /**
      * 下载附件
      */
-    public static boolean downloadAttachment(String email, String attachmentId, String fileName) {
-        try {
-            File file = new File(attachmentPath + email);
-            if (!file.exists()) {
-                boolean mkdir = file.mkdir();
-                Assert.isTrue(mkdir, "创建文件夹失败");
-                return false;
-            }
-
-            FileOutputStream fileOutputStream = new FileOutputStream(attachmentPath + fileName);
-            download("v1/account/" + email + "/attachment/" + attachmentId, fileOutputStream);
-            return true;
-        } catch (Exception e) {
-            throw new EmailEngineException("下载附件失败");
+    public static void downloadAttachment(String email, String attachmentId, String fileName) throws FileNotFoundException {
+        File file = new File(attachmentPath + email);
+        if (!file.exists()) {
+            boolean mkdir = file.mkdirs();
+            Assert.isTrue(mkdir, "创建文件夹失败");
         }
+
+        FileOutputStream fileOutputStream = new FileOutputStream(attachmentPath + fileName);
+        download("v1/account/" + email + "/attachment/" + attachmentId, fileOutputStream);
     }
 
     public static void submit(SubmitVo submitVo) {
@@ -210,18 +204,15 @@ public class EmailEngineUtil {
     }
 
     public static <T> T get(String url, Class<T> cls) {
-        return RetryUtil.execute(() -> restTemplate.getForObject(urlPrefix + url, cls), 5, 5000L);
+        return restTemplate.getForObject(urlPrefix + url, cls);
     }
 
     public static <T> T post(String url, Object paramObj, Class<T> cls) {
-        return RetryUtil.execute(() -> restTemplate.postForObject(urlPrefix + url, paramObj, cls), 5, 3000L);
+        return restTemplate.postForObject(urlPrefix + url, paramObj, cls);
     }
 
     public static void delete(String url, Map<String, Object> map) {
-        RetryUtil.execute(() -> {
-            restTemplate.delete(urlPrefix + url, map);
-            return null;
-        }, 5, 3000L);
+        restTemplate.delete(urlPrefix + url, map);
     }
 
 }

+ 23 - 14
src/main/java/com/fjhx/utils/RetryUtil.java

@@ -8,15 +8,6 @@ import lombok.extern.slf4j.Slf4j;
 public class RetryUtil {
 
     /**
-     * 重试机制
-     * 默认执行3次
-     * 每次失败等待1秒
-     */
-    public static <T> T execute(RetryRunnable<T> runnable) {
-        return execute(runnable, 0, 3, 1000L);
-    }
-
-    /**
      * 任务发生异常开启重试
      *
      * @param runnable 任务
@@ -25,11 +16,11 @@ public class RetryUtil {
      * @param <T>      返回值类型
      * @return runnable执行结果
      */
-    public static <T> T execute(RetryRunnable<T> runnable, int maxCount, Long sleep) {
-        return execute(runnable, 0, maxCount, sleep);
+    public static <T> T executeT(RetryRunnable<T> runnable, int maxCount, Long sleep) {
+        return executeT(runnable, 0, maxCount, sleep);
     }
 
-    private static <T> T execute(RetryRunnable<T> runnable, int executeCount, int maxCount, Long sleep) {
+    private static <T> T executeT(RetryRunnable<T> runnable, int executeCount, int maxCount, Long sleep) {
         try {
             return runnable.run();
         } catch (EmailEngineException e) {
@@ -38,12 +29,30 @@ public class RetryUtil {
             }
             log.warn("任务执行失败,已失败{}次,最多允许失败{}次,{}秒后开始重试", executeCount + 1, maxCount, sleep / 1000, e);
             ThreadUtil.sleep(sleep);
-            return execute(runnable, ++executeCount, maxCount, sleep);
+            return executeT(runnable, ++executeCount, maxCount, sleep);
+        }
+    }
+
+
+    public static void execute(Runnable runnable, int maxCount, Long sleep) {
+        execute(runnable, 0, maxCount, sleep);
+    }
+
+    private static void execute(Runnable runnable, int executeCount, int maxCount, Long sleep) {
+        try {
+            runnable.run();
+        } catch (EmailEngineException e) {
+            if (executeCount >= maxCount) {
+                throw e;
+            }
+            log.warn("任务执行失败,已失败{}次,最多允许失败{}次,{}秒后开始重试", executeCount + 1, maxCount, sleep / 1000, e);
+            ThreadUtil.sleep(sleep);
+            execute(runnable, ++executeCount, maxCount, sleep);
         }
     }
 
     public interface RetryRunnable<T> {
-        T run() throws RuntimeException;
+        T run();
     }
 
 }