fix:1.修改邮件过程日志功能,解决并发情况下有些日志不能正确写入问题
This commit is contained in:
parent
ff6d8b9baa
commit
68460340d5
|
@ -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,46 +84,27 @@ 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){
|
||||||
|
if(queue.containsKey(threadId)){
|
||||||
final LinkedList<EmailLogEvent> logEventList = queue.remove(threadId);
|
final LinkedList<EmailLogEvent> logEventList = queue.remove(threadId);
|
||||||
return logEventList;
|
if(!CollectionUtils.isEmpty(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(Objects.nonNull(getAllIdLogEvent)){
|
if(Objects.nonNull(getAllIdLogEvent)){
|
||||||
logEventList.addFirst(getAllIdLogEvent);
|
logEventList.addFirst(getAllIdLogEvent);
|
||||||
}
|
}
|
||||||
|
@ -145,13 +113,10 @@ public class EmailLogManager {
|
||||||
}
|
}
|
||||||
List<String> logContentList = new ArrayList<>();
|
List<String> logContentList = new ArrayList<>();
|
||||||
logEventList.forEach(logEvent->{
|
logEventList.forEach(logEvent->{
|
||||||
final String logContent = getLogContent(logEvent);
|
final String logContent = this.getLogContent(logEvent);
|
||||||
logContentList.add(logContent);
|
logContentList.add(logContent);
|
||||||
});
|
});
|
||||||
writeLog(GS_TYPE_GET,logContentList);
|
this.write(GS_TYPE_GET,logContentList);
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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());
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
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);
|
||||||
|
|
||||||
|
EmailLogManager.getInstance().writeLog(Thread.currentThread().getId());
|
||||||
|
}finally {
|
||||||
this.taskLatch.countDown();
|
this.taskLatch.countDown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验邮件内容完整性
|
* 校验邮件内容完整性
|
||||||
|
|
|
@ -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();
|
||||||
// 删除过期的文件
|
// 删除过期的文件
|
||||||
|
|
Loading…
Reference in New Issue
Block a user