In my java web application is a file based integration. They used to send the bunch of xml files (example: 10000) in our production server opt/app/proceed/ folder. But as per the current configuration our application able to handle 200 files in a sequential processing. Due to this, delay in the processing of files. I am trying to increase the number of files processing in parallel way. Please find the block of code for your reference.
public class FileEx {
public static void main(String[] args) throws IOException {
String fileDir = "C:\\Users\\inputfiles"; //contains more than 10000 files
new FileEx().traverseFilesFromDir(new File(fileDir));
}
public void traverseFilesFromDir(File dir) throws IOException {
List<File> files = new ArrayList<File>();
if (dir == null || !dir.isDirectory()) {
throw new IllegalArgumentException("Not a valid directory (value: " + dir + ").");
}
File[] acknFiles = dir.listFiles();
int fileCount = (acknFiles == null ? 0 : acknFiles.length);
System.out.println("fileCount:::::::::" + fileCount);
Arrays.sort(acknFiles, new Comparator<File>() {
public int compare(File f1, File f2) {
return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
}
});
**int maxNoFiles = acknFiles.length <= 500 ? acknFiles.length : 500;**
System.out.println(acknFiles.length + " Ackn found and starting to process oldest " + maxNoFiles + " files.");
for (int i = 0; i < maxNoFiles; i++) {
files.add(acknFiles[i]);
}
int fileCount1 = (files == null ? 0 : files.size());
if (fileCount1 > 0) {
for (int i = 0; i < fileCount1; i++) {
boolean success = true;// processFile(files.get(i));
if (success) {
System.out.println("File Successfully processed.");
}
}
}
}
}
How to proceed to change the way of file processing. Awaiting support/guidance needed.
// java 8.1 onwards Paraller stream you can use // here paralell() by default is executed using ForkJoinPool.commonPool()