It's hard to give a good headline to my problem, but it is. At first I do this on windows, and it can be used in the linux box too, so I need the fix to work on both systems. I keep track of the directory for new files. I basically look at directory files and compare them over and over and process only new files. The problem is that I keep getting an error when the file does not end, when I try to process the file.
public class LiveDetectionsProvider extends DetectionsProvider { protected LiveDetectionsProvider.MonitorDirectory monitorDirectory = null; protected TimeModel timeModel = null; private ArrayList<String> loaded = new ArrayList(); private File topLayerFolder = null; public LiveDetectionsProvider(String directory, String id) { super(directory, id); timeModel = super.timeModel; } public void initialize() { try { topLayerFolder = new File(directory); File[] dir = topLayerFolder.listFiles(); for (File file : dir) { loaded.add(file.getName()); } monitorDirectory = new MonitorDirectory(); monitorDirectory.execute(); } catch (Exception ex) { Logger.getLogger(LiveDetectionsProvider.class.getName()).log(Level.SEVERE, "Failed to read detection\n{0}", ex.getMessage()); } super.initialize(); } public void uninitialize() { super.uninitialize(); if (monitorDirectory != null) { monitorDirectory.continuing = false; } } protected class MonitorDirectory extends SwingWorker<Void, Void> { public boolean continuing = true; private ExecutorService executor = null; private CompletionService<Object> completionService = null; @Override protected Void doInBackground() throws Exception { int count = 0; executor = Executors.newFixedThreadPool(1); completionService = new ExecutorCompletionService<>(executor); while (continuing && topLayerFolder != null) { File[] dir = topLayerFolder.listFiles(); Thread.sleep(10); ArrayList<File> filesToLoad = new ArrayList(); for (File file : dir) { if (!loaded.contains(file.getName())) { long filesize = 0; boolean cont = true; while (cont) { if (file.length() == filesize) { cont = false; Thread.sleep(3); filesToLoad.add(file); } else { filesize = file.length(); Thread.sleep(3); } } Thread.sleep(3); } } for (File file : filesToLoad) { timeModel.setLoadingData(LiveDetectionsProvider.this.hashCode(), true); completionService.submit(Executors.callable(new ReadDetection(file, false))); while (completionService.take() == null) { Thread.sleep(2); } loaded.add(file.getName()); count++; Logger.getLogger(LiveDetectionsProvider.class.getName()).log(Level.SEVERE, "Detection Message Count:" + count); } detectionsModel.fireStateChanged(DetectionsModel.CHANGE_EVENT_DETECTIONS); timeModel.setLoadingData(LiveDetectionsProvider.this.hashCode(), false); } return null; } } }
The file is processed in line using
completionService.submit(Executors.callable(new ReadDetection(file, false)));
The file has not finished writing at this point and thus does not work. I tried sleeping my thread to slow it down, and I tried to check the size of the file, which did not change. My test case: I will unzip a tar file containing thousands of files of size 1000 KB.
source share