Skip to content

Commit d8c0d94

Browse files
committed
chore(java-sample): Move logic out of onFeatureFlags
It's not necessary considering the first request to trigger local evaluation will synchronously retrieve flag definitions if they're not yet loaded.
1 parent 50d1aed commit d8c0d94

File tree

1 file changed

+34
-35
lines changed

1 file changed

+34
-35
lines changed

posthog-samples/posthog-java-sample/src/main/java/com/posthog/java/sample/PostHogJavaExample.java

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,20 @@
1212
* Simple Java 1.8 example demonstrating PostHog usage
1313
*/
1414
public class PostHogJavaExample {
15-
private static PostHogInterface postHog;
1615

1716
public static void main(String[] args) {
1817
PostHogConfig config = PostHogConfig
19-
.builder("phc_qYXiHw5odMiVWF7Dwh2sHWS7Hj6FsutBNp2SEaMqS0A")
20-
.personalApiKey("phx_example")
18+
.builder("phc_wxtaSxv9yC8UYxUAxNojluoAf41L8p6SJZmiTMtS8jA")
19+
.personalApiKey("phs_DuaFTmUtxQNj5R2W03emB1jMLIX5XwDvrt3DKfi5uYNcxzd")
2120
.host("http://localhost:8010")
2221
.localEvaluation(true)
2322
.debug(true)
24-
.onFeatureFlags(() -> {
25-
if (postHog.isFeatureEnabled("distinct-id", "beta-feature", false)) {
26-
System.out.println("The feature is enabled.");
27-
}
28-
29-
Object flagValue = postHog.getFeatureFlag("distinct-id", "multi-variate-flag", "default");
30-
String flagVariate = flagValue instanceof String ? (String) flagValue : "default";
31-
Object flagPayload = postHog.getFeatureFlagPayload("distinct-id", "multi-variate-flag");
32-
33-
System.out.println("The flag variant was: " + flagVariate);
34-
System.out.println("Received flag payload: " + flagPayload);
35-
36-
Boolean hasFilePreview = postHog.isFeatureEnabled(
37-
"distinct-id",
38-
"file-previews",
39-
PostHogFeatureFlagOptions
40-
.builder()
41-
.defaultValue(false)
42-
.personProperty("email", "[email protected]")
43-
.build());
44-
45-
System.out.println("File previews enabled: " + hasFilePreview);
46-
47-
postHog.flush();
48-
postHog.close();
49-
})
5023
.build();
5124

52-
postHog = PostHog.with(config);
25+
PostHogInterface posthog = PostHog.with(config);
5326

54-
postHog.group("distinct-id", "company", "some-company-id");
55-
postHog.capture(
27+
posthog.group("distinct-id", "company", "some-company-id");
28+
posthog.capture(
5629
"distinct-id",
5730
"new-purchase",
5831
PostHogCaptureOptions
@@ -63,14 +36,40 @@ public static void main(String[] args) {
6336

6437
HashMap<String, Object> userProperties = new HashMap<>();
6538
userProperties.put("email", "[email protected]");
66-
postHog.identify("distinct-id", userProperties);
39+
posthog.identify("distinct-id", userProperties);
6740

6841
// AVOID - Anonymous inner class holds reference to outer class.
6942
// The following won't serialize properly.
70-
// postHog.identify("user-123", new HashMap<String, Object>() {{
43+
// posthog.identify("user-123", new HashMap<String, Object>() {{
7144
// put("key", "value");
7245
// }});
7346

74-
postHog.alias("distinct-id", "alias-id");
47+
posthog.alias("distinct-id", "alias-id");
48+
49+
// Feature flag examples with local evaluation
50+
if (posthog.isFeatureEnabled("distinct-id", "beta-feature", false)) {
51+
System.out.println("The feature is enabled.");
52+
}
53+
54+
Object flagValue = posthog.getFeatureFlag("distinct-id", "multi-variate-flag", "default");
55+
String flagVariate = flagValue instanceof String ? (String) flagValue : "default";
56+
Object flagPayload = posthog.getFeatureFlagPayload("distinct-id", "multi-variate-flag");
57+
58+
System.out.println("The flag variant was: " + flagVariate);
59+
System.out.println("Received flag payload: " + flagPayload);
60+
61+
Boolean hasFilePreview = posthog.isFeatureEnabled(
62+
"distinct-id",
63+
"file-previews",
64+
PostHogFeatureFlagOptions
65+
.builder()
66+
.defaultValue(false)
67+
.personProperty("email", "[email protected]")
68+
.build());
69+
70+
System.out.println("File previews enabled: " + hasFilePreview);
71+
72+
posthog.flush();
73+
posthog.close();
7574
}
7675
}

0 commit comments

Comments
 (0)