问题遇到的现象和发生背景
入库时不会定时调度
问题相关代码,请勿粘贴截图
@EnableScheduling
@Component
public class PrecipitationCsvDataScheduler {
private Logger logger = LoggerFactory.getLogger(PrecipitationCsvDataScheduler.class);
@Autowired
TLiveDataService tLiveDataService;
@Scheduled(cron="*/30 * * * * ?")
public void startScheduler() {
logger.info("----文件数据入库任务开始----");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Date startDate = cal.getTime();
try {
getPrecipitationForMusic(startDate);
} catch (IOException ioException) {
logger.error("----文件数据入库任务失败:{}----",startDate);
ioException.printStackTrace();
}
}
public void getPrecipitationForMusic(Date startDate) throws IOException {
Date endDate = new Date(startDate.getTime());
System.out.println(endDate);
String path = "E:\\需要入库的路径";
File file = new File(path);
File[] fileList = file.listFiles();
Pattern p = Pattern.compile("obs-\\d{10}-\\d{10}.nc");
TLiveDataDTO tLiveDataDTO =null;
List<TLiveDataDTO> list = tLiveDataService.findTest();
Map<String, TLiveDataDTO> maps = new HashMap<>();
for (TLiveDataDTO tlo : list) {
maps.put(tlo.getAbsolutePath(), tlo);
}
for (File file1:fileList){
if (maps.get(file1.getAbsolutePath())!=null){
logger.info("文件已经存在"+file1.getAbsolutePath());
continue;
}
Matcher matcher = p.matcher(file1.getName());
while(matcher.find()){
System.out.println(path+File.separator+matcher.group());
tLiveDataDTO = new TLiveDataDTO();
String[] filetime = file1.getName().split("-");
tLiveDataDTO.setType(filetime[0]);
tLiveDataDTO.setTime(filetime[1].replace(".nc",""));
tLiveDataDTO.setAbsolutePath(file1.getAbsolutePath());
tLiveDataService.save(tLiveDataDTO);
}
}
}
}
运行结果及报错内容
结果就是入库用正则指定这个类型,不是则无法入库,判断入库文件名,如果库中有该文件,则执行下一次for