-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSVLoader.java
80 lines (70 loc) · 2.13 KB
/
CSVLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package darun.csvloader;
import java.io.BufferedReader;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CSVLoader implements JobAllocator {
private ExecutorService pool = Executors.newFixedThreadPool(50);
private Job job = null;
private JobExecutor executor = null;
private int jobUnitSize = 0;
//private int exeCount = 1;
CSVLoader(int jobUnitSize){
this.jobUnitSize = jobUnitSize;
executor = new DatabaseWriter(jobUnitSize);
}
public void allocate(){
if(job != null){
if(executor.addJob(job)){
job = null;
}
else{
pool.execute(executor);
executor = new DatabaseWriter(jobUnitSize);
allocate();
//exeCount++;
//System.out.println("executor ="+exeCount);
}
}else{
pool.execute(executor);
}
}
public void clean() throws Exception{
pool.shutdown();
while(!pool.isTerminated()){
Thread.sleep(500);
}
}
public void load(String fileName){
try {
BufferedReader br = new BufferedReader( new FileReader(fileName));
String strLine = "";
String[] values = null;
while( (strLine = br.readLine()) != null){
values = strLine.split(",");
job = new Job();
job.setJobDefn(values);
allocate();
}
allocate();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println("started at: "+sdf.format(new Date(System.currentTimeMillis())));
try{
int loadUnitSize = Integer.parseInt(ConfigLoader.getConfig().getProperty("LOAD_UNIT_SIZE"));
CSVLoader loader = new CSVLoader(loadUnitSize);
loader.load(ConfigLoader.getConfig().getProperty("LOAD_FILE_NAME"));
loader.clean();
}catch(Exception e){
e.printStackTrace();
}
System.out.println("ended at: "+sdf.format(new Date(System.currentTimeMillis())));
}
}