修改获取邮件发送日期为空时、获取邮件接受时间
This commit is contained in:
parent
0fd138b175
commit
a0cff20da8
|
@ -2,14 +2,9 @@ package org.jeecg.modules.spectrum;
|
||||||
|
|
||||||
import cn.hutool.core.date.DatePattern;
|
import cn.hutool.core.date.DatePattern;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.io.FileUtil;
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.io.IOUtils;
|
|
||||||
import org.apache.commons.io.LineIterator;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.jeecg.common.constant.RedisConstant;
|
import org.jeecg.common.constant.RedisConstant;
|
||||||
import org.jeecg.common.constant.StringConstant;
|
import org.jeecg.common.constant.StringConstant;
|
||||||
import org.jeecg.common.email.EmailLogEvent;
|
import org.jeecg.common.email.EmailLogEvent;
|
||||||
|
@ -17,30 +12,31 @@ import org.jeecg.common.email.EmailLogManager;
|
||||||
import org.jeecg.common.email.EmailServiceManager;
|
import org.jeecg.common.email.EmailServiceManager;
|
||||||
import org.jeecg.common.exception.DownloadEmailException;
|
import org.jeecg.common.exception.DownloadEmailException;
|
||||||
import org.jeecg.common.util.DateUtils;
|
import org.jeecg.common.util.DateUtils;
|
||||||
import org.jeecg.common.util.Md5Util;
|
|
||||||
import org.jeecg.modules.common.SpectrumUtils;
|
import org.jeecg.modules.common.SpectrumUtils;
|
||||||
import org.jeecg.modules.email.EmailProperties;
|
import org.jeecg.modules.email.EmailProperties;
|
||||||
import org.jeecg.modules.enums.SpectrumSource;
|
import org.jeecg.modules.enums.SpectrumSource;
|
||||||
import org.jeecg.modules.exception.AnalySpectrumException;
|
import org.jeecg.modules.exception.AnalySpectrumException;
|
||||||
import org.jeecg.modules.file.FileOperation;
|
import org.jeecg.modules.file.FileOperation;
|
||||||
import org.springframework.util.Base64Utils;
|
|
||||||
|
|
||||||
import javax.mail.Message;
|
import javax.mail.Message;
|
||||||
import javax.mail.MessagingException;
|
import javax.mail.MessagingException;
|
||||||
import javax.mail.Session;
|
|
||||||
import javax.mail.internet.MimeMessage;
|
import javax.mail.internet.MimeMessage;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.file.Files;
|
import java.util.Date;
|
||||||
import java.util.Base64;
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Properties;
|
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 能谱解析
|
* 能谱解析
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class SpectrumParsingActuator implements Runnable{
|
public class SpectrumParsingActuator implements Runnable {
|
||||||
|
/**
|
||||||
|
* IMS2.0格式邮件判断条件
|
||||||
|
*/
|
||||||
|
private final static String EMAIL_BEGIN = "BEGIN IMS2.0";
|
||||||
|
private final static String MSG_TYPE = "MSG_TYPE DATA";
|
||||||
|
private final static String EMAIL_STOP = "STOP";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 邮件对象
|
* 邮件对象
|
||||||
|
@ -73,9 +69,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;
|
||||||
|
@ -92,39 +88,36 @@ 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();
|
||||||
if (StrUtil.isEmpty(messageId)) {
|
String messageId = msgID == null ? emailServiceManager.getMessagesID(message, this.batchesCounter) : msgID;
|
||||||
String subjectStr = message.getSubject().replace(" ", "_");
|
Date dateUtils = message.getReceivedDate() == null ? message.getSentDate() : message.getReceivedDate();
|
||||||
String receivedStr = DateUtil.format(message.getReceivedDate(), DatePattern.NORM_DATETIME_MINUTE_PATTERN);
|
receiveDate = DateUtils.formatDate(dateUtils, "yyyy-MM-dd HH:mm:ss");
|
||||||
messageId = subjectStr + StringConstant.UNDER_LINE + receivedStr;
|
String emlName = subject + StringConstant.UNDER_LINE + receiveDate;
|
||||||
}
|
String key = RedisConstant.EMAIL_MSG_ID + StringConstant.COLON + messageId;
|
||||||
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);
|
||||||
//线程开始初始化时,初始本线程负责的能谱日志事件
|
//线程开始初始化时,初始本线程负责的能谱日志事件
|
||||||
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();
|
||||||
|
|
||||||
//校验邮件是否是IMS2.0协议文件,如果是设置邮件内容到mailContent变量
|
//校验邮件是否是IMS2.0协议文件,如果是设置邮件内容到mailContent变量
|
||||||
// 如果邮件内容校验成功 将文件保存到eml目录 并删除邮件对象
|
// 如果邮件内容校验成功 将文件保存到eml目录 并删除邮件对象
|
||||||
if(checkMailContent(mailContent,subject,emlFile)){
|
if (checkMailContent(mailContent, subject, emlFile)) {
|
||||||
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();
|
||||||
|
@ -133,44 +126,46 @@ 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("邮件内容校验失败,邮件移走后手动抛出CheckMailContentException");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// 如果不是下载导致的失败 并且 下载成功,则删除下载的邮件对象
|
|
||||||
if(!(e instanceof DownloadEmailException) && downloadFlag){
|
|
||||||
log.error("Catch Remove Email:"+ subject + StringPool.UNDERSCORE + receiveDate + StringPool.UNDERSCORE);
|
|
||||||
emailServiceManager.removeMail(message,batchesCounter);
|
|
||||||
}
|
|
||||||
//输出异常信息
|
//输出异常信息
|
||||||
log.error("邮件处理异常{},邮件主题:{}:", e, subject);
|
log.error("邮件处理异常{},邮件主题:{}:", e, subject);
|
||||||
}finally {
|
// todo 需要解决其他异常会进入if 删除邮件
|
||||||
|
// 如果不是下载导致的失败 并且 下载成功,则删除下载的邮件对象
|
||||||
|
if (!(e instanceof DownloadEmailException) && downloadFlag) {
|
||||||
|
log.error("Catch Remove Email:" + subject + StringPool.UNDERSCORE + receiveDate + StringPool.UNDERSCORE);
|
||||||
|
emailServiceManager.removeMail(message, batchesCounter);
|
||||||
|
}
|
||||||
|
|
||||||
|
} 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -178,6 +173,7 @@ public class SpectrumParsingActuator implements Runnable{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验邮件内容是否符合IMS2.0格式
|
* 校验邮件内容是否符合IMS2.0格式
|
||||||
|
*
|
||||||
* @param mailContent
|
* @param mailContent
|
||||||
* @param subject
|
* @param subject
|
||||||
* @param emlFile
|
* @param emlFile
|
||||||
|
@ -185,15 +181,15 @@ public class SpectrumParsingActuator implements Runnable{
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* @throws MessagingException
|
* @throws MessagingException
|
||||||
*/
|
*/
|
||||||
private boolean checkMailContent(StringBuilder mailContent,String subject,File emlFile) throws IOException, MessagingException {
|
private boolean checkMailContent(StringBuilder mailContent, String subject, File emlFile) throws IOException, MessagingException {
|
||||||
return SpectrumUtils.checkMailContent(mailContent,subject,emlFile,SpectrumSource.FORM_EMAIL_SERVICE.getSourceType());
|
return SpectrumUtils.checkMailContent(mailContent, subject, emlFile, SpectrumSource.FORM_EMAIL_SERVICE.getSourceType());
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user