SDK guide

Feature flags in Java and Kotlin with OpenFeature

How to add feature flags to a JVM service: add the dependency, evaluate flags in process without a network call, use them from Spring Boot, and shut down cleanly.

Railflag's Java and Kotlin provider evaluates feature flags inside your JVM in a few microseconds, with no network call per check. It implements the OpenFeature Java SDK, so your code depends on the vendor-neutral API.

Install

The provider needs Java 11 or newer, and its only dependency is the OpenFeature Java SDK.

// build.gradle.kts
implementation("com.railflag:railflag-openfeature:0.2.0")
<!-- pom.xml -->
<dependency>
  <groupId>com.railflag</groupId>
  <artifactId>railflag-openfeature</artifactId>
  <version>0.2.0</version>
</dependency>

Connect and evaluate in Java

Create a server key for the environment in the dashboard and register the provider once at startup:

OpenFeatureAPI api = OpenFeatureAPI.getInstance();
api.setProviderAndWait(new RailflagProvider(RailflagOptions.builder()
    .sdkKey(System.getenv("RAILFLAG_SERVER_KEY"))
    .baseUrl("https://relay.railflag.com")
    .build()));
Client client = api.getClient();

boolean on = client.getBooleanValue("new-checkout", false,
    new ImmutableContext("user-42", Map.of(
        "plan", new Value("pro"),
        "country", new Value("GR"))));

The same in Kotlin

val api = OpenFeatureAPI.getInstance()
api.setProviderAndWait(RailflagProvider(RailflagOptions.builder()
    .sdkKey(System.getenv("RAILFLAG_SERVER_KEY"))
    .baseUrl("https://relay.railflag.com")
    .build()))
val client = api.client

val on = client.getBooleanValue("new-checkout", false,
    ImmutableContext("user-42", mapOf("plan" to Value("pro"), "country" to Value("GR"))))

The provider downloads the flag config once, follows changes over Server-Sent Events and applies them within a second. If the stream drops it polls every 30 seconds, and if Railflag cannot be reached it keeps serving the last config. Call api.shutdown() on exit to close the stream and send the last experiment events.

Value types

getBooleanValue, getStringValue, getIntegerValue, getLongValue, getDoubleValue and getObjectValue, and their …Details variants, work as in any OpenFeature provider. An unknown flag or a wrong type returns your default with an error code. Context values can be strings, booleans, numbers, Instants (compared as instants by before and after rules), lists and structures. The details object also carries the variation index and metadata such as the flag version and the id of the rule that matched.

Using it in Spring Boot

Register the provider once as a bean or in a startup hook, and build the evaluation context per request from the signed-in user. The client is thread-safe, so one instance can be injected everywhere:

@Configuration
class FlagsConfig {
    @Bean
    Client flagClient() {
        OpenFeatureAPI api = OpenFeatureAPI.getInstance();
        api.setProviderAndWait(new RailflagProvider(RailflagOptions.builder()
            .sdkKey(System.getenv("RAILFLAG_SERVER_KEY"))
            .baseUrl("https://relay.railflag.com")
            .build()));
        return api.getClient();
    }
}

The builder also takes stream(false) for poll-only operation, pollInterval, timeout, and your own HttpClient. This provider is for servers, not for Android apps, because a server key can read every rule.

The same answer in every language

Evaluator.java is a port of Railflag's TypeScript evaluator, and its tests replay the shared vectors, so a user lands in the same variation whichever SDK evaluates the flag. The known differences are small: regexes in matches rules run on java.util.regex, which reads a few constructs differently from JavaScript, and a date-time without a time zone is read as UTC.

Keys, privacy and experiments

  • Once a minute the provider sends counts of how often each flag returned each variation, for Release Watch. It never sends contexts or user ids.
  • For experiments, client.track("purchase", ctx, new MutableTrackingEventDetails(49.9)) records conversions. Only events a running experiment counts are sent, and user keys are hashed before storage. sendEvents(false) turns it all off.
  • Server-side checks are never metered.

Frequently asked questions

How do I use feature flags in Java?

Add com.railflag:railflag-openfeature, register a RailflagProvider with your server key through OpenFeatureAPI.setProviderAndWait, and call client.getBooleanValue(key, default, context). The provider keeps the rules in memory, so checks take a few microseconds and make no network call.

Does the Railflag provider work with Kotlin?

Yes. It is a JVM library, so Kotlin calls the same OpenFeature client. The builder and the client work the same way from Kotlin, with Kotlin collections for the context.

Which Java version is required?

Java 11 or newer. The only dependency is the OpenFeature Java SDK.

Can I use it in an Android app?

No. The provider uses a server key that can read every rule, so it is meant for servers. Browsers use the JavaScript SDK with a public client key.

Feature flags for your JVM services

Local evaluation, live updates within a second, unlimited server-side checks on every plan.

Start for free