Git Product home page Git Product logo

ff-java-server-sdk's Introduction

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:

  • InstalledJDK 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 and uses the 1.1.6 version as an example:

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.1.6</version>
</dependency>

Gradle

implementation group: 'io.harness', name: 'ff-java-server-sdk', version: '1.1.6'

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");

        try {
            //Create a Feature Flag Client
            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);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // Close the SDK
            CfClient.getInstance().close();
        }
    }

    // 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>
cd examples

mvn clean package
mvn exec:java -Dexec.mainClass="io.harness.ff.examples.GettingStarted"

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.

# Clean and Package
docker run -v $(PWD)/examples:/app -v "$HOME/.m2":/root/.m2 -w /app maven:3.3-jdk-8 mvn clean package

# Run the Example
docker run -e FF_API_KEY=$FF_API_KEY -v $(PWD)/examples:/app -v "$HOME/.m2":/root/.m2 -w /app maven:3.3-jdk-8 mvn exec:java -Dexec.mainClass="io.harness.ff.examples.GettingStarted"

Additional Reading

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

Further Reading


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


ff-java-server-sdk's People

Contributors

enver-bisevac avatar rushabh-harness avatar milos85vasic avatar davejohnston avatar nam054 avatar andybharness avatar hannah-tang avatar bmjen avatar mosheeshel avatar puneetsar avatar subiradhikari avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.