-
Notifications
You must be signed in to change notification settings - Fork 958
Expand file tree
/
Copy pathDemoApp.java
More file actions
110 lines (101 loc) · 3.65 KB
/
Copy pathDemoApp.java
File metadata and controls
110 lines (101 loc) · 3.65 KB
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
/*
* Copyright (c) 2026, Jaroslav Bachorik <j.bachorik@btrace.io>.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
/**
* Demo application for the BTrace hands-on tutorials (docs/tutorials/).
*
* <p>Simulates a small order-processing service with two deliberate defects for you to find with
* BTrace:
*
* <ul>
* <li>an intermittent latency problem (a slow payment provider), and
* <li>an intermittent validation failure (an exception).
* </ul>
*
* <p>Run it with a single command (JDK 11+): {@code java DemoApp.java}
*
* <p>No dependencies, no build. It keeps processing orders until you stop it with Ctrl+C.
*/
public class DemoApp {
public static void main(String[] args) throws Exception {
OrderService service = new OrderService();
AtomicLong processed = new AtomicLong();
AtomicLong failed = new AtomicLong();
for (int i = 0; i < 3; i++) {
Thread worker =
new Thread(
() -> {
Random rnd = new Random();
while (true) {
String orderId = "order-" + rnd.nextInt(100_000);
try {
service.processOrder(orderId);
processed.incrementAndGet();
} catch (IllegalStateException e) {
failed.incrementAndGet();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
try {
Thread.sleep(50 + rnd.nextInt(150));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
},
"order-worker-" + i);
worker.setDaemon(true);
worker.start();
}
System.out.println("[demo] order service running - stop with Ctrl+C");
while (true) {
Thread.sleep(5_000);
System.out.printf(
"[demo] processed %d orders, %d failed%n", processed.get(), failed.get());
}
}
}
/** The service under observation. Not public on purpose: its binary name is just "OrderService". */
class OrderService {
private final Random rnd = new Random();
void processOrder(String orderId) throws InterruptedException {
validateOrder(orderId);
lookupInventory(orderId);
chargeCard(orderId);
}
/** Fails for roughly 8% of orders - find it with an @error probe. */
void validateOrder(String orderId) throws InterruptedException {
if (rnd.nextInt(100) < 8) {
throw new IllegalStateException("order failed validation: " + orderId);
}
Thread.sleep(2 + rnd.nextInt(8));
}
void lookupInventory(String orderId) throws InterruptedException {
Thread.sleep(5 + rnd.nextInt(35));
}
/** Usually fast, but roughly 10% of calls hit a "slow payment provider" - find it with BTrace. */
void chargeCard(String orderId) throws InterruptedException {
if (rnd.nextInt(100) < 10) {
Thread.sleep(250 + rnd.nextInt(150));
} else {
Thread.sleep(10 + rnd.nextInt(40));
}
}
}