Skip to content

Commit 94d9f37

Browse files
Update the Hello Java application based on the new specification. (#22)
The hello app should run continuously now, printing an ASCII logo when the flag is true, and supporting the CI mode. Also updated the code to take the SDK Key via the environment variable, which we prefer for CI reasons.
2 parents 00eb948 + a2df6d7 commit 94d9f37

File tree

6 files changed

+97
-88
lines changed

6 files changed

+97
-88
lines changed

.circleci/config.yml

Lines changed: 0 additions & 65 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Build and run
2+
on:
3+
schedule:
4+
# * is a special character in YAML so you have to quote this string
5+
- cron: '0 9 * * *'
6+
push:
7+
branches: [ main, 'feat/**' ]
8+
paths-ignore:
9+
- '**.md' # Do not need to run CI for markdown changes.
10+
pull_request:
11+
branches: [ main, 'feat/**' ]
12+
paths-ignore:
13+
- '**.md'
14+
15+
jobs:
16+
build-and-run:
17+
runs-on: ubuntu-latest
18+
19+
permissions:
20+
id-token: write # Needed if using OIDC to get release secrets.
21+
22+
steps:
23+
- uses: actions/checkout@v4
24+
25+
- name: Set up Java
26+
uses: actions/setup-java@v4
27+
with:
28+
distribution: 'temurin'
29+
java-version: '21'
30+
31+
- uses: launchdarkly/gh-actions/actions/verify-hello-app@verify-hello-app-v1.0.1
32+
with:
33+
use_server_key: true
34+
role_arn: ${{ vars.AWS_ROLE_ARN }}
35+
command: ./gradlew run

CODEOWNERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Repository Maintainers
2+
* @launchdarkly/team-sdk-java

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
# LaunchDarkly Sample Java Application
1+
# LaunchDarkly Sample Java Application
22

33
We've built a simple console application that demonstrates how LaunchDarkly's SDK works.
44

5-
Below, you'll find the basic build procedure, but for more comprehensive instructions, you can visit your [Quickstart page](https://app.launchdarkly.com/quickstart#/) or the [Java SDK reference guide](https://docs.launchdarkly.com/sdk/server-side/java).
5+
Below, you'll find the basic build procedure, but for more comprehensive instructions, you can visit your [Quickstart page](https://app.launchdarkly.com/quickstart#/) or the [Java SDK reference guide](https://docs.launchdarkly.com/sdk/server-side/java).
66

7-
## Build instructions
7+
## Build instructions
88

9-
This project uses [Gradle](https://gradle.org/). It requires that Java is already installed on your system (version 8 or higher). It will automatically use the latest release of the LaunchDarkly SDK with major version 6.
9+
This project uses [Gradle](https://gradle.org/). It requires that Java is already installed on your system (version 8 or higher). It will automatically use the latest release of the LaunchDarkly SDK with major version 7.
1010

11-
1. Edit `src/main/java/Hello.java` and set the value of `SDK_KEY` to your LaunchDarkly SDK key. If there is an existing boolean feature flag in your LaunchDarkly project that you want to evaluate, set `FEATURE_FLAG_KEY` to the flag key.
11+
1. Set the value of environment variable `LAUNCHDARKLY_SERVER_KEY` to your LaunchDarkly SDK key. If there is an existing boolean feature flag in your LaunchDarkly project that you want to evaluate, edit `src/main/java/Hello.java` and set `FEATURE_FLAG_KEY` in code to the flag key.
1212

13-
```java
14-
static final String SDK_KEY = "1234567890abcdef";
13+
```sh
14+
export LAUNCHDARKLY_SERVER_KEY=1234567890abcdef
15+
```
1516

17+
```java
1618
static final String FEATURE_FLAG_KEY = "my-flag";
1719
```
1820

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

src/main/java/Hello.java

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,56 @@
66
public class Hello {
77

88
// Set SDK_KEY to your LaunchDarkly SDK key.
9-
static final String SDK_KEY = "";
9+
static String SDK_KEY = "";
1010

1111
// Set FEATURE_FLAG_KEY to the feature flag key you want to evaluate.
12-
static final String FEATURE_FLAG_KEY = "my-boolean-flag";
12+
static String FEATURE_FLAG_KEY = "sample-feature";
1313

1414
private static void showMessage(String s) {
1515
System.out.println("*** " + s);
1616
System.out.println();
1717
}
1818

19+
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");
29+
}
30+
1931
public static void main(String... args) throws Exception {
32+
// Set this environment variable to skip the loop process and evaluate the flag
33+
// a single time.
34+
boolean CIMode = System.getenv("CI") != null;
35+
36+
// Get SDK Key from env variable LAUNCHDARKLY_SERVER_KEY
37+
String envSDKKey = System.getenv("LAUNCHDARKLY_SERVER_KEY");
38+
if(envSDKKey != null) {
39+
SDK_KEY = envSDKKey;
40+
}
41+
42+
String envFlagKey = System.getenv("LAUNCHDARKLY_FLAG_KEY");
43+
if(envFlagKey != null) {
44+
FEATURE_FLAG_KEY = envFlagKey;
45+
}
46+
2047
LDConfig config = new LDConfig.Builder().build();
2148

49+
if (SDK_KEY == null || SDK_KEY.equals("")) {
50+
showMessage("Please set the LAUNCHDARKLY_SERVER_KEY environment variable or edit Hello.java to set SDK_KEY to your LaunchDarkly SDK key first.");
51+
System.exit(1);
52+
}
53+
2254
final LDClient client = new LDClient(SDK_KEY, config);
2355
if (client.isInitialized()) {
2456
showMessage("SDK successfully initialized!");
2557
} else {
26-
showMessage("SDK failed to initialize");
58+
showMessage("SDK failed to initialize. Please check your internet connection and SDK credential for any typo.");
2759
System.exit(1);
2860
}
2961

@@ -37,22 +69,25 @@ public static void main(String... args) throws Exception {
3769
boolean flagValue = client.boolVariation(FEATURE_FLAG_KEY, context, false);
3870
showMessage("Feature flag '" + FEATURE_FLAG_KEY + "' is " + flagValue + " for this context");
3971

40-
// Here we request that the SDK flush pending analytic events so that you see
41-
// data for the above evaluation on the dashboard immediatelynow rather than
42-
// at the next automatic flush interval. You don't need to do this under normal
43-
// circumstances in your own application.
44-
client.flush();
72+
if (flagValue) {
73+
showBanner();
74+
}
75+
76+
//If this is building for CI, we don't need to keep running the Hello App continously.
77+
if(CIMode) {
78+
System.exit(0);
79+
}
4580

4681
// We set up a flag change listener so you can see flag changes as you change
4782
// the flag rules.
48-
client.getFlagTracker().addFlagChangeListener(event -> {
49-
showMessage("Feature flag '" + event.getKey() + "' has changed.");
50-
if (event.getKey().equals(FEATURE_FLAG_KEY)) {
51-
boolean value = client.boolVariation(FEATURE_FLAG_KEY, context, false);
52-
showMessage("Feature flag '" + FEATURE_FLAG_KEY + "' is " + value + " for this context");
53-
}
83+
client.getFlagTracker().addFlagValueChangeListener(FEATURE_FLAG_KEY, context, event -> {
84+
showMessage("Feature flag '" + FEATURE_FLAG_KEY + "' is " + event.getNewValue() + " for this context");
85+
86+
if (event.getNewValue().booleanValue()) {
87+
showBanner();
88+
}
5489
});
55-
showMessage("Listening for feature flag changes. Use Ctrl+C to terminate.");
90+
showMessage("Listening for feature flag changes.");
5691

5792
// Here we ensure that when the application terminates, the SDK shuts down
5893
// cleanly and has a chance to deliver analytics events to LaunchDarkly.

0 commit comments

Comments
 (0)