Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/harness/ff-java-server-sdk

Java Server SDK for integrating with Harness Feature Flag service.
https://github.com/harness/ff-java-server-sdk

cd ci feature-flags java sdk

Last synced: 2 months ago
JSON representation

Java Server SDK for integrating with Harness Feature Flag service.

Awesome Lists containing this project

README

        

Java SDK For Harness Feature Flags
========================

## Table of Contents
**[Intro](#Intro)**

**[Requirements](#Requirements)**

**[Quickstart](#Quickstart)**

**[Further Reading](docs/further_reading.md)**

## 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](https://ngdocs.harness.io/article/i7et9ebkst-integrate-feature-flag-with-java-sdk).

For a sample FF Java SDK project, see our [test Java project](https://github.com/harness/ff-java-server-sdk/blob/main/examples/src/main/java/io/harness/ff/examples/GettingStarted.java).

![FeatureFlags](https://github.com/harness/ff-java-server-sdk/raw/main/docs/images/ff-gui.png)

## Requirements

To use this SDK, make sure you've:

- Installed [JDK 8](https://openjdk.java.net/install/) 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:
- [Created a Feature Flag](https://ngdocs.harness.io/article/1j7pdkqh7j-create-a-feature-flag) on the Harness Platform called harnessappdemodarkmode
- Created a [server SDK key](https://ngdocs.harness.io/article/1j7pdkqh7j-create-a-feature-flag#step_3_create_an_sdk_key) and made a copy of it

### General Dependencies

[Defined in the main project](./pom.xml)

### Logging Dependencies

Logback
```pom

ch.qos.logback
logback-classic
VERSION

```

Log4j
```pom

org.apache.logging.log4j
log4j-api
VERSION

org.apache.logging.log4j
log4j-core
VERSION

org.apache.logging.log4j
log4j-slf4j-impl
VERSION

```

### 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](https://mvnrepository.com/artifact/io.harness/ff-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:
```pom

io.harness
ff-java-server-sdk
1.8.0

```

#### Gradle

```
implementation 'io.harness:ff-java-server-sdk:1.8.0'
```

### 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.

```java
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

```bash
export FF_API_KEY=

./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.

```bash
# 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](docs/further_reading.md)

[GraalVM](examples-graalvm-native/README.md)

-------------------------
[Harness](https://www.harness.io/) 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](/examples/src/main/java/io/harness/ff/code_cleanup_examples/README.md)