-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathMain.java
131 lines (117 loc) · 4.77 KB
/
Main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.io.PrintStream;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import job.CallbackTask;
import job.Job;
import utils.CustomPrintStream;
import utils.SameThreadExecutorService;
public class Main {
ExecutorService service;
JobInitializer initializer;
private Job readConfig;
private Job readFromFile;
private Job readFromDb;
private Job httpReq;
public static void main(String[] args) {
Main test = new Main();
test.setup();
test.sequentialExecutionsWithCallbacks();
// test.asyncCalls();
//test.orderedCalls();
}
private void setup() {
//change implementation of standard output stream.
PrintStream stream = new CustomPrintStream(System.out);
System.setOut(stream);
//initialize jobs
this.initializer = new JobInitializer();
this.readConfig = initializer.getReadConfig();
this.readFromFile = initializer.getReadFromFile();
this.readFromDb = initializer.getReadFromDb();
this.httpReq = initializer.getHttpReq();
}
//Sequential calls and sequential execution
private void sequentialExecutionsWithCallbacks() {
service = new SameThreadExecutorService();
CallbackTask readConfigTask = new CallbackTask(initializer.getReadConfig(),
arg -> System.out.println("Successfully read from config: " + arg));
CallbackTask readFileTask = new CallbackTask(initializer.getReadFromFile(),
arg -> System.out.println("Successfully read from file: " + arg));
CallbackTask readFromDbTask = new CallbackTask(initializer.getReadFromDb(),
arg -> System.out.println("Successfully read from DB: " + arg));
CallbackTask httpReqTask = new CallbackTask(initializer.getHttpReq(),
arg -> System.out.println("Successfully read from URL: " + arg));
executeAsync(service, readConfigTask);
executeAsync(service, readFileTask);
executeAsync(service, readFromDbTask);
executeAsync(service, httpReqTask);
System.out.println("End of src.Main thread in [sequentialExecutionsWithCallbacks].");
service.shutdown();
}
//async calls. Notify when all jobs are done
private void asyncCalls() {
service = Executors.newFixedThreadPool(4, r -> {
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
return t;
});
CountDownLatch latch = new CountDownLatch(4);
CallbackTask readConfigTask = new CallbackTask(readConfig, arg -> {
latch.countDown();
System.out.println("Successfully read from config: " + arg);
});
CallbackTask readFileTask = new CallbackTask(readFromFile, arg -> {
latch.countDown();
System.out.println("Successfully read from file: " + arg);
});
CallbackTask readFromDbTask = new CallbackTask(readFromDb, arg -> {
latch.countDown();
System.out.println("Successfully read from DB: " + arg);
});
CallbackTask httpReqTask = new CallbackTask(httpReq, arg -> {
latch.countDown();
System.out.println("Successfully read from URL: " + arg);
});
executeAsync(service, readConfigTask);
executeAsync(service, readFileTask);
executeAsync(service, readFromDbTask);
executeAsync(service, httpReqTask);
try {
latch.await();
service.shutdown();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("End of src.Main thread in [asyncCalls].");
}
//Asynchronous calls with jobs rely on results of some previous jobs
public void orderedCalls() {
service = Executors.newFixedThreadPool(4, r -> {
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
return t;
});
CallbackTask httpReqTask = new CallbackTask(httpReq,
arg -> System.out.println("Successfully read from URL: " + arg));
CallbackTask readFromDbTask = new CallbackTask(readFromDb).callback(arg -> {
System.out.println("Successfully read from DB. It`s time to send req with data we found");
executeAsync(service, httpReqTask.args(arg));
});
CallbackTask readFileTask = new CallbackTask(readFromFile).callback(arg -> {
System.out.println("Successfully read from file. Init connection with DB");
executeAsync(service, readFromDbTask.args(arg));
});
CallbackTask readConfigTask = new CallbackTask(readConfig).callback(arg -> {
executeAsync(service, readFileTask.args(arg));
service.shutdown();
}).args("Config file");
executeAsync(service, readConfigTask);
System.out.println("End of src.Main thread in [orderedCallsWithFutures].");
}
//wrap src.job to be executed asynchronously by ExecutorService
private Future<String> executeAsync(ExecutorService service, CallbackTask<String> func) {
return service.submit(func);
}
}