|
@@ -4,6 +4,7 @@ import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
import cn.hutool.core.thread.ThreadUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
@@ -26,15 +27,18 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.context.ServletContextAware;
|
|
|
|
|
|
+import javax.servlet.ServletContext;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.concurrent.Executor;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
-public class AccountServiceImpl implements IAccountService {
|
|
|
+public class AccountServiceImpl implements IAccountService, ServletContextAware {
|
|
|
|
|
|
@Autowired
|
|
|
private IEmailInfoService emailInfoService;
|
|
@@ -60,11 +64,132 @@ public class AccountServiceImpl implements IAccountService {
|
|
|
@Autowired
|
|
|
private Executor syncExecutor;
|
|
|
|
|
|
+ List<String> syncEmailMessageList = Collections.synchronizedList(new ArrayList<>(100));
|
|
|
+ List<String> syncMessageAttachmentList = Collections.synchronizedList(new ArrayList<>(100));
|
|
|
+
|
|
|
/**
|
|
|
* 每页获取多少封邮件
|
|
|
*/
|
|
|
private static final int SIZE = 50;
|
|
|
|
|
|
+ @Override
|
|
|
+ public void setServletContext(ServletContext servletContext) {
|
|
|
+ syncExecutor.execute(this::syncMessageContent);
|
|
|
+ syncExecutor.execute(this::syncMessageAttachment);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步正文
|
|
|
+ */
|
|
|
+ private void syncMessageContent() {
|
|
|
+
|
|
|
+ List<String> collect = emailMessageService.list(Wrappers.<EmailMessage>lambdaQuery()
|
|
|
+ .select(EmailMessage::getEmail, EmailMessage::getMessageId)
|
|
|
+ .eq(EmailMessage::getContentSync, false)
|
|
|
+ .orderByDesc(EmailMessage::getFromDate).last("limit 50"))
|
|
|
+ .stream()
|
|
|
+ .map(item -> item.getEmail() + "," + item.getMessageId())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ for (String item : collect) {
|
|
|
+ if (!syncEmailMessageList.contains(item)) {
|
|
|
+ syncEmailMessageList.add(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ while (syncEmailMessageList.size() > 0) {
|
|
|
+ String remove = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ synchronized (this) {
|
|
|
+ log.info("下载正文");
|
|
|
+ remove = syncEmailMessageList.remove(0);
|
|
|
+ if (ObjectUtils.isEmpty(remove)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ String[] split = remove.split(",");
|
|
|
+ String email = split[0];
|
|
|
+ String messageId = split[1];
|
|
|
+
|
|
|
+ // 正文内容
|
|
|
+ MessageDetailVo messageDetail = EmailEngineUtil.getMessageDetail(email, messageId);
|
|
|
+ MessageDetailVo.TextDTO text = messageDetail.getText();
|
|
|
+ if (ObjectUtils.isNotEmpty(text)) {
|
|
|
+ EmailMessageContent emailMessageContent = new EmailMessageContent();
|
|
|
+ emailMessageContent.setMessageId(messageId);
|
|
|
+ emailMessageContent.setHtmlContent(text.getHtml());
|
|
|
+ emailMessageContentService.saveOrUpdate(emailMessageContent);
|
|
|
+
|
|
|
+ EmailMessage emailMessage = new EmailMessage();
|
|
|
+ emailMessage.setMessageId(messageId);
|
|
|
+ emailMessage.setContentSync(true);
|
|
|
+ emailMessageService.updateById(emailMessage);
|
|
|
+ }
|
|
|
+ log.info("下载正文成功");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("同步正文发生异常:{}", remove, e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ ThreadUtil.sleep(60 * 1000);
|
|
|
+ this.syncMessageContent();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void syncMessageAttachment() {
|
|
|
+ List<String> collect = emailMessageAttachmentService.list(Wrappers.<EmailMessageAttachment>lambdaQuery()
|
|
|
+ .select(EmailMessageAttachment::getAttachmentId, EmailMessageAttachment::getEmail, EmailMessageAttachment::getPath)
|
|
|
+ .eq(EmailMessageAttachment::getIsDownload, false)
|
|
|
+ .orderByDesc(EmailMessageAttachment::getCreateTime)
|
|
|
+ .last("limit 50"))
|
|
|
+ .stream()
|
|
|
+ .map(item -> item.getAttachmentId() + "," + item.getEmail() + "," + item.getPath())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ for (String item : collect) {
|
|
|
+ if (!syncMessageAttachmentList.contains(item)) {
|
|
|
+ syncMessageAttachmentList.add(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ while (syncMessageAttachmentList.size() > 0) {
|
|
|
+ String remove = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ synchronized (this) {
|
|
|
+ log.info("下载附件");
|
|
|
+ remove = syncMessageAttachmentList.remove(0);
|
|
|
+ if (ObjectUtil.isEmpty(remove)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ String[] split = remove.split(",");
|
|
|
+
|
|
|
+ String attachmentId = split[0];
|
|
|
+ String email = split[1];
|
|
|
+ String path = split[2];
|
|
|
+
|
|
|
+ // 下载附件
|
|
|
+ RetryUtil.execute(() -> EmailEngineUtil.downloadAttachment(email, attachmentId, path));
|
|
|
+ EmailMessageAttachment attachment = new EmailMessageAttachment();
|
|
|
+ attachment.setAttachmentId(attachmentId);
|
|
|
+ attachment.setIsDownload(true);
|
|
|
+ emailMessageAttachmentService.updateById(attachment);
|
|
|
+ log.info("下载附件成功");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("下载附件发生异常:{}", remove, e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ ThreadUtil.sleep(60 * 1000);
|
|
|
+ this.syncMessageAttachment();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
@Override
|
|
|
public EmailInfo binding(BindingVo bindingVo) {
|
|
@@ -282,58 +407,29 @@ public class AccountServiceImpl implements IAccountService {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 获取附件详情
|
|
|
+ // 获取邮件详情
|
|
|
MessageVo.MessagesDTO messagesDTO = listenerVo.getData().toJavaObject(MessageVo.MessagesDTO.class);
|
|
|
|
|
|
// 保存邮件
|
|
|
EmailMessage emailMessage = this.createMessage(emailInfo.getId(), emailInfo.getEmail(), messagesDTO);
|
|
|
- emailMessageService.save(emailMessage);
|
|
|
+ emailMessageService.saveOrUpdate(emailMessage);
|
|
|
|
|
|
// 保存邮箱附件
|
|
|
List<EmailMessageAttachment> emailMessageAttachmentList;
|
|
|
List<MessageVo.MessagesDTO.AttachmentsDTO> attachments = messagesDTO.getAttachments();
|
|
|
- if (attachments == null || attachments.size() == 0) {
|
|
|
- emailMessageAttachmentList = null;
|
|
|
- } else {
|
|
|
+ if (attachments != null && attachments.size() > 0) {
|
|
|
emailMessageAttachmentList = attachments.stream()
|
|
|
.map(attachment -> this.createMessageAttachment(emailMessage, attachment))
|
|
|
.collect(Collectors.toList());
|
|
|
emailMessageAttachmentService.saveOrUpdateBatch(emailMessageAttachmentList);
|
|
|
- }
|
|
|
|
|
|
- // 下载正文
|
|
|
- syncExecutor.execute(() -> {
|
|
|
- String messageId = emailMessage.getMessageId();
|
|
|
- // 正文内容
|
|
|
- MessageDetailVo messageDetail = EmailEngineUtil.getMessageDetail(account, messagesDTO.getId());
|
|
|
- MessageDetailVo.TextDTO text = messageDetail.getText();
|
|
|
- if (ObjectUtils.isNotEmpty(text)) {
|
|
|
- EmailMessageContent emailMessageContent = new EmailMessageContent();
|
|
|
- emailMessageContent.setMessageId(messageId);
|
|
|
- emailMessageContent.setHtmlContent(text.getHtml());
|
|
|
- emailMessageContentService.saveOrUpdate(emailMessageContent);
|
|
|
+ for (EmailMessageAttachment attachment : emailMessageAttachmentList) {
|
|
|
+ String value = attachment.getAttachmentId() + "," + attachment.getEmail() + "," + attachment.getPath();
|
|
|
+ syncMessageAttachmentList.add(0, value);
|
|
|
}
|
|
|
- });
|
|
|
-
|
|
|
- // 下载附件
|
|
|
- if (emailMessageAttachmentList != null) {
|
|
|
- syncExecutor.execute(() -> {
|
|
|
- for (EmailMessageAttachment attachment : emailMessageAttachmentList) {
|
|
|
- String attachmentId = attachment.getAttachmentId();
|
|
|
- String path = attachment.getPath();
|
|
|
-
|
|
|
- // 下载附件
|
|
|
- try {
|
|
|
- RetryUtil.execute(() -> EmailEngineUtil.downloadAttachment(account, attachmentId, path));
|
|
|
- attachment.setIsDownload(true);
|
|
|
- emailMessageAttachmentService.updateById(attachment);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("下载附件失败:email:{},attachmentId:{}", account, attachmentId, e);
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
}
|
|
|
|
|
|
+ syncEmailMessageList.add(0, emailMessage.getEmail() + "," + emailMessage.getMessageId());
|
|
|
}
|
|
|
|
|
|
/**
|