Skip to main content

Java

Github | Maven Repository

Getting Started With the Java SDK

Install the latest version

<dependency>
<groupId>cloud.prefab</groupId>
<artifactId>client</artifactId>
<version>0.3.9</version>
</dependency>

Initialize the client

final PrefabCloudClient prefabCloudClient = new PrefabCloudClient(new Options());

Options

Options options = new Options()
.setNamespace("billing-service.jobs.dunning-job")
.setConfigOverrideDir(System.getProperty("user.home"))
.setApikey(System.getenv("PREFAB_API_KEY"))
.setPrefabDatasource(Options.Datasources.ALL) // Option: Datasources.LOCAL_ONLY
.setOnInitializationFailure(Options.OnInitializationFailure.) // Option Options.OnInitializationFailure.UNLOCK
.setInitializationTimeoutSec(10);

Get FeatureFlag

FeatureFlagClient featureFlagClient = prefabCloudClient.featureFlagClient();

featureFlagClient.featureIsOn(
"features.example-flag",
PrefabContext.newBuilder("customer")
.put("group", "beta")
.build()
)

Get Config

final Optional<Prefab.ConfigValue> configValue = prefabCloudClient.configClient().get("the.key");
if(configValue.isPresent()){
System.out.println(configValue.get().getString());
}

Typical Usage

We recommend using the PrefabCloudClient as a singleton in your application. This is the most common way to use the SDK.

We have runnable example apps walking through installation, configuration and usage for these containers:

An example of usage in Spring Boot is on the way. Let us know about any others you'd like to see

// Micronaut Factory
@Factory
public class PrefabFactory {

@Singleton
public PrefabCloudClient prefabCloudClient() {
PrefabCloudClient.Options builder = new PrefabCloudClient.Options();
return new PrefabCloudClient(builder);
}

@Singleton
public FeatureFlagClient featureFlagClient(
PrefabCloudClient prefabCloudClient
) {
return prefabCloudClient.featureFlagClient();
}

@Singleton
public ConfigClient configClient(PrefabCloudClient prefabCloudClient) {
return prefabCloudClient.configClient();
}
}


public class MyClass {
@Inject
private ConfigClient configClient;

public String test(String key){
Optional<Prefab.ConfigValue> val = configClient.get(key);
return "Live value of %s is %s".formatted(key, val.orElse("no value found"));
}
}

Live Values

Live values are a convenient and clear way to use configuration throughout your system. Inject a prefab client and get live values for the configuration keys you need.

In code, .get() will return the value. These values will update automatically when the configuration is updated in Prefab Cloud.

Get a live value

# .prefab.config.default.yaml
sample:
long: 123
string: "hello"
import java.util.function.Supplier;

class MyClass {

private Supplier<String> sampleString;
private Supplier<Long> sampleLong;

@Inject
public MyClass(ConfigClient configClient) {
this.sampleString = configClient.liveString("sample.string");
this.sampleLong = configClient.liveLong("sample.long");
}

public String test(){
return "I got %s and %d from Prefab Cloud.".formatted(sampleString.get(), sampleLong.get());
}
}

Testing

Prefab suggests testing with generous usage of Mockito. We also provide a useful FixedValue for testing Live Values.

  @Test
void testPrefab(){
ConfigClient mockConfigClient = mock(ConfigClient.class);
when(mockConfigClient.liveString("sample.string")).thenReturn(FixedValue.of("test value"));
when(mockConfigClient.liveLong("sample.long")).thenReturn(FixedValue.of(123L));

MyClass myClass = new MyClass(mock(ConfigClient.class));

// test business logic

}