-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathStreamingExample.java
More file actions
82 lines (70 loc) · 2.58 KB
/
Copy pathStreamingExample.java
File metadata and controls
82 lines (70 loc) · 2.58 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
package io.getunleash.example;
import io.getunleash.DefaultUnleash;
import io.getunleash.Unleash;
import io.getunleash.UnleashException;
import io.getunleash.event.ClientFeaturesResponse;
import io.getunleash.event.UnleashSubscriber;
import io.getunleash.util.UnleashConfig;
public class StreamingExample {
private static Unleash unleash;
private static class StreamingEventSubscriber implements UnleashSubscriber {
@Override
public void togglesFetched(ClientFeaturesResponse toggleResponse) {
System.out.println("[STREAMING EVENT] Features updated");
if (unleash != null) {
boolean isEnabled = unleash.isEnabled("streaming_flag");
System.out.println(
" streaming_flag: " + (isEnabled ? "ENABLED" : "DISABLED")
);
System.out.println(
" test devices flag: " + unleash.isEnabled("devices")
);
}
}
@Override
public void onError(UnleashException unleashException) {
System.err.println(
"[STREAMING ERROR] " + unleashException.getMessage()
);
}
}
public static void main(String[] args) {
StreamingEventSubscriber subscriber = new StreamingEventSubscriber();
UnleashConfig config = UnleashConfig.builder()
.appName("streaming-example")
.instanceId("streaming-example-instance")
.unleashAPI(
getOrElse(
"UNLEASH_API_URL",
"https://app.unleash-hosted.com/demo/api"
)
)
.customHttpHeader(
"Authorization",
getOrElse(
"UNLEASH_API_TOKEN",
"*:development.25a06b75248528f8ca93ce179dcdd141aedfb632231e0d21fd8ff349"
)
)
.experimentalStreamingMode()
.subscriber(subscriber)
.build();
unleash = new DefaultUnleash(config);
System.out.println(
"Streaming client started. Waiting for events... (Press Ctrl+C to exit)"
);
try {
Thread.currentThread().join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Streaming example shutting down");
}
}
public static String getOrElse(String key, String defaultValue) {
String value = System.getenv(key);
if (value == null) {
return defaultValue;
}
return value;
}
}