修改解析邮件时获取邮件接收日期或者是发件日期

This commit is contained in:
duwenyuan 2025-06-30 10:51:11 +08:00
parent 0c5127a3ab
commit a25e7e91ac

View File

@ -24,6 +24,7 @@ import javax.mail.internet.MimeMessage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.Date;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
@ -31,7 +32,7 @@ import java.util.concurrent.CountDownLatch;
* 能谱解析 * 能谱解析
*/ */
@Slf4j @Slf4j
public class SpectrumParsingActuator implements Runnable{ public class SpectrumParsingActuator implements Runnable {
/** /**
* IMS2.0格式邮件判断条件 * IMS2.0格式邮件判断条件
*/ */
@ -70,9 +71,9 @@ public class SpectrumParsingActuator implements Runnable{
*/ */
private final int expiryTime = 86400; private final int expiryTime = 86400;
public void init(Message message, EmailProperties emailProperties,EmailServiceManager emailServiceManager, public void init(Message message, EmailProperties emailProperties, EmailServiceManager emailServiceManager,
CountDownLatch taskLatch, SpectrumServiceQuotes spectrumServiceQuotes, CountDownLatch taskLatch, SpectrumServiceQuotes spectrumServiceQuotes,
EmailCounter emailCounter,Integer batchesCounter){ EmailCounter emailCounter, Integer batchesCounter) {
this.message = message; this.message = message;
this.emailProperties = emailProperties; this.emailProperties = emailProperties;
this.emailServiceManager = emailServiceManager; this.emailServiceManager = emailServiceManager;
@ -89,37 +90,39 @@ public class SpectrumParsingActuator implements Runnable{
String receiveDate = null; String receiveDate = null;
try { try {
//获取邮件主题 //获取邮件主题
subject = emailServiceManager.getMailSubject(message,this.batchesCounter); subject = emailServiceManager.getMailSubject(message, this.batchesCounter);
//解析之前先把邮件唯一信息存储到redis //解析之前先把邮件唯一信息存储到redis
String messageId = ((MimeMessage) message).getMessageID(); String msgID = ((MimeMessage) message).getMessageID();
receiveDate = DateUtils.formatDate(message.getReceivedDate(),"yyyy-MM-dd HH:mm:ss"); String messageId = msgID == null ? emailServiceManager.getMessagesID(message,this.batchesCounter) : msgID;
String emlName = subject+ StringConstant.UNDER_LINE+ receiveDate; Date dateUtils = message.getReceivedDate() == null ? message.getSentDate() : message.getReceivedDate();
String key = RedisConstant.EMAIL_MSG_ID+StringConstant.COLON+messageId; receiveDate = DateUtils.formatDate(dateUtils, "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);
//线程开始初始化时初始本线程负责的能谱日志事件 //线程开始初始化时初始本线程负责的能谱日志事件
SpectrumLogManager.mailSpectrumLogManager.offer(Thread.currentThread().getId(),null); SpectrumLogManager.mailSpectrumLogManager.offer(Thread.currentThread().getId(), null);
//所有邮件都需以.eml格式存储到eml文件夹中 //所有邮件都需以.eml格式存储到eml文件夹中
final File emlFile = emailServiceManager.downloadEmailToEmlDir(message, emailCounter.getCurrValue(),this.batchesCounter); final File emlFile = emailServiceManager.downloadEmailToEmlDir(message, emailCounter.getCurrValue(), this.batchesCounter);
downloadFlag = true; downloadFlag = true;
//保存邮件日志到PG数据库 //保存邮件日志到PG数据库
this.spectrumServiceQuotes.getMailLogService().create(message,emailProperties); this.spectrumServiceQuotes.getMailLogService().create(message, emailProperties);
//获取邮件内容 //获取邮件内容
StringBuilder mailContent = new StringBuilder(); StringBuilder mailContent = new StringBuilder();
if(Objects.nonNull(emlFile) && emlFile.length() > 0){ if (Objects.nonNull(emlFile) && emlFile.length() > 0) {
mailContent.append(FileUtil.readUtf8String(emlFile)); mailContent.append(FileUtil.readUtf8String(emlFile));
} }
//判断是否是IMS2.0协议文件 //判断是否是IMS2.0协议文件
// 如果邮件内容校验成功 将文件保存到eml目录 并删除邮件对象 // 如果邮件内容校验成功 将文件保存到eml目录 并删除邮件对象
if(checkMailContent(mailContent,subject)){ if (checkMailContent(mailContent, subject)) {
AbstractSpectrumHandler spectrumHandler = new SamplephdSpectrum(); AbstractSpectrumHandler spectrumHandler = new SamplephdSpectrum();
spectrumHandler.init(mailContent.toString(),emlFile.getName(),spectrumServiceQuotes,new StringBuilder(),SpectrumSource.FORM_EMAIL_SERVICE.getSourceType(),batchesCounter); spectrumHandler.init(mailContent.toString(), emlFile.getName(), spectrumServiceQuotes, new StringBuilder(), SpectrumSource.FORM_EMAIL_SERVICE.getSourceType(), batchesCounter);
final boolean matchResult = spectrumHandler.saveEmailToLocal(); final boolean matchResult = spectrumHandler.saveEmailToLocal();
if(matchResult){ if (matchResult) {
try { try {
//开始解析 //开始解析
spectrumHandler.handler(); spectrumHandler.handler();
@ -128,25 +131,25 @@ public class SpectrumParsingActuator implements Runnable{
//如果是gamma谱的分析异常 //如果是gamma谱的分析异常
if (e instanceof AnalySpectrumException) { if (e instanceof AnalySpectrumException) {
// 如果邮件内容校验失败(邮件内容不完整) 将错误邮件从eml移动到emlError // 如果邮件内容校验失败(邮件内容不完整) 将错误邮件从eml移动到emlError
if (Objects.nonNull(emlFile) && emlFile.exists()){ if (Objects.nonNull(emlFile) && emlFile.exists()) {
moveEmail(emlFile, key); moveEmail(emlFile, key);
} }
//删除邮件 //删除邮件
emailServiceManager.removeMail(message,batchesCounter); emailServiceManager.removeMail(message, batchesCounter);
} else { } else {
throw e; throw e;
} }
} }
}else{ } else {
log.warn("This email {} parsing failed and is not listed in the Met, Alert, SOH, Sample, Detbkphd, QC, Gasbkphd spectra.",subject); log.warn("This email {} parsing failed and is not listed in the Met, Alert, SOH, Sample, Detbkphd, QC, Gasbkphd spectra.", subject);
} }
emailServiceManager.removeMail(message,batchesCounter); emailServiceManager.removeMail(message, batchesCounter);
} else { } else {
//判断当前key的下载次数是否超过限制次数 //判断当前key的下载次数是否超过限制次数
spectrumServiceQuotes.getRedisUtil().incr(key, 1L); spectrumServiceQuotes.getRedisUtil().incr(key, 1L);
spectrumServiceQuotes.getRedisUtil().expire(key, expiryTime); spectrumServiceQuotes.getRedisUtil().expire(key, expiryTime);
// 如果邮件内容校验失败(邮件内容不完整) 将错误邮件从eml移动到emlError // 如果邮件内容校验失败(邮件内容不完整) 将错误邮件从eml移动到emlError
if (Objects.nonNull(emlFile) && emlFile.exists()){ if (Objects.nonNull(emlFile) && emlFile.exists()) {
moveEmail(emlFile, key); moveEmail(emlFile, key);
throw new DownloadEmailException("邮件移走后手动抛出DownloadEmailException"); throw new DownloadEmailException("邮件移走后手动抛出DownloadEmailException");
} }
@ -156,18 +159,18 @@ public class SpectrumParsingActuator implements Runnable{
log.error("邮件处理异常{},邮件主题:{}", e, subject); log.error("邮件处理异常{},邮件主题:{}", e, subject);
// todo 需要解决其他异常会进入if 删除邮件 // todo 需要解决其他异常会进入if 删除邮件
// 如果不是下载导致的失败 并且 下载成功则删除下载的邮件对象 // 如果不是下载导致的失败 并且 下载成功则删除下载的邮件对象
if(!(e instanceof DownloadEmailException) && downloadFlag){ if (!(e instanceof DownloadEmailException) && downloadFlag) {
log.error("Catch Remove Email"+ subject + StringPool.UNDERSCORE + receiveDate + StringPool.UNDERSCORE); log.error("Catch Remove Email" + subject + StringPool.UNDERSCORE + receiveDate + StringPool.UNDERSCORE);
emailServiceManager.removeMail(message,batchesCounter); emailServiceManager.removeMail(message, batchesCounter);
} }
}finally { } finally {
try { try {
EmailLogEvent expungeEvent = new EmailLogEvent(this.batchesCounter,Thread.currentThread().getId(),EmailLogManager.GS_TYPE_GET,EmailLogManager.DONE); EmailLogEvent expungeEvent = new EmailLogEvent(this.batchesCounter, Thread.currentThread().getId(), EmailLogManager.GS_TYPE_GET, EmailLogManager.DONE);
EmailLogManager.getInstance().offer(Thread.currentThread().getId(),expungeEvent); EmailLogManager.getInstance().offer(Thread.currentThread().getId(), expungeEvent);
EmailLogManager.getInstance().writeLog(Thread.currentThread().getId()); EmailLogManager.getInstance().writeLog(Thread.currentThread().getId());
}finally { } finally {
this.taskLatch.countDown(); this.taskLatch.countDown();
} }
} }
@ -184,30 +187,31 @@ public class SpectrumParsingActuator implements Runnable{
* 这些类型的邮件在头部中会指定相应的内容类型和协议信息 * 这些类型的邮件在头部中会指定相应的内容类型和协议信息
* 3.简单纯文本邮件另一方面简单的纯文本邮件没有特殊的附件或内容类型要求因此可能不需要使用 MIME 格式 * 3.简单纯文本邮件另一方面简单的纯文本邮件没有特殊的附件或内容类型要求因此可能不需要使用 MIME 格式
* 这种情况下邮件文本中可能不包含 Mime-Version: 1.0 Content-Type: multipart/signed; * 这种情况下邮件文本中可能不包含 Mime-Version: 1.0 Content-Type: multipart/signed;
*
* @param mailContent * @param mailContent
* @return * @return
*/ */
private boolean checkMailContent(StringBuilder mailContent,String subject){ private boolean checkMailContent(StringBuilder mailContent, String subject) {
if(StringUtils.isNotBlank(mailContent) && mailContent.indexOf(EMAIL_BEGIN) != -1 && if (StringUtils.isNotBlank(mailContent) && mailContent.indexOf(EMAIL_BEGIN) != -1 &&
mailContent.indexOf(MSG_TYPE) != -1 && mailContent.indexOf(EMAIL_STOP) != -1){ mailContent.indexOf(MSG_TYPE) != -1 && mailContent.indexOf(EMAIL_STOP) != -1) {
if(!StringUtils.startsWith(mailContent,EMAIL_BEGIN)){ if (!StringUtils.startsWith(mailContent, EMAIL_BEGIN)) {
mailContent.delete(0,mailContent.indexOf(EMAIL_BEGIN)); mailContent.delete(0, mailContent.indexOf(EMAIL_BEGIN));
} }
if(!StringUtils.endsWith(mailContent,EMAIL_STOP)){ if (!StringUtils.endsWith(mailContent, EMAIL_STOP)) {
mailContent.delete(mailContent.indexOf(EMAIL_STOP)+EMAIL_STOP.length(),mailContent.length()); mailContent.delete(mailContent.indexOf(EMAIL_STOP) + EMAIL_STOP.length(), mailContent.length());
} }
log.info("{}邮件校验成功符合IMS2.0格式",subject); log.info("{}邮件校验成功符合IMS2.0格式", subject);
return true; return true;
} }
log.warn("{}邮件校验成功此邮件不符合IMS2.0格式",subject); log.warn("{}邮件校验成功此邮件不符合IMS2.0格式", subject);
return false; return false;
} }
private void moveEmail(File emlFile, String key) throws IOException { private void moveEmail(File emlFile, String key) throws IOException {
final String rootPath = spectrumServiceQuotes.getSpectrumPathProperties().getRootPath(); final String rootPath = spectrumServiceQuotes.getSpectrumPathProperties().getRootPath();
final String emlErrorPath = spectrumServiceQuotes.getSpectrumPathProperties().getEmlErrorPath(); final String emlErrorPath = spectrumServiceQuotes.getSpectrumPathProperties().getEmlErrorPath();
final String finalPath = rootPath+emlErrorPath; final String finalPath = rootPath + emlErrorPath;
FileOperation.moveFile(emlFile,finalPath,true); FileOperation.moveFile(emlFile, finalPath, true);
// 删除 key防止下次线程执行删除邮件 // 删除 key防止下次线程执行删除邮件
// spectrumServiceQuotes.getRedisUtil().del(key); // spectrumServiceQuotes.getRedisUtil().del(key);
} }