fix:1.修改邮件过程日志功能,解决并发情况下有些日志不能正确写入问题

This commit is contained in:
panbaolin 2023-12-07 08:44:32 +08:00
parent ff6d8b9baa
commit 68460340d5
6 changed files with 49 additions and 77 deletions

View File

@ -1,12 +1,11 @@
package org.jeecg.common.email;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import lombok.Setter;
import org.jeecg.common.constant.StringConstant;
import org.jeecg.common.properties.SpectrumPathProperties;
import org.jeecg.common.util.DateUtils;
import org.springframework.util.CollectionUtils;
import java.io.File;
import java.time.LocalDateTime;
import java.util.*;
@ -44,11 +43,6 @@ public class EmailLogManager {
@Setter
private EmailLogEvent getAllIdLogEvent = null;
/**
* 完成解析邮件流程的线程id集合
*/
private LinkedList<Long> completeThreadIds = new LinkedList<>();
/**
* 线程邮件日志队列
*/
@ -64,9 +58,6 @@ public class EmailLogManager {
public EmailLogManager(SpectrumPathProperties spectrumPathProperties) {
this.spectrumPathProperties = spectrumPathProperties;
EmailLogExecThread logExecThread = new EmailLogExecThread();
logExecThread.setName("email-get-exec-thread");
logExecThread.start();
}
/**
@ -82,13 +73,9 @@ public class EmailLogManager {
* @param event
*/
public void offer(Long threadId,EmailLogEvent event){
synchronized (completeThreadIds){
synchronized (queue){
if(queue.containsKey(threadId)){
queue.get(threadId).offer(event);
if(EmailLogManager.DONE.equals(event.getLogProcess())){
completeThreadIds.offer(threadId);
completeThreadIds.notify();
}
}else{
LinkedList<EmailLogEvent> logEventList = new LinkedList<>();
logEventList.offer(event);
@ -97,46 +84,27 @@ public class EmailLogManager {
}
}
/**
* 清空队列日志
*/
public void clear(){
synchronized (queue){
this.setConnectLogEvent(null);
this.setGetAllIdLogEvent(null);
this.queue.clear();
}
}
/**
* 获取日志事件
* @return
* @throws InterruptedException
*/
public LinkedList<EmailLogEvent> take(Long threadId) throws InterruptedException {
synchronized (completeThreadIds){
public void writeLog(Long threadId){
synchronized (queue){
if(queue.containsKey(threadId)){
final LinkedList<EmailLogEvent> logEventList = queue.remove(threadId);
return logEventList;
}
}
/**
* 获取解析邮件完成的线程id
* @return
* @throws InterruptedException
*/
public Long getCompleteThreadId() throws InterruptedException {
synchronized (completeThreadIds){
if(completeThreadIds.isEmpty()){
completeThreadIds.wait();
return null;
}
final Long threadId = completeThreadIds.removeFirst();
return threadId;
}
}
/**
* 邮件日志执行线程
*/
private class EmailLogExecThread extends Thread{
@Override
public void run() {
for(;;){
try {
final Long threadId = EmailLogManager.getInstance().getCompleteThreadId();
if(Objects.nonNull(threadId)){
final LinkedList<EmailLogEvent> logEventList = EmailLogManager.getInstance().take(threadId);
if(!CollectionUtils.isEmpty(logEventList)){
if(Objects.nonNull(getAllIdLogEvent)){
logEventList.addFirst(getAllIdLogEvent);
}
@ -145,13 +113,10 @@ public class EmailLogManager {
}
List<String> logContentList = new ArrayList<>();
logEventList.forEach(logEvent->{
final String logContent = getLogContent(logEvent);
final String logContent = this.getLogContent(logEvent);
logContentList.add(logContent);
});
writeLog(GS_TYPE_GET,logContentList);
}
} catch (InterruptedException e) {
e.printStackTrace();
this.write(GS_TYPE_GET,logContentList);
}
}
}
@ -339,7 +304,7 @@ public class EmailLogManager {
/**
* 把日志写入文件
*/
private void writeLog(String gsType, List<String> logContentList){
private void write(String gsType, List<String> logContentList){
LocalDateTime now = LocalDateTime.now();
StringBuilder logFilePath = new StringBuilder();
logFilePath.append(spectrumPathProperties.getRootPath());

View File

@ -223,6 +223,7 @@ public class EmailServiceManager {
// props.put("mail.imap.starttls.enable", "true");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
@ -286,6 +287,7 @@ public class EmailServiceManager {
// props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}

View File

@ -63,9 +63,8 @@ public class EmailParsingActuator extends Thread{
}catch (InterruptedException e) {
e.printStackTrace();
}finally {
//每批次连接关闭后清空邮箱全局日志
EmailLogManager.getInstance().setConnectLogEvent(null);
EmailLogManager.getInstance().setGetAllIdLogEvent(null);
//清除本批次日志缓存
EmailLogManager.getInstance().clear();
//关闭资源
emailServiceManager.close();
}

View File

@ -80,9 +80,9 @@ public class SamplephdSpectrum extends AbstractS_D_Q_G_SpectrumHandler {
Sample_B_Analysis bAnalysis = new Sample_B_Analysis(this);
bAnalysis.analysis();
}
if (this.sourceData.system_type.equals(SystemType.PARTICULATE.getType()) || this.sourceData.system_type.equals(SystemType.GAMMA.getType())) {
Sample_G_Analysis sample_g_analysis = new Sample_G_Analysis(super.sourceData, super.spectrumServiceQuotes, super.sampleData);
sample_g_analysis.analysis();
}
// if (this.sourceData.system_type.equals(SystemType.PARTICULATE.getType()) || this.sourceData.system_type.equals(SystemType.GAMMA.getType())) {
// Sample_G_Analysis sample_g_analysis = new Sample_G_Analysis(super.sourceData, super.spectrumServiceQuotes, super.sampleData);
// sample_g_analysis.analysis();
// }
}
}

View File

@ -87,15 +87,21 @@ public class SpectrumParsingActuator implements Runnable{
log.warn("This email {} parsing failed and is not listed in the Met, Alert, SOH, Sample, Detbkphd, QC, Gasbkphd spectra.",subject);
}
}
emailServiceManager.removeMail(message);
} catch (Exception e) {
emailServiceManager.removeMail(message);
e.printStackTrace();
}finally {
emailServiceManager.removeMail(message);
try {
EmailLogEvent expungeEvent = new EmailLogEvent(EmailLogManager.GS_TYPE_GET,EmailLogManager.DONE);
EmailLogManager.getInstance().offer(Thread.currentThread().getId(),expungeEvent);
EmailLogManager.getInstance().writeLog(Thread.currentThread().getId());
}finally {
this.taskLatch.countDown();
}
}
}
/**
* 校验邮件内容完整性

View File

@ -80,7 +80,7 @@ public class JeecgAutoProcessApplication extends SpringBootServletInitializer im
EmailLogManager.init(spectrumPathProperties);
//校验存储目录是否存在不存在则创建存在无操作
checkStorageDirectory();
// autoProcessManager.start(systemStartupTime);
autoProcessManager.start(systemStartupTime);
undealHandleManager.start();
fileSourceHandleManager.start();
// 删除过期的文件