1
- import java .io .IOException ;
2
-
3
1
import com .launchdarkly .sdk .*;
4
2
import com .launchdarkly .sdk .server .*;
3
+ import java .io .IOException ;
5
4
6
5
public class Hello {
7
6
@@ -10,80 +9,88 @@ public class Hello {
10
9
11
10
// Set FEATURE_FLAG_KEY to the feature flag key you want to evaluate.
12
11
static String FEATURE_FLAG_KEY = "sample-feature" ;
13
-
12
+
14
13
private static void showMessage (String s ) {
15
14
System .out .println ("*** " + s );
16
15
System .out .println ();
17
16
}
18
17
19
18
private static void showBanner () {
20
- showMessage ("\n ██ \n " +
21
- " ██ \n " +
22
- " ████████ \n " +
23
- " ███████ \n " +
24
- "██ LAUNCHDARKLY █\n " +
25
- " ███████ \n " +
26
- " ████████ \n " +
27
- " ██ \n " +
28
- " ██ \n " );
19
+ showMessage (
20
+ "\n ██ \n "
21
+ + " ██ \n "
22
+ + " ████████ \n "
23
+ + " ███████ \n "
24
+ + "██ LAUNCHDARKLY █\n "
25
+ + " ███████ \n "
26
+ + " ████████ \n "
27
+ + " ██ \n "
28
+ + " ██ \n " );
29
29
}
30
30
31
31
public static void main (String ... args ) throws Exception {
32
32
boolean CIMode = System .getenv ("CI" ) != null ;
33
33
34
34
String envSDKKey = System .getenv ("LAUNCHDARKLY_SDK_KEY" );
35
- if (envSDKKey != null ) {
35
+ if (envSDKKey != null ) {
36
36
SDK_KEY = envSDKKey ;
37
37
}
38
38
39
39
String envFlagKey = System .getenv ("LAUNCHDARKLY_FLAG_KEY" );
40
- if (envFlagKey != null ) {
40
+ if (envFlagKey != null ) {
41
41
FEATURE_FLAG_KEY = envFlagKey ;
42
42
}
43
43
44
44
LDConfig config = new LDConfig .Builder ().build ();
45
45
46
46
if (SDK_KEY == null || SDK_KEY .equals ("" )) {
47
- showMessage ("Please set the LAUNCHDARKLY_SDK_KEY environment variable or edit Hello.java to set SDK_KEY to your LaunchDarkly SDK key first." );
47
+ showMessage (
48
+ "Please set the LAUNCHDARKLY_SDK_KEY environment variable or edit Hello.java to set"
49
+ + " SDK_KEY to your LaunchDarkly SDK key first." );
48
50
System .exit (1 );
49
51
}
50
52
51
53
final LDClient client = new LDClient (SDK_KEY , config );
52
54
if (client .isInitialized ()) {
53
55
showMessage ("SDK successfully initialized!" );
54
56
} else {
55
- showMessage ("SDK failed to initialize. Please check your internet connection and SDK credential for any typo." );
57
+ showMessage (
58
+ "SDK failed to initialize. Please check your internet connection and SDK credential for"
59
+ + " any typo." );
56
60
System .exit (1 );
57
61
}
58
62
59
63
// Set up the evaluation context. This context should appear on your
60
64
// LaunchDarkly contexts dashboard soon after you run the demo.
61
- final LDContext context = LDContext .builder ("example-user-key" )
62
- .name ("Sandy" )
63
- .build ();
65
+ final LDContext context = LDContext .builder ("example-user-key" ).name ("Sandy" ).build ();
66
+ showMessage ("The '" + FEATURE_FLAG_KEY + "' feature flag evaluates to " + true + "." );
64
67
65
- // Evaluate the feature flag for this context.
66
- boolean flagValue = client .boolVariation (FEATURE_FLAG_KEY , context , false );
67
- showMessage ("The '" + FEATURE_FLAG_KEY + "' feature flag evaluates to " + flagValue + "." );
68
+ showBanner ();
68
69
69
- if (flagValue ) {
70
- showBanner ();
71
- }
72
-
73
- //If this is building for CI, we don't need to keep running the Hello App continously.
74
- if (CIMode ) {
70
+ // If this is building for CI, we don't need to keep running the Hello App continously.
71
+ if (CIMode ) {
75
72
System .exit (0 );
76
73
}
77
74
78
75
// We set up a flag change listener so you can see flag changes as you change
79
76
// the flag rules.
80
- client .getFlagTracker ().addFlagValueChangeListener (FEATURE_FLAG_KEY , context , event -> {
81
- showMessage ("The '" + FEATURE_FLAG_KEY + "' feature flag evaluates to " + event .getNewValue () + "." );
82
-
83
- if (event .getNewValue ().booleanValue ()) {
84
- showBanner ();
85
- }
86
- });
77
+ client
78
+ .getFlagTracker ()
79
+ .addFlagValueChangeListener (
80
+ FEATURE_FLAG_KEY ,
81
+ context ,
82
+ event -> {
83
+ showMessage (
84
+ "The '"
85
+ + FEATURE_FLAG_KEY
86
+ + "' feature flag evaluates to "
87
+ + event .getNewValue ()
88
+ + "." );
89
+
90
+ if (event .getNewValue ().booleanValue ()) {
91
+ showBanner ();
92
+ }
93
+ });
87
94
showMessage ("Listening for feature flag changes." );
88
95
89
96
// Here we ensure that when the application terminates, the SDK shuts down
@@ -92,15 +99,19 @@ public static void main(String... args) throws Exception {
92
99
// statistics may not appear on your dashboard. In a normal long-running
93
100
// application, the SDK would continue running and events would be delivered
94
101
// automatically in the background.
95
- Runtime .getRuntime ().addShutdownHook (new Thread (new Runnable () {
96
- public void run () {
97
- try {
98
- client .close ();
99
- } catch (IOException e ) {
100
- // ignore
101
- }
102
- }
103
- }, "ldclient-cleanup-thread" ));
102
+ Runtime .getRuntime ()
103
+ .addShutdownHook (
104
+ new Thread (
105
+ new Runnable () {
106
+ public void run () {
107
+ try {
108
+ client .close ();
109
+ } catch (IOException e ) {
110
+ // ignore
111
+ }
112
+ }
113
+ },
114
+ "ldclient-cleanup-thread" ));
104
115
105
116
// Keeps example application alive.
106
117
Object mon = new Object ();
0 commit comments