Java SDK
GoGreen feature flag and experimentation SDK for Java. Evaluate flags, stream real-time updates, and track events with a simple API. Supports OpenFeature provider.
Requirements
- Java 11 or later
- Gradle 8.x (or use the included wrapper:
./gradlew)
Installation
Maven
<dependency>
<groupId>com.gogreen</groupId>
<artifactId>gogreen-sdk</artifactId>
<version>1.0.0</version>
</dependency>Gradle
repositories {
mavenCentral()
}
dependencies {
implementation 'com.gogreen:gogreen-sdk:1.0.0'
}Building from source
cd sdk/java
./gradlew build
./gradlew publishToMavenLocalThen in your project:
repositories {
mavenLocal()
}
dependencies {
implementation 'com.gogreen:gogreen-sdk:1.0.0'
}Quick start
import com.gogreen.sdk.*;
// Build config (builder pattern)
GoGreenConfig config = GoGreenConfig.builder()
.sdkKey("your-sdk-key")
.envId("your-environment-id")
.apiBaseUrl("https://app.gogreenflags.com")
.defaultContext(EvaluationContext.builder("user-123").build())
.build();
GoGreenClient client = new GoGreenClient(config);
// Wait for initial config (e.g. up to 10 seconds)
client.waitForInitialization(10, TimeUnit.SECONDS);
// Evaluate flags
boolean showNewUI = client.boolVariation("new-ui", false);
String plan = client.stringVariation("plan", "free");
double discount = client.numberVariation("discount", 0.0);
// With context override
EvaluationContext ctx = EvaluationContext.builder("other-user")
.putCustom("region", "eu")
.build();
boolean flag = client.boolVariationDetail("feature-x", false, ctx);
// Track events for experimentation
client.track("purchase", Map.of("amount", 99.99));
client.close();Configuration
| Option | Description | Default |
|---|---|---|
sdkKey | SDK key (required) | — |
envId | Environment ID (required) | — |
apiBaseUrl | Evaluation API base URL | http://localhost:8092 |
streamUrl | SSE stream URL | same as apiBaseUrl |
eventsUrl | Events ingestion URL | same as apiBaseUrl |
enableStreaming | Real-time SSE updates | true |
offline | Offline mode (bootstrap only) | false |
requestTimeoutMs | HTTP timeout (ms) | 10000 |
flushIntervalMs | Event flush interval (ms) | 30000 |
defaultContext | Default evaluation context (required) | — |
bootstrap | Bootstrap flags for offline | null |
OpenFeature provider
import com.gogreen.sdk.*;
import com.gogreen.sdk.provider.*;
import dev.openfeature.sdk.*;
GoGreenClient client = new GoGreenClient(config);
GoGreenProvider provider = new GoGreenProvider(client);
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
api.setProvider(provider);
Client ofClient = api.getClient();
boolean value = ofClient.getBooleanValue("my-flag", false);Testing
Use GoGreenClient.testClient(Map.of("flag-key", value)) for unit tests with no network:
GoGreenClient client = GoGreenClient.testClient(Map.of(
"feature-x", true,
"ui-theme", "dark",
"rate-limit", 50
));
boolean enabled = client.boolVariation("feature-x", false); // returns trueAPI reference
- Variation methods:
boolVariation,stringVariation,numberVariation,jsonVariation— return the flag value or default. - Detail methods:
boolVariationDetail,stringVariationDetail, etc. — returnEvaluationDetail<T>with value, variationId, reason, ruleIndex, prerequisiteKey. - Context:
setContext,identify— set evaluation context (user key and custom attributes). - Events:
track(eventKey, properties)— send custom events for experimentation. - Lifecycle:
waitForInitialization,initialized,enterOfflineMode,exitOfflineMode,close.