Reputation:
I have searched for this everywhere but could not find a suitable solution for my case.
I have a program that takes txt file as program argument, create output txt file, check type of the file
-i /Users/me/test.txt -o /Users/me/output.txt -t TEST
I have a processor to process/extract info from the txt file, check if the extracted data exists via api call to the service
I have relatively large txt file to read and check line by line. I would like to read it with executor service.
I have a processor to process the input file, extract data:
public class FileParser {
private String line;
public List<FileInfo> processFile(InputStream inputStream) throws IOException {
List<FileInfo> resultSet = new ArrayList<>();
try (Scanner sc = new Scanner(inputStream, "UTF_8")) {
line = sc.nextLine();
while (line != null) {
if (!line.trim().startWith("*") && !line.isEmpty()) {
String number = line.subString(1, 4);
String id = line.subString(5, 10);
FileInfo fileInfo = new FileInfo(number, id);
resultSet.add(fileInfo);
}
if (sc.hasNextLine()) {
line = sc.nextLine();
} else {
break;
}
}
if (sc.ioException() != null) {
throw sc.ioException();
}
}
return resultSet;
}
}
In main, I have:
public class ValidatorApplication {
public static void main(String[] args) {
ValidatorApplication validator = new ValidatorApplication();
// parse program argument like above -i, -o, -t
validator.parseArguments(arg);
// output txt file model
Report report = new Report();
FileParser fileParser = new FileParser();
// service for API call
ApiService apiService = new ApiService();
// Output stream - how the output txt file look like
ReportGenerator reportGenerator = new ReportGenerator();
report.setFileName(validator.outputFilePath);
try (InputStream inputStream = new FileInputStream(validator.inputFilePath)) {
if (validator.type.equalsIgnoreCase("TEST")) {
List<FileInfo> fileInfoList = fileParser.processFile(inputStream);
for(FileInfo fileInfo : fileInfoList) {
if (!apiService.isDataExist(fileInfo)) {
report.getUnmatched().add(fileInfo);
}
}
}
reportGenerator.generator(report);
} catch (IOException e) {
e.printStackTrace();
}
}
}
This program works fine. I am having trouble figuring out how to apply ExecutorService on top of what is here. I just want some basic thread pools to process the file.
Upvotes: 0
Views: 593
Reputation: 9463
So what you might want to do is:
With that solution you can parallel process the records. But the resulting output file may have a different sequence of lines.
Upvotes: 2