自动处理模块新增加逻辑,邮箱邮件处理前调用检查方法如果邮件失败处理多次则将邮件从邮箱中移除不影响后续邮件操作

This commit is contained in:
qiaoqinzheng 2024-04-01 10:02:38 +08:00
parent aa4c2b93d8
commit d5f40c9023
4 changed files with 39 additions and 21 deletions

View File

@ -7,6 +7,7 @@ import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.sun.mail.imap.IMAPStore;
import com.sun.mail.smtp.SMTPAddressFailedException;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.dto.message.MessageDTO;
@ -16,6 +17,7 @@ import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.email.emuns.MailContentType;
import org.jeecg.common.exception.DownloadEmailException;
import org.jeecg.common.properties.SpectrumPathProperties;
import org.jeecg.common.properties.TaskProperties;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.Md5Util;
import org.jeecg.common.util.RedisUtil;
@ -47,6 +49,8 @@ public class EmailServiceManager {
private SysEmail email;
private SpectrumPathProperties spectrumPathProperties;
private TaskProperties taskProperties;
/**
* 系统启动时间
*/
@ -80,13 +84,14 @@ public class EmailServiceManager {
* @param email 邮件属性
*/
public void init(SysEmail email, Integer receiveNum, String temporaryStoragePath,
Date systemStartupTime, SpectrumPathProperties pathProperties,
Date systemStartupTime, SpectrumPathProperties pathProperties,TaskProperties taskProperties,
RedisUtil redisUtil){
this.email = email;
this.receiveNum = receiveNum;
this.temporaryStoragePath = temporaryStoragePath;
this.systemStartupTime = systemStartupTime;
this.spectrumPathProperties = pathProperties;
this.taskProperties = taskProperties;
this.redisUtil = redisUtil;
}
@ -630,10 +635,10 @@ public class EmailServiceManager {
if(null != store){
store.close();
}
for(String messageId : messageIds){
String key = RedisConstant.EMAIL_MSG_ID+StringConstant.COLON+messageId;
redisUtil.del(key);
}
// for(String messageId : messageIds){
// String key = RedisConstant.EMAIL_MSG_ID+StringConstant.COLON+messageId;
// redisUtil.del(key);
// }
} catch (MessagingException e) {
log.error("Email closure failed, email address is: {}, reason is: {}",email.getUsername(),e.getMessage());
e.printStackTrace();
@ -649,10 +654,13 @@ public class EmailServiceManager {
boolean exist = false;
try {
String key = RedisConstant.EMAIL_MSG_ID+StringConstant.COLON+messageId;
exist = redisUtil.hasKey(key);
if(exist){
int numberKey = redisUtil.get(key) != null? (int) redisUtil.get(key):0;
// exist = redisUtil.hasKey(key);
if(numberKey > taskProperties.getForceDeletedNumber()){
exist = true;
log.info("Check: Remove Email:{},receiveTime:{}",message.getSubject(), DateUtils.formatDate(message.getReceivedDate(),"yyyy-MM-dd HH:mm:ss"));
message.setFlag(Flags.Flag.DELETED,true);
redisUtil.del(key);
}
return exist;
} catch (MessagingException e) {

View File

@ -41,6 +41,11 @@ public class TaskProperties implements Serializable {
*/
private Integer mailThreadExecCycle;
/**
* 线程获取失败邮件次数
*/
private Integer forceDeletedNumber;
/**
* 监测需删除的邮件线程执行周期毫秒
*/

View File

@ -74,23 +74,23 @@ public class EmailParsingActuator extends Thread{
long start = System.currentTimeMillis();
final EmailServiceManager emailServiceManager = EmailServiceManager.getInstance();
emailServiceManager.init(this.emailProperties,this.taskProperties.getReceiveNum(),this.taskProperties.getTemporaryStoragePath(),
this.systemStartupTime, spectrumServiceQuotes.getSpectrumPathProperties(),spectrumServiceQuotes.getRedisUtil());
this.systemStartupTime, spectrumServiceQuotes.getSpectrumPathProperties(), spectrumServiceQuotes.getTaskProperties(), spectrumServiceQuotes.getRedisUtil());
List<String> messageIds = new ArrayList<>();
try {
Message[] messages = emailServiceManager.receiveMail();
if(ArrayUtils.isNotEmpty(messages)){
log.info("EmailParsingActuator本次获取邮件数量为{}",messages.length);
//检验获取的邮件是否在之前删除失败列表中若在直接调用邮件API删除并且此次数组里元素也删除
// for(int i=messages.length-1;i>=0;i--){
// if (!messages[i].isExpunged()){
// String messageId = ((MimeMessage) messages[i]).getMessageID();
// final boolean exist = emailServiceManager.check(messages[i],messageId);
// messageIds.add(messageId);
// if(exist){
// messages = ArrayUtils.remove(messages,i);
// }
// }
// }
for(int i=messages.length-1;i>=0;i--){
if (!messages[i].isExpunged()){
String messageId = ((MimeMessage) messages[i]).getMessageID();
final boolean exist = emailServiceManager.check(messages[i],messageId);
messageIds.add(messageId);
if(exist){
messages = ArrayUtils.remove(messages,i);
}
}
}
log.info("EmailParsingActuator本次真实执行邮件数量为{}",messages.length);
if(messages.length > 0){
//本批次邮件号
@ -107,6 +107,8 @@ public class EmailParsingActuator extends Thread{
}
}catch (InterruptedException e) {
e.printStackTrace();
} catch (MessagingException e) {
throw new RuntimeException(e);
} finally {
//清除本批次邮件日志缓存
EmailLogManager.getInstance().clear();

View File

@ -96,7 +96,10 @@ public class SpectrumParsingActuator implements Runnable{
receiveDate = DateUtils.formatDate(message.getReceivedDate(),"yyyy-MM-dd HH:mm:ss");
String emlName = subject+ StringConstant.UNDER_LINE+ receiveDate;
String key = RedisConstant.EMAIL_MSG_ID+StringConstant.COLON+messageId;
spectrumServiceQuotes.getRedisUtil().set(key,emlName,expiryTime);
// spectrumServiceQuotes.getRedisUtil().set(key,emlName,expiryTime);
//判断当前key的下载次数是否超过限制次数
spectrumServiceQuotes.getRedisUtil().incr(key, 1L);
spectrumServiceQuotes.getRedisUtil().expire(key, expiryTime);
//线程开始初始化时初始本线程负责的能谱日志事件
SpectrumLogManager.mailSpectrumLogManager.offer(Thread.currentThread().getId(),null);
@ -123,6 +126,7 @@ public class SpectrumParsingActuator implements Runnable{
try {
//开始解析
spectrumHandler.handler();
spectrumServiceQuotes.getRedisUtil().del(key);
} catch (Exception e) {
//如果是gamma谱的分析异常
if (e instanceof AnalySpectrumException) {
@ -136,7 +140,6 @@ public class SpectrumParsingActuator implements Runnable{
throw e;
}
}
}else{
log.warn("This email {} parsing failed and is not listed in the Met, Alert, SOH, Sample, Detbkphd, QC, Gasbkphd spectra.",subject);
}
@ -204,7 +207,7 @@ public class SpectrumParsingActuator implements Runnable{
final String finalPath = rootPath+emlErrorPath;
FileOperation.moveFile(emlFile,finalPath,true);
// 删除 key防止下次线程执行删除邮件
spectrumServiceQuotes.getRedisUtil().del(key);
// spectrumServiceQuotes.getRedisUtil().del(key);
}
}