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
jeecg-boot-base-core/src/main/java/org/jeecg/common/email
jeecg-module-auto-process/src/main/java/org/jeecg/modules
jeecg-server-cloud/armd-auto-process-start/src/main/java/org/jeecg

View File

@ -1,12 +1,11 @@
package org.jeecg.common.email; package org.jeecg.common.email;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import lombok.Setter; import lombok.Setter;
import org.jeecg.common.constant.StringConstant; import org.jeecg.common.constant.StringConstant;
import org.jeecg.common.properties.SpectrumPathProperties; import org.jeecg.common.properties.SpectrumPathProperties;
import org.jeecg.common.util.DateUtils; import org.jeecg.common.util.DateUtils;
import org.springframework.util.CollectionUtils;
import java.io.File; import java.io.File;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.*; import java.util.*;
@ -44,11 +43,6 @@ public class EmailLogManager {
@Setter @Setter
private EmailLogEvent getAllIdLogEvent = null; private EmailLogEvent getAllIdLogEvent = null;
/**
* 完成解析邮件流程的线程id集合
*/
private LinkedList<Long> completeThreadIds = new LinkedList<>();
/** /**
* 线程邮件日志队列 * 线程邮件日志队列
*/ */
@ -64,9 +58,6 @@ public class EmailLogManager {
public EmailLogManager(SpectrumPathProperties spectrumPathProperties) { public EmailLogManager(SpectrumPathProperties spectrumPathProperties) {
this.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 * @param event
*/ */
public void offer(Long threadId,EmailLogEvent event){ public void offer(Long threadId,EmailLogEvent event){
synchronized (completeThreadIds){ synchronized (queue){
if(queue.containsKey(threadId)){ if(queue.containsKey(threadId)){
queue.get(threadId).offer(event); queue.get(threadId).offer(event);
if(EmailLogManager.DONE.equals(event.getLogProcess())){
completeThreadIds.offer(threadId);
completeThreadIds.notify();
}
}else{ }else{
LinkedList<EmailLogEvent> logEventList = new LinkedList<>(); LinkedList<EmailLogEvent> logEventList = new LinkedList<>();
logEventList.offer(event); logEventList.offer(event);
@ -97,61 +84,39 @@ public class EmailLogManager {
} }
} }
/**
* 清空队列日志
*/
public void clear(){
synchronized (queue){
this.setConnectLogEvent(null);
this.setGetAllIdLogEvent(null);
this.queue.clear();
}
}
/** /**
* 获取日志事件 * 获取日志事件
* @return * @return
* @throws InterruptedException * @throws InterruptedException
*/ */
public LinkedList<EmailLogEvent> take(Long threadId) throws InterruptedException { public void writeLog(Long threadId){
synchronized (completeThreadIds){ synchronized (queue){
final LinkedList<EmailLogEvent> logEventList = queue.remove(threadId); if(queue.containsKey(threadId)){
return logEventList; final LinkedList<EmailLogEvent> logEventList = queue.remove(threadId);
} if(!CollectionUtils.isEmpty(logEventList)){
} if(Objects.nonNull(getAllIdLogEvent)){
logEventList.addFirst(getAllIdLogEvent);
/**
* 获取解析邮件完成的线程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(Objects.nonNull(getAllIdLogEvent)){
logEventList.addFirst(getAllIdLogEvent);
}
if(Objects.nonNull(connectLogEvent)){
logEventList.addFirst(connectLogEvent);
}
List<String> logContentList = new ArrayList<>();
logEventList.forEach(logEvent->{
final String logContent = getLogContent(logEvent);
logContentList.add(logContent);
});
writeLog(GS_TYPE_GET,logContentList);
} }
} catch (InterruptedException e) { if(Objects.nonNull(connectLogEvent)){
e.printStackTrace(); logEventList.addFirst(connectLogEvent);
}
List<String> logContentList = new ArrayList<>();
logEventList.forEach(logEvent->{
final String logContent = this.getLogContent(logEvent);
logContentList.add(logContent);
});
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(); LocalDateTime now = LocalDateTime.now();
StringBuilder logFilePath = new StringBuilder(); StringBuilder logFilePath = new StringBuilder();
logFilePath.append(spectrumPathProperties.getRootPath()); logFilePath.append(spectrumPathProperties.getRootPath());

View File

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

View File

@ -63,9 +63,8 @@ public class EmailParsingActuator extends Thread{
}catch (InterruptedException e) { }catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
}finally { }finally {
//每批次连接关闭后清空邮箱全局日志 //清除本批次日志缓存
EmailLogManager.getInstance().setConnectLogEvent(null); EmailLogManager.getInstance().clear();
EmailLogManager.getInstance().setGetAllIdLogEvent(null);
//关闭资源 //关闭资源
emailServiceManager.close(); 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); Sample_B_Analysis bAnalysis = new Sample_B_Analysis(this);
bAnalysis.analysis(); bAnalysis.analysis();
} }
if (this.sourceData.system_type.equals(SystemType.PARTICULATE.getType()) || this.sourceData.system_type.equals(SystemType.GAMMA.getType())) { // 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 sample_g_analysis = new Sample_G_Analysis(super.sourceData, super.spectrumServiceQuotes, super.sampleData);
sample_g_analysis.analysis(); // sample_g_analysis.analysis();
} // }
} }
} }

View File

@ -87,13 +87,19 @@ 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); 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) { } catch (Exception e) {
emailServiceManager.removeMail(message);
e.printStackTrace(); e.printStackTrace();
}finally { }finally {
emailServiceManager.removeMail(message); try {
EmailLogEvent expungeEvent = new EmailLogEvent(EmailLogManager.GS_TYPE_GET,EmailLogManager.DONE); EmailLogEvent expungeEvent = new EmailLogEvent(EmailLogManager.GS_TYPE_GET,EmailLogManager.DONE);
EmailLogManager.getInstance().offer(Thread.currentThread().getId(),expungeEvent); EmailLogManager.getInstance().offer(Thread.currentThread().getId(),expungeEvent);
this.taskLatch.countDown();
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); EmailLogManager.init(spectrumPathProperties);
//校验存储目录是否存在不存在则创建存在无操作 //校验存储目录是否存在不存在则创建存在无操作
checkStorageDirectory(); checkStorageDirectory();
// autoProcessManager.start(systemStartupTime); autoProcessManager.start(systemStartupTime);
undealHandleManager.start(); undealHandleManager.start();
fileSourceHandleManager.start(); fileSourceHandleManager.start();
// 删除过期的文件 // 删除过期的文件