fix:1.修改能谱过程日志功能,解决并发情况下有些日志时间顺序错误写入问题
This commit is contained in:
parent
40b793811b
commit
aeeec179ad
|
@ -111,27 +111,18 @@ public class EmailServiceManager {
|
|||
String status = EmailLogManager.STATUS_SUCCESS;
|
||||
try{
|
||||
//配置邮件服务属性
|
||||
Properties props = new Properties();
|
||||
props.put("mail.store.protocol","imap");
|
||||
props.put("mail.imap.host",email.getEmailServerAddress());
|
||||
props.put("mail.imap.port",email.getPort());
|
||||
Properties properties = new Properties();
|
||||
properties.put("mail.store.protocol", "imap");
|
||||
properties.put("mail.imap.host", email.getEmailServerAddress());
|
||||
properties.put("mail.imap.port",email.getPort());
|
||||
properties.put("mail.imap.ssl.enable", "true");
|
||||
//获取邮件回话
|
||||
final Session session = Session.getInstance(props, new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(email.getUsername(), email.getPassword());
|
||||
}
|
||||
});
|
||||
Map<String, String> iam = new HashMap<>();
|
||||
iam.put("name", "myname");
|
||||
iam.put("version", "1.0.0");
|
||||
iam.put("vendor", "myclient");
|
||||
iam.put("support-email", "testmail@test.com");
|
||||
final Session session = Session.getDefaultInstance(properties);
|
||||
|
||||
//获取smtp协议的存储对象
|
||||
store = (IMAPStore) session.getStore();
|
||||
//连接
|
||||
store.connect();
|
||||
store.id(iam);
|
||||
store.connect(email.getUsername(),email.getPassword());
|
||||
//获取收件箱
|
||||
folder = store.getFolder("INBOX");//INBOX
|
||||
folder.open(Folder.READ_WRITE);
|
||||
|
|
|
@ -5,7 +5,9 @@ import org.jeecg.common.email.EmailLogManager;
|
|||
import org.jeecg.common.email.EmailServiceManager;
|
||||
import org.jeecg.common.properties.TaskProperties;
|
||||
import org.jeecg.modules.email.EmailProperties;
|
||||
import org.jeecg.modules.eneity.event.SpectrumLog;
|
||||
import org.jeecg.modules.spectrum.EmailCounter;
|
||||
import org.jeecg.modules.spectrum.SpectrumLogManager;
|
||||
import org.jeecg.modules.spectrum.SpectrumParsingActuator;
|
||||
import org.jeecg.modules.spectrum.SpectrumServiceQuotes;
|
||||
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
|
||||
|
@ -63,8 +65,12 @@ public class EmailParsingActuator extends Thread{
|
|||
}catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
//清除本批次日志缓存
|
||||
//清除本批次邮件日志缓存
|
||||
EmailLogManager.getInstance().clear();
|
||||
//保存本批次所有能谱日志
|
||||
SpectrumLogManager.mailSpectrumLogManager.saveAllLog();
|
||||
//清除本批次能谱日志缓存
|
||||
SpectrumLogManager.mailSpectrumLogManager.clear();
|
||||
//关闭资源
|
||||
emailServiceManager.close();
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import org.jeecg.modules.service.BlockConstant;
|
|||
import org.jeecg.modules.file.FileOperation;
|
||||
import org.jeecg.modules.spectrum.AbstractSpectrumHandler;
|
||||
import org.jeecg.modules.spectrum.SamplephdSpectrum;
|
||||
import org.jeecg.modules.spectrum.SpectrumLogManager;
|
||||
import org.jeecg.modules.spectrum.SpectrumServiceQuotes;
|
||||
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
@ -83,6 +84,11 @@ public class FileSourceHandleManager{
|
|||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
//保存本批次所有能谱日志
|
||||
SpectrumLogManager.filesourceSpectrumLogManager.saveAllLog();
|
||||
//清除本批次能谱日志缓存
|
||||
SpectrumLogManager.filesourceSpectrumLogManager.clear();
|
||||
}
|
||||
long end = System.currentTimeMillis();
|
||||
long sleepTime = taskProperties.getFilesourceDirExecCycle() - (end-start);
|
||||
|
@ -131,8 +137,6 @@ public class FileSourceHandleManager{
|
|||
e.printStackTrace();
|
||||
}finally {
|
||||
taskLatch.countDown();
|
||||
//解析成功或者失败都会删除源文件
|
||||
// spectrumFile.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import org.jeecg.modules.service.BlockConstant;
|
|||
import org.jeecg.modules.file.FileOperation;
|
||||
import org.jeecg.modules.spectrum.AbstractSpectrumHandler;
|
||||
import org.jeecg.modules.spectrum.SamplephdSpectrum;
|
||||
import org.jeecg.modules.spectrum.SpectrumLogManager;
|
||||
import org.jeecg.modules.spectrum.SpectrumServiceQuotes;
|
||||
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
@ -83,6 +84,11 @@ public class UndealHandleManager{
|
|||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
//保存本批次所有能谱日志
|
||||
SpectrumLogManager.undelSpectrumLogManager.saveAllLog();
|
||||
//清除本批次能谱日志缓存
|
||||
SpectrumLogManager.undelSpectrumLogManager.clear();
|
||||
}
|
||||
long end = System.currentTimeMillis();
|
||||
long sleepTime = taskProperties.getUndealDirExecCycle() - (end-start);
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package org.jeecg.modules.eneity.event;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 能谱日志存储封装对象
|
||||
*/
|
||||
public class SpectrumLog {
|
||||
|
||||
/**
|
||||
* 日志内容
|
||||
*/
|
||||
@Getter
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 存储路径
|
||||
*/
|
||||
@Getter
|
||||
private String storePath;
|
||||
|
||||
public SpectrumLog(String storePath,String content) {
|
||||
this.storePath = storePath;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public SpectrumLog() {
|
||||
}
|
||||
}
|
|
@ -158,4 +158,5 @@ public class FileOperation {
|
|||
}
|
||||
return filePath;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import org.jeecg.common.email.EmailLogEvent;
|
|||
import org.jeecg.common.email.EmailLogManager;
|
||||
import org.jeecg.common.properties.SpectrumPathProperties;
|
||||
import org.jeecg.modules.base.enums.DataType;
|
||||
import org.jeecg.modules.eneity.event.SpectrumLog;
|
||||
import org.jeecg.modules.enums.SpectrumSource;
|
||||
import org.jeecg.modules.exception.FileRepeatException;
|
||||
import org.jeecg.modules.file.FileOperation;
|
||||
|
@ -249,4 +250,18 @@ public abstract class AbstractSpectrumHandler extends AbstractChain {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理能谱日志到队列
|
||||
*/
|
||||
protected void sendSpectrumLogToQueue(String filePath,String logContent){
|
||||
SpectrumLog spectrumLog = new SpectrumLog(filePath,logContent);
|
||||
if(SpectrumSource.FORM_EMAIL_SERVICE.getSourceType().equals(spectrumSource)){
|
||||
SpectrumLogManager.mailSpectrumLogManager.offer(Thread.currentThread().getId(),spectrumLog);
|
||||
}else if(SpectrumSource.FROM_FILE_SOURCE.getSourceType().equals(spectrumSource)){
|
||||
SpectrumLogManager.filesourceSpectrumLogManager.offer(Thread.currentThread().getId(),spectrumLog);
|
||||
}else if(SpectrumSource.FORM_FILE_UNDEL.getSourceType().equals(spectrumSource)){
|
||||
SpectrumLogManager.undelSpectrumLogManager.offer(Thread.currentThread().getId(),spectrumLog);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -166,7 +166,7 @@ public class AlertSpectrum extends AbstractSpectrumHandler{
|
|||
@Override
|
||||
protected void saveLogToLogDir() throws IOException {
|
||||
final SpectrumPathProperties properties = super.spectrumServiceQuotes.getSpectrumPathProperties();
|
||||
final String dirPath = properties.getRootPath()+File.separator+properties.getLogPath()+File.separator+this.getFileSaveRelativePath();
|
||||
final String dirPath = properties.getRootPath()+properties.getLogPath()+File.separator+this.getFileSaveRelativePath();
|
||||
final String fileName = super.spectrumFile.getName().replace(this.currDataType.getSuffix(),LOG_FILE_SUFFIX);
|
||||
final String finalPath = dirPath+File.separator+fileName;
|
||||
|
||||
|
@ -187,6 +187,6 @@ public class AlertSpectrum extends AbstractSpectrumHandler{
|
|||
logContent.append("------------------- ").append("Write Data into Database ").append(handleFlag).append(" at ").append(DateUtils.formatDate(this.endIntoDatabaseTime,"yyyy-MM-dd HH:mm:ss")).append(" --------------------");
|
||||
logContent.append(System.lineSeparator()).append(System.lineSeparator()).append(System.lineSeparator());
|
||||
|
||||
FileOperation.saveOrAppendFile(finalPath,logContent.toString(),true);
|
||||
super.sendSpectrumLogToQueue(finalPath,logContent.toString());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,9 +13,7 @@ import org.jeecg.modules.file.FileOperation;
|
|||
import org.jeecg.modules.native_jni.EnergySpectrumHandler;
|
||||
import org.jeecg.modules.native_jni.struct.SOHSpectrumStruct;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
@ -167,9 +165,10 @@ public class HealthStatusSpectrum extends AbstractSpectrumHandler{
|
|||
logContent.append("------------------- ").append("Write Data into Database Successfully at ").append(DateUtils.formatDate(this.endIntoDatabaseTime,"yyyy-MM-dd HH:mm:ss")).append(" --------------------");
|
||||
|
||||
final SpectrumPathProperties properties = this.spectrumServiceQuotes.getSpectrumPathProperties();
|
||||
final String dirPath = properties.getRootPath()+File.separator+properties.getLogPath()+File.separator+this.getFileSaveRelativePath();
|
||||
final String dirPath = properties.getRootPath()+properties.getLogPath()+File.separator+this.getFileSaveRelativePath();
|
||||
final String fileName = super.spectrumFile.getName().replace(this.currDataType.getSuffix(),LOG_FILE_SUFFIX);
|
||||
final String finalPath = dirPath+ File.separator+fileName;
|
||||
FileOperation.saveOrAppendFile(finalPath,logContent.toString(),true);
|
||||
|
||||
super.sendSpectrumLogToQueue(finalPath,logContent.toString());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -161,9 +161,10 @@ public class MetSpectrum extends AbstractSpectrumHandler{
|
|||
logContent.append("------------------- ").append("Write Data into Database Successfully at ").append(DateUtils.formatDate(this.endIntoDatabaseTime,"yyyy-MM-dd HH:mm:ss")).append(" --------------------");
|
||||
|
||||
final SpectrumPathProperties properties = this.spectrumServiceQuotes.getSpectrumPathProperties();
|
||||
final String dirPath = properties.getRootPath()+File.separator+properties.getLogPath()+File.separator+this.getFileSaveRelativePath();
|
||||
final String dirPath = properties.getRootPath()+properties.getLogPath()+File.separator+this.getFileSaveRelativePath();
|
||||
final String fileName = super.spectrumFile.getName().replace(this.currDataType.getSuffix(),LOG_FILE_SUFFIX);
|
||||
final String finalPath = dirPath+File.separator+fileName;
|
||||
FileOperation.saveOrAppendFile(finalPath,logContent.toString(),true);
|
||||
|
||||
super.sendSpectrumLogToQueue(finalPath,logContent.toString());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,10 +6,8 @@ import org.jeecg.common.properties.SpectrumPathProperties;
|
|||
import org.jeecg.common.util.DateUtils;
|
||||
import org.jeecg.modules.base.enums.DataType;
|
||||
import org.jeecg.modules.base.enums.SystemType;
|
||||
import org.jeecg.modules.file.FileOperation;
|
||||
import org.jeecg.modules.native_jni.struct.BgAnalyseResult;
|
||||
import org.jeecg.modules.native_jni.struct.EnergySpectrumStruct;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
@ -798,24 +796,24 @@ public class ParsingProcessLog extends AbstractAutoLogOrReport{
|
|||
if(Objects.nonNull(this.analysisLog)){
|
||||
this.storageLog.append(this.analysisLog);
|
||||
}
|
||||
this.saveLog(this.storageLog.toString());
|
||||
this.offfLogToQueue(this.storageLog.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存日志到log目录
|
||||
* 保存日志对象到队列
|
||||
*/
|
||||
private void saveLog(String log) throws IOException {
|
||||
private void offfLogToQueue(String log){
|
||||
//保存日志到指定目录文件
|
||||
final SpectrumPathProperties properties = spectrumHandler.spectrumServiceQuotes.getSpectrumPathProperties();
|
||||
final String logFileName = spectrumHandler.spectrumFile.getName().replace(DataType.SAMPLEPHD.getSuffix(),AbstractSpectrumHandler.LOG_FILE_SUFFIX);
|
||||
StringBuilder finalLogPath = new StringBuilder();
|
||||
finalLogPath.append(properties.getRootPath());
|
||||
finalLogPath.append(File.separator);
|
||||
finalLogPath.append(properties.getLogPath());
|
||||
finalLogPath.append(File.separator);
|
||||
finalLogPath.append(spectrumHandler.getFileSaveRelativePath());
|
||||
finalLogPath.append(File.separator);
|
||||
finalLogPath.append(logFileName);
|
||||
FileOperation.saveOrAppendFile(finalLogPath.toString(),log,true);
|
||||
StringBuilder finalPath = new StringBuilder();
|
||||
finalPath.append(properties.getRootPath());
|
||||
finalPath.append(properties.getLogPath());
|
||||
finalPath.append(File.separator);
|
||||
finalPath.append(spectrumHandler.getFileSaveRelativePath());
|
||||
finalPath.append(File.separator);
|
||||
finalPath.append(logFileName);
|
||||
|
||||
spectrumHandler.sendSpectrumLogToQueue(finalPath.toString(),log);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
package org.jeecg.modules.spectrum;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.modules.eneity.event.SpectrumLog;
|
||||
import org.jeecg.modules.file.FileOperation;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 能谱日志管理器
|
||||
*/
|
||||
@Slf4j
|
||||
public class SpectrumLogManager {
|
||||
|
||||
/**
|
||||
* 能谱日志队列
|
||||
*/
|
||||
private LinkedHashMap<Long, SpectrumLog> execLogMap = new LinkedHashMap<>();
|
||||
|
||||
public static SpectrumLogManager mailSpectrumLogManager = new SpectrumLogManager();
|
||||
|
||||
public static SpectrumLogManager filesourceSpectrumLogManager = new SpectrumLogManager();
|
||||
|
||||
public static SpectrumLogManager undelSpectrumLogManager = new SpectrumLogManager();
|
||||
|
||||
/**
|
||||
* 添加能谱日志
|
||||
* @param threadId
|
||||
* @param log
|
||||
*/
|
||||
public void offer(Long threadId, SpectrumLog log){
|
||||
synchronized (execLogMap){
|
||||
execLogMap.put(threadId,log);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 本批次所有能谱处理完毕清空日志队列
|
||||
*/
|
||||
public void clear(){
|
||||
synchronized (execLogMap){
|
||||
execLogMap.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存能谱日志
|
||||
* @param threadId
|
||||
*/
|
||||
public void saveLog(Long threadId){
|
||||
if(!execLogMap.isEmpty() && execLogMap.containsKey(threadId)){
|
||||
final SpectrumLog spectrumLog = execLogMap.get(threadId);
|
||||
if(Objects.nonNull(spectrumLog)){
|
||||
try {
|
||||
FileOperation.saveOrAppendFile(spectrumLog.getStorePath(),spectrumLog.getContent(),true);
|
||||
}catch (IOException e){
|
||||
log.error("Failed to write energy spectrum log, path: {}",spectrumLog.getStorePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存所有日志
|
||||
*/
|
||||
public void saveAllLog(){
|
||||
if(!execLogMap.isEmpty()){
|
||||
execLogMap.forEach((k,v)->{
|
||||
this.saveLog(k);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -63,6 +63,8 @@ public class SpectrumParsingActuator implements Runnable{
|
|||
public void run() {
|
||||
String subject = null;
|
||||
try {
|
||||
//线程开始初始化时,初始本线程负责的能谱日志事件
|
||||
SpectrumLogManager.mailSpectrumLogManager.offer(Thread.currentThread().getId(),null);
|
||||
//获取邮件主题
|
||||
subject = emailServiceManager.getMailSubject(message);
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user