I need to read a large csv file (328 MB) and process it .The processing of each row includes calling a Webservice also .
I am using ThreadPoolExecutor for first time . My logic is , i will spit every 100 rows from csv and create a thread that will run and process each row and writes the result in templ file. Once all the threads are finished , i will read the temp files and create a comined output file.
My method that splits the file and creates Threads
private List<Thread> invokeWS(String csvFilename, String tempFolder) {
List<Thread> processCsvThreadList = new ArrayList<Thread>();
//Thread Pool Executer
int corePoolSize = 3;
int maximumPoolSize = 6;
long keepAliveTime = 10;
ThreadFactory threadFactory = Executors.defaultThreadFactory();
ThreadPoolExecutor thrdPoolEx = new ThreadPoolExecutor(corePoolSize,
maximumPoolSize, keepAliveTime, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(2));
try {
BufferedReader bfr = new BufferedReader(new FileReader(csvFilename));
String line = "";
int i = 0;
line = bfr.readLine();
Thread csvThread;
List<String> rowList = new ArrayList<String>();
do {
line = bfr.readLine();
if (line != null) {
rowList.add(line);
i++;
if (i % 100 == 0) {
csvThread = new Thread(new ProcessCsvRow(rowList,
tempFolder));
csvThread.start();
thrdPoolEx.execute(csvThread);
rowList = new ArrayList<String>();
processCsvThreadList.add(csvThread);
}
} else {
if (null != rowList && !rowList.isEmpty()) {
csvThread = new Thread(new ProcessCsvRow(rowList,
tempFolder));
csvThread.start();
thrdPoolEx.execute(csvThread);
processCsvThreadList.add(csvThread);
}
break;
}
} while (true);
} catch (FileNotFoundException fnf) {
fnf.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
thrdPoolEx.shutdown();
}
return processCsvThreadList;
}
My ProcessCsvRow class
public class ProcessCsvRow implements Runnable {
private List<String> csvRowsList;
private String tempDir;
public ProcessCsvRow(List<String> csvRowsList, String tempDir) {
this.csvRowsList = csvRowsList;
this.tempDir = tempDir;
}
@Override
public void run() {
UUID idOne = UUID.randomUUID();
FileWriter fw = null;
BufferedWriter bufferedWriter = null;
try {
String res = "";
fw = new FileWriter(new File(tempDir + "\\" + idOne.toString()+FilePropConstants.FILE_NAME_EXT_TMP));
bufferedWriter = new BufferedWriter(fw);
SentimentAnalyzer sentimentAnalyzer = new SentimentAnalyzer();
for (String csvRow : csvRowsList) {
//calling webservice for each row
res = sentimentAnalyzer.invokeSentWS(csvRow);
bufferedWriter.write(res);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedWriter != null) {
bufferedWriter.flush();
bufferedWriter.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
The issue is if for 5 row csv there should be one temp file created, but when i run this program i am getting two temp files generated which is wrong . I strongly belive its not a logical issue but the way I have implemented ThreadPoolExecuter.
Any help is greatly appreciated .