1 package examples.example1;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.concurrent.ExecutionException;
6 import java.util.concurrent.ExecutorService;
7 import java.util.concurrent.Executors;
8 import java.util.concurrent.Future;
9 import java.util.concurrent.TimeUnit;
10 import java.util.concurrent.atomic.AtomicInteger;
11
12 import examples.example1.classes.MyCallerWithCircuitBreaker;
13
14
15 public class SimulationWithCircuitBreaker {
16 public static void main(String[] args) throws ExecutionException, InterruptedException {
17 MyCallerWithCircuitBreaker myCaller = new MyCallerWithCircuitBreaker();
18
19
20
21 ExecutorService executorService = Executors.newWorkStealingPool(10);
22 List<Future<Long>> responses = new ArrayList<>();
23
24
25 long start = System.currentTimeMillis();
26
27 for (int i = 0; i < 100; i++) {
28 AtomicInteger callId = new AtomicInteger(i);
29
30 Future<Long> response = executorService.submit(() -> myCaller.runService(callId.get()));
31 responses.add(response);
32 }
33
34 ArrayList<Long> responseValues = new ArrayList<>(responses.size());
35 for (int i = 0; i < responses.size(); i++) {
36 Long response = responses.get(i).get();
37 responseValues.add(response);
38 }
39
40 for (int i = 0; i < responseValues.size(); i++) {
41 System.out.println("Response for call #:" + i + " is: " + responseValues.get(i));
42 }
43
44
45 executorService.shutdown();
46 executorService.awaitTermination(2, TimeUnit.MINUTES);
47
48 long stop = System.currentTimeMillis();
49 System.out.printf("Finished in %d seconds", (int) (stop - start) / 1000);
50 }
51
52 }