Commit 0d0e1c7ef94599b15dade2f075ab4d6947fc06f5
1 parent
4ccb8977
文件监听改为定时器
Showing
1 changed file
with
24 additions
and
20 deletions
src/main/java/com/example/demo/SaticScheduleTask.java
| ... | ... | @@ -30,7 +30,7 @@ import java.util.*; |
| 30 | 30 | @Configuration |
| 31 | 31 | @EnableScheduling |
| 32 | 32 | @Component |
| 33 | -public class SaticScheduleTask implements InitializingBean { | |
| 33 | +public class SaticScheduleTask { | |
| 34 | 34 | |
| 35 | 35 | private final static Logger logger = LoggerFactory.getLogger(SaticScheduleTask.class); |
| 36 | 36 | |
| ... | ... | @@ -50,6 +50,7 @@ public class SaticScheduleTask implements InitializingBean { |
| 50 | 50 | |
| 51 | 51 | |
| 52 | 52 | @Scheduled(cron = "0 0/10 * * * ?") |
| 53 | + @PostConstruct | |
| 53 | 54 | void getArrivalInfos(){ |
| 54 | 55 | logger.info("票务数据缓存加载: " + LocalDateTime.now()); |
| 55 | 56 | try { |
| ... | ... | @@ -58,18 +59,7 @@ public class SaticScheduleTask implements InitializingBean { |
| 58 | 59 | contrast.getSchedulingS=result.get(0); |
| 59 | 60 | contrast.getSchedulingSSell=result.get(1); |
| 60 | 61 | logger.info("===============================数据存放结束,开始读取文件================================"); |
| 61 | - WatchKey watchKey = null; | |
| 62 | - List<File> files = new ArrayList<>(); | |
| 63 | - while ((watchKey = watchService.poll()) != null) { | |
| 64 | - for (WatchEvent watchEvent : watchKey.pollEvents()) { | |
| 65 | - Path path = (Path)watchEvent.context(); | |
| 66 | - if(path!=null){ | |
| 67 | - files.add(new File(rootPath + File.separator + path)); | |
| 68 | - } | |
| 69 | - | |
| 70 | - } | |
| 71 | - watchKey.reset(); | |
| 72 | - } | |
| 62 | + List<File> files = FileUtil.loopFiles(Paths.get(rootPath),1,new wdbFileFilter()); | |
| 73 | 63 | System.out.println("文件数"+files.size()); |
| 74 | 64 | logger.info("===============================文件数"+files.size()+",读取文件结束================================"); |
| 75 | 65 | //先把处理上一次没匹配到的 |
| ... | ... | @@ -149,13 +139,6 @@ public class SaticScheduleTask implements InitializingBean { |
| 149 | 139 | |
| 150 | 140 | |
| 151 | 141 | |
| 152 | - @Override | |
| 153 | - public void afterPropertiesSet() throws Exception { | |
| 154 | - watchService = FileSystems.getDefault().newWatchService(); | |
| 155 | - Path path = Paths.get(rootPath); | |
| 156 | - path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE); | |
| 157 | - this.getArrivalInfos(); | |
| 158 | - } | |
| 159 | 142 | |
| 160 | 143 | public List<List<File>> getSubList(int length, List<File> list){ |
| 161 | 144 | int size = list.size(); |
| ... | ... | @@ -174,4 +157,25 @@ public class SaticScheduleTask implements InitializingBean { |
| 174 | 157 | } |
| 175 | 158 | return subList; |
| 176 | 159 | } |
| 160 | + | |
| 161 | + | |
| 162 | + public static class wdbFileFilter implements FileFilter { | |
| 163 | + @Override | |
| 164 | + public boolean accept(File file) { | |
| 165 | + if(file.isDirectory()) | |
| 166 | + return false; | |
| 167 | + String name = file.getName(); | |
| 168 | + if(name.endsWith(".wdb")) //mp3则返回true | |
| 169 | + return true; | |
| 170 | + return false; | |
| 171 | + } | |
| 172 | + } | |
| 173 | + | |
| 174 | + public static void main(String[] args) { | |
| 175 | + List<File> files = FileUtil.loopFiles(Paths.get("E:\\sftp\\home\\sftp\\20220725"),1,new wdbFileFilter()); | |
| 176 | + files.forEach(file->{ | |
| 177 | + System.out.println(file.getName()); | |
| 178 | + }); | |
| 179 | + } | |
| 180 | + | |
| 177 | 181 | } |
| 178 | 182 | \ No newline at end of file | ... | ... |