自动处理生成error日志功能实现
新增LogFileUtil工具类,StationNotFoundException异常
This commit is contained in:
parent
758bd27f27
commit
4e49e29324
|
@ -51,6 +51,11 @@ public class TaskProperties implements Serializable {
|
|||
*/
|
||||
private Integer undealDirReceiveNum;
|
||||
|
||||
/**
|
||||
* undeal目录文件分析周期
|
||||
*/
|
||||
private Integer undealFileTimeOut;
|
||||
|
||||
/**
|
||||
* filesource目录文件获取周期
|
||||
*/
|
||||
|
|
|
@ -161,6 +161,15 @@ public class RedisStreamUtil {
|
|||
return redisTemplate.opsForStream().delete(streamKey, recordIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据记录id删除n个消息记录
|
||||
*
|
||||
* @param streamKey
|
||||
*/
|
||||
public boolean del(String streamKey){
|
||||
return redisTemplate.delete(streamKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加Map消息
|
||||
*
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
package org.jeecg.common.util;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import org.jeecg.common.properties.SpectrumPathProperties;
|
||||
import org.jeecg.common.properties.TaskProperties;
|
||||
import org.jeecg.modules.exception.StationNotFoundException;
|
||||
import org.jeecg.modules.spectrum.SpectrumServiceQuotes;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
@Component
|
||||
public class LogFileUtil {
|
||||
|
||||
/**
|
||||
* 任务属性
|
||||
*/
|
||||
@Autowired
|
||||
private TaskProperties taskProperties;
|
||||
/**
|
||||
* 相关Spring组件引用
|
||||
*/
|
||||
@Autowired
|
||||
private SpectrumPathProperties spectrumPathProperties;
|
||||
|
||||
|
||||
public void errorLog(String spectrumFileName, long millis, Exception e) {
|
||||
String logFilePath = spectrumPathProperties.getRootPath() + File.separator + spectrumPathProperties.getLogPath() + File.separator + "Error";
|
||||
File logPath = new File(logFilePath);
|
||||
if (!logPath.exists()) {
|
||||
logPath.mkdir();
|
||||
}
|
||||
String logFileName = logFilePath + File.separator + spectrumFileName.replace("PHD", "log");
|
||||
File logFile = new File(logFileName);
|
||||
StringBuffer out = new StringBuffer();
|
||||
String nowDate = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
|
||||
out.append(nowDate+ StringPool.SPACE + "Data Anlyse Error:");
|
||||
String warning = "";
|
||||
if (e.getClass().equals(StationNotFoundException.class)) {
|
||||
warning = e.getMessage()+StringPool.SPACE+"timeout:"+(long) Math.floor(millis/1000)+",waittime:"+taskProperties.getUndealFileTimeOut();
|
||||
} else {
|
||||
warning = e.getMessage();
|
||||
}
|
||||
out.append(warning);
|
||||
out.append(System.lineSeparator());
|
||||
out.append(System.lineSeparator());
|
||||
out.append(System.lineSeparator());
|
||||
FileWriter writer = null;
|
||||
try {
|
||||
writer = new FileWriter(logFile, true);
|
||||
writer.write(out.toString());
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
} finally {
|
||||
try {
|
||||
if (Objects.nonNull(writer)) {
|
||||
writer.close();
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,9 +1,12 @@
|
|||
package org.jeecg.modules;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.jeecg.common.properties.TaskProperties;
|
||||
import org.jeecg.common.util.DateUtils;
|
||||
import org.jeecg.modules.exception.StationNotFoundException;
|
||||
import org.jeecg.modules.service.BlockConstant;
|
||||
import org.jeecg.modules.file.FileOperation;
|
||||
import org.jeecg.modules.spectrum.AbstractSpectrumHandler;
|
||||
|
@ -13,7 +16,13 @@ import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
|
|||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
|
@ -137,6 +146,9 @@ public class FileSourceHandleManager{
|
|||
spectrumHandler.handler();
|
||||
}
|
||||
}catch (Exception e){
|
||||
//生成日志
|
||||
spectrumServiceQuotes.getLogFileUtil().errorLog(spectrumFile.getName(), 0, e);
|
||||
|
||||
log.error("Parsing the {} file of the filesource directory failed.The reason is {}",spectrumFile.getName(),e.getMessage());
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
|
|
|
@ -1,19 +1,32 @@
|
|||
package org.jeecg.modules;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.jeecg.common.properties.TaskProperties;
|
||||
import org.jeecg.common.util.DateUtils;
|
||||
import org.jeecg.common.util.LogFileUtil;
|
||||
import org.jeecg.modules.exception.StationNotFoundException;
|
||||
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.SpectrumServiceQuotes;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
|
@ -122,6 +135,14 @@ public class UndealHandleManager{
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
long currentMillis = System.currentTimeMillis();
|
||||
long createMillis = currentMillis;
|
||||
//判断redis是否包含文件名称 key
|
||||
if (spectrumServiceQuotes.getRedisStreamUtil().hasKey(spectrumFile.getName())) {
|
||||
createMillis = (long) spectrumServiceQuotes.getRedisStreamUtil().get(spectrumFile.getName());
|
||||
} else {
|
||||
spectrumServiceQuotes.getRedisStreamUtil().set(spectrumFile.getName(), currentMillis);
|
||||
}
|
||||
try {
|
||||
//获取文件内容
|
||||
final String fileContent = FileUtils.readFileToString(spectrumFile,"UTF-8");
|
||||
|
@ -134,13 +155,20 @@ public class UndealHandleManager{
|
|||
spectrumHandler.handler();
|
||||
}
|
||||
}catch (Exception e){
|
||||
//生成日志
|
||||
long millis = currentMillis - createMillis;
|
||||
spectrumServiceQuotes.getLogFileUtil().errorLog(spectrumFile.getName(), millis, e);
|
||||
log.error("The {} file of the undeal directory fails to be parsed again.The reason is {}",spectrumFile.getName(),e.getMessage());
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
this.taskLatch.countDown();
|
||||
//满足undeal文件处理周期时长会删除源文件
|
||||
if ((currentMillis - createMillis) >= taskProperties.getUndealFileTimeOut()) {
|
||||
spectrumServiceQuotes.getRedisStreamUtil().del(spectrumFile.getName());
|
||||
//解析成功或者失败都会删除源文件
|
||||
spectrumFile.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package org.jeecg.modules.exception;
|
||||
|
||||
public class StationNotFoundException extends Exception {
|
||||
|
||||
public StationNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
|
@ -13,8 +13,8 @@ public interface GardsSampleDataMapper extends BaseMapper<GardsSampleData> {
|
|||
@Select(value = "select " +
|
||||
"gsd.SAMPLE_ID as sampleId,gsd.input_file_name as inputFileName " +
|
||||
"from ORIGINAL.GARDS_SAMPLE_AUX gsa inner join ORIGINAL.GARDS_SAMPLE_DATA gsd on gsa.sample_id = gsd.sample_id " +
|
||||
"where gsa.measurement_id = #{measurementId} and gsd.data_type=#{dataType}")
|
||||
public List<GardsSampleData> getSampleIdAndInputFileName(@Param("measurementId") String measurementId, @Param("dataType") String dataType);
|
||||
"where gsa.measurement_id = #{measurementId} and gsd.SAMPLE_TYPE = #{systemType} and gsd.data_type=#{dataType} and TRIM(gsd.SITE_DET_CODE) = #{detectorId}")
|
||||
public List<GardsSampleData> getSampleIdAndInputFileName(@Param("measurementId") String measurementId, @Param("dataType") String dataType, @Param("systemType") String systemType, String detectorId);
|
||||
|
||||
@Update(value = "UPDATE ORIGINAL.GARDS_SAMPLE_DATA SET STATUS=#{status} WHERE INPUT_FILE_NAME=#{inputFileName}")
|
||||
public int updateStatus(@Param("status") String status,@Param("inputFileName") String inputFileName);
|
||||
|
|
|
@ -25,7 +25,7 @@ public interface GardsSampleDataService extends IService<GardsSampleData> {
|
|||
* @param dataType
|
||||
* @return
|
||||
*/
|
||||
public GardsSampleData getSampleIdAndInputFileName(String measurementId,String dataType);
|
||||
public GardsSampleData getSampleIdAndInputFileName(String measurementId,String dataType, String systemType);
|
||||
|
||||
/**
|
||||
* 修改能谱处理状态
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.jeecg.modules.service;
|
|||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.jeecg.modules.base.entity.configuration.GardsStations;
|
||||
import org.jeecg.modules.exception.StationNotFoundException;
|
||||
|
||||
/**
|
||||
* 台站服务
|
||||
|
@ -12,5 +13,5 @@ public interface GardsStationsService extends IService<GardsStations> {
|
|||
* 校验台站编码是否存在
|
||||
* @return
|
||||
*/
|
||||
GardsStations check(String site_code);
|
||||
GardsStations check(String site_code) throws StationNotFoundException;
|
||||
}
|
||||
|
|
|
@ -61,8 +61,9 @@ public class GardsSampleDataServiceImpl extends ServiceImpl<GardsSampleDataMappe
|
|||
* @return
|
||||
*/
|
||||
@Override
|
||||
public GardsSampleData getSampleIdAndInputFileName(String measurementId, String dataType) {
|
||||
final List<GardsSampleData> sampleDatas = this.baseMapper.getSampleIdAndInputFileName(measurementId, dataType);
|
||||
public GardsSampleData getSampleIdAndInputFileName(String measurementId, String dataType, String systemType) {
|
||||
String detectorId = measurementId.substring(0, 8);
|
||||
final List<GardsSampleData> sampleDatas = this.baseMapper.getSampleIdAndInputFileName(measurementId, dataType, systemType, detectorId);
|
||||
if(!CollectionUtils.isEmpty(sampleDatas)){
|
||||
//如果查询出多条则需要根据inputFileName字段降序排序后返回第一个
|
||||
final List<GardsSampleData> sortResult = sampleDatas.stream().sorted(Comparator.comparing(GardsSampleData::getInputFileName).reversed()).collect(Collectors.toList());
|
||||
|
|
|
@ -5,11 +5,14 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.jeecg.modules.base.entity.configuration.GardsStations;
|
||||
import org.jeecg.modules.exception.StationNotFoundException;
|
||||
import org.jeecg.modules.mapper.GardsStationsMapper;
|
||||
import org.jeecg.modules.service.GardsStationsService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@DS("ora")
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
|
@ -22,12 +25,14 @@ public class GardsStationsServiceImpl extends ServiceImpl<GardsStationsMapper, G
|
|||
* @return
|
||||
*/
|
||||
@Override
|
||||
public GardsStations check(String site_code) {
|
||||
public GardsStations check(String site_code) throws StationNotFoundException {
|
||||
LambdaQueryWrapper<GardsStations> gardsStationsQuery = new LambdaQueryWrapper<>();
|
||||
gardsStationsQuery.select(GardsStations::getStationId);
|
||||
gardsStationsQuery.eq(GardsStations::getStationCode,site_code);
|
||||
final GardsStations station = this.baseMapper.selectOne(gardsStationsQuery);
|
||||
Assert.notNull(station,"The station to which this "+site_code+" station code belongs does not exist");
|
||||
if (Objects.isNull(station)) {
|
||||
throw new StationNotFoundException("station_code:"+site_code+"=0");
|
||||
}
|
||||
return station;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -181,8 +181,8 @@ public class Sample_B_Analysis implements BlockConstant {
|
|||
*/
|
||||
private void queryPHDFile() throws FileNotExistException {
|
||||
//查询det和gas能谱文
|
||||
this.detSampleData = spectrumServiceQuotes.getSampleDataService().getSampleIdAndInputFileName(this.sampleStruct.detector_bk_measurement_id, DataTypeAbbr.DETBKPHD.getType());
|
||||
this.gasSampleData = spectrumServiceQuotes.getSampleDataService().getSampleIdAndInputFileName(this.sampleStruct.gas_bk_measurement_id, DataTypeAbbr.GASBKPHD.getType());
|
||||
this.detSampleData = spectrumServiceQuotes.getSampleDataService().getSampleIdAndInputFileName(this.sampleStruct.detector_bk_measurement_id, DataTypeAbbr.DETBKPHD.getType(), this.sampleStruct.system_type);
|
||||
this.gasSampleData = spectrumServiceQuotes.getSampleDataService().getSampleIdAndInputFileName(this.sampleStruct.gas_bk_measurement_id, DataTypeAbbr.GASBKPHD.getType(), this.sampleStruct.system_type);
|
||||
|
||||
//如果找不到sample、det、gas谱文件数据则解析失败修改记录状态
|
||||
if(StringUtils.isEmpty(this.sampleData.getInputFileName()) || Objects.isNull(this.detSampleData) || StringUtils.isEmpty(this.detSampleData.getInputFileName()) || Objects.isNull(this.gasSampleData) || StringUtils.isEmpty(this.gasSampleData.getInputFileName())){
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.jeecg.modules.spectrum;
|
|||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.jeecg.common.properties.*;
|
||||
import org.jeecg.common.util.LogFileUtil;
|
||||
import org.jeecg.common.util.NameStandUtil;
|
||||
import org.jeecg.common.util.RedisStreamUtil;
|
||||
import org.jeecg.modules.datasource.OraDataSourceProperties;
|
||||
|
@ -76,6 +77,8 @@ public class SpectrumServiceQuotes {
|
|||
|
||||
private final NameStandUtil nameStandUtil;
|
||||
|
||||
private final LogFileUtil logFileUtil;
|
||||
|
||||
/**
|
||||
* 原始库插入数据锁
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue
Block a user