Skip to content

davejohnston/ff-java-server-sdk

 
 

Repository files navigation

Java SDK For Harness Feature Flags

Table of Contents

Intro
Requirements
Quickstart
Further Reading

Intro

Use this README to get started with our Feature Flags (FF) SDK for Java. This guide outlines the basics of getting started with the SDK and provides a full code sample for you to try out. This sample doesn’t include configuration options, for in depth steps and configuring the SDK, for example, disabling streaming or using our Relay Proxy, see the Java SDK Reference.

For a sample FF Java SDK project, see our test Java project.

FeatureFlags

Requirements

To use this SDK, make sure you've:

  • Installed JDK 8 or a newer version
  • Installed Maven or Gradle or an alternative build automation tool for your application

To follow along with our test code sample, make sure you’ve:

General Dependencies

Defined in the main project

Logging Dependencies

Logback

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>VERSION</version>
</dependency>

Log4j

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>VERSION</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>VERSION</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>VERSION</version>
</dependency>

Install the SDK

The first step is to install the FF SDK as a dependency in your application using your application's dependency manager. You can use Maven, Gradle, SBT, etc. for your application.

Refer to the Harness Feature Flag Java Server SDK to identify the latest version for your build automation tool.

This section lists dependencies for Maven and Gradle:

Maven

Add the following Maven dependency in your project's pom.xml file:

<dependency>
    <groupId>io.harness</groupId>
    <artifactId>ff-java-server-sdk</artifactId>
    <version>1.5.2</version>
</dependency>

Gradle

implementation 'io.harness:ff-java-server-sdk:1.5.2'

Code Sample

Here is a complete example that will connect to the feature flag service and report the flag value every 10 seconds until the connection is closed. Any time a flag is toggled from the feature flag service you will receive the updated value.

After installing the SDK, enter the SDK keys that you created for your environment. The SDK keys authorize your application to connect to the FF client.

package io.harness.ff.examples;

import io.harness.cf.client.api.*;
import io.harness.cf.client.dto.Target;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class GettingStarted {
    // API Key - set this as an env variable
    private static String apiKey = getEnvOrDefault("FF_API_KEY", "");

    // Flag Identifier
    private static String flagName = getEnvOrDefault("FF_FLAG_NAME", "harnessappdemodarkmode");

    private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

    public static void main(String[] args) {
        System.out.println("Harness SDK Getting Started");

        // Create a Feature Flag Client
        // try-with-resources is used here to automatically close the client when this block is exited
        try (CfClient cfClient = new CfClient(apiKey)) {
            cfClient.waitForInitialization();

            // Create a target (different targets can get different results based on rules.  This includes a custom attribute 'location')
            final Target target = Target.builder()
                    .identifier("javasdk")
                    .name("JavaSDK")
                    .attribute("location", "emea")
                    .build();

            // Loop forever reporting the state of the flag
            scheduler.scheduleAtFixedRate(
                    () -> {
                        boolean result = cfClient.boolVariation(flagName, target, false);
                        System.out.println("Boolean variation is " + result);
                    },
                    0,
                    10,
                    TimeUnit.SECONDS);

            // SDK will exit after 15 minutes, this gives the example time to stream events
            TimeUnit.MINUTES.sleep(15);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Get the value from the environment or return the default
    private static String getEnvOrDefault(String key, String defaultValue) {
        String value = System.getenv(key);
        if (value == null || value.isEmpty()) {
            return defaultValue;
        }
        return value;
    }
}

Running the example

export FF_API_KEY=<your key here>

./gradlew clean build
./gradlew examples:GettingStarted --console=plain

Running the example with Docker

If you don't have the right version of java installed locally, or don't want to install the dependencies you can use docker to get started.

# Run the Example
docker run -e FF_API_KEY=$FF_API_KEY -v $(PWD):/app -v "$HOME/.m2":/root/.m2 -w /app gradle:8.5-jdk11 gradle clean build -xtest examples:GettingStarted --console=plain

Additional Reading

Further examples and config options are in the further reading section:

Further Reading

GraalVM


Harness is a feature management platform that helps teams to build better software and to test features quicker.


Code Cleanup (Beta)

The java sdk supports automated code cleanup. For more info see the docs

About

Java Server SDK for integrating with Harness Feature Flag service.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 93.7%
  • Mustache 6.3%