Java Server SDK Usage
DevCycleUser Object
The user object is required for all methods. The only required field in the user object is userId.
See the DevCycleUser class in Java DevCycleUser model doc for all accepted fields.
import com.devcycle.sdk.server.common.model.DevCycleUser;
DevCycleUser user = DevCycleUser.builder()
.userId("a_user_id")
.country("US")
.build();
Get and use Variable by key
This method will fetch a specific variable value by key for a given user. The default value will be used in cases where the user is not segmented into a feature using that variable, or the project configuration is unavailable to be fetched from DevCycle's CDN.
Boolean variableValue = client.variableValue(user, "super_cool_feature", true);
if (variableValue.booleanValue()) {
// New Feature code here
} else {
// Old code here
}
The default value can be of type String
, Boolean
, Number
, or Object
.
If you would like to get the full Variable Object you can use variable()
instead. This contains fields such as:
key
, value
, type
, defaultValue
, isDefaulted
.
Getting All Variables
This method will fetch all variables for a given user and return as Map<String, Variable>. If the project configuration is unavailable, this will return an empty map.
To get values from your Variables, the value
field inside the variable object can be accessed.
import com.devcycle.sdk.server.common.model.BaseVariable;
Map<String, BaseVariable> variables = client.allVariables(user);
This method is intended to be used for debugging and analytics purposes, not as a method for retrieving the value of Variables to change code behaviour. For that purpose, we strongly recommend using the individual variable access method described in Get and use Variable by key Using this method instead will result in no evaluation events being tracked for individual variables, and will not allow the use of other DevCycle features such as Code Usage detection
Getting All Features
This method will fetch all features for a given user and return them as Map<String, Feature>. If the project configuration is unavailable, this will return an empty map.
Map<String, Feature> features = client.allFeatures(user);
Track Event
To POST custom event for a user, pass in the user and event object.
DevCycleEvent event = DevCycleEvent.builder()
.date(Instant.now().toEpochMilli())
.target("test target")
.type("test event")
.value(new BigDecimal(600))
.build();
client.track(user, event);
Set Client Custom Data
To assist with segmentation and bucketing you can set a custom data map that will be used for all variable and feature evaluations. User specific custom data will override client custom data.
// create a map of custom data
Map<String,Object> customData = new HashMap();
customData.put("some-key", "some-value");
// set the map into the DevCycle client
client.setClientCustomData(customData);
Override Logging
The SDK logs to stdout by default and does not require any specific logging package. To integrate with your own logging system, such as Java Logging or SLF4J, you can create a wrapper that implements the IDevCycleLogger interface. Then you can set the logger into the Java Server SDK setting the Custom Logger property in the options object used to initialize the client.
IDevCycleLogger loggingWrapper = new IDevCycleLogger() {
@Override
public void debug(String message) {
// Your logging implementation here
}
@Override
public void info(String message) {
// Your logging implementation here
}
@Override
public void warning(String message) {
// Your logging implementation here
}
@Override
public void error(String message) {
// Your logging implementation here
}
@Override
public void error(String message, Throwable throwable) {
// Your logging implementation here
}
};
// Set the logger in the options before creating the DevCycleLocalClient
DevCycleLocalOptions options = DevCycleLocalOptions.builder().customLogger(loggingWrapper).build();
EdgeDB
NOTE: EdgeDB is only available with Cloud Bucketing.
EdgeDB allows you to save user data to our EdgeDB storage so that you don't have to pass in all the user data every time you identify a user. Read more about EdgeDB.
To get started, contact us at support@devcycle.com to enable EdgeDB for your project.
Once you have EdgeDB enabled in your project, pass in the enableEdgeDB option to turn on EdgeDB mode for the SDK:
import com.devcycle.sdk.server.cloud.api.DevCycleCloudClient;
import com.devcycle.sdk.server.cloud.model.DevCycleCloudOptions;
import com.devcycle.sdk.server.common.model.DevCycleUser;
import com.devcycle.sdk.server.common.model.Variable;
DevCycleUser user = DevCycleUser.builder()
.userId("test_user")
.country("US")
.email("example@example.com")
.build();
DevCycleUser onlyUserId = DevCycleUser.builder()
.userId("test_user");
DevCycleCloudOptions devcycleOptions = DevCycleCloudOptions.builder()
.enableEdgeDB(true)
.build();
private DevCycleCloudClient devcycleClient;
public MyClass() {
devcycleClient = new DevCycleCloudClient("<DEVCYCLE_SERVER_SDK_KEY>", devcycleOptions);
Boolean testBoolean = devcycleClient.variableValue(user, "test-boolean-variable", false);
String onlyCountry = devcycleClient.variableValue(onlyUserId, "test-string-country-variable", "Not Available");
}
This will send a request to our EdgeDB API to save the custom data under the user test_user
.
In the example, Email and Country are associated to the user test_user
.
In your next identify call for the same userId
, you may omit any of the data you've sent already as it will be pulled
from the EdgeDB storage when segmenting to experiments and features.
Enabling Beta Realtime Updates
This feature is in beta, and may not function as expected. Please ensure that you have the latest version of the SDK.
This functionality will reduce the number of polling requests that are made to the DevCycle Config CDN, and instead will use a long-lived HTTP connection (Server Sent Events) to receive updates when there is a new config available. This reduces outbound network traffic, as well as optimizes the SDK for efficiency.
To enable Beta Realtime Updates, pass in the enableBetaRealtimeUpdates
option to the SDK initialization:
import com.devcycle.sdk.server.local.api.DevCycleLocalClient;
import com.devcycle.sdk.server.local.model.DevCycleLocalOptions;
public class MyClass {
private DevCycleLocalClient client;
public MyClass() {
DevCycleLocalOptions options = DevCycleLocalOptions.builder()
.enableBetaRealtimeUpdates(true)
.build();
client = new DevCycleLocalClient(System.getenv("DEVCYCLE_SERVER_SDK_KEY"), options);
}
}