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