Hi. I am exploring growthbook java sdk. I have a v...
# ask-questions
h
Hi. I am exploring growthbook java sdk. I have a very basic question. If I toggle on/off any feature, do I need to restart my application for the change to reflect?
b
if you're using the
GBFeaturesRepository
class that does networking for you, it'll re-fetch the features based on the criteria described here: https://docs.growthbook.io/lib/java#fetching-cacheing-and-refreshing-features-with-gbfeaturesrepository Provided that your instance of
GBContext
has the latest features, the feature will be evaluated based on the updates you made:
Copy code
// Initialize the GrowthBook SDK with the GBContext and features
GBContext context = GBContext
    .builder()
    .featuresJson(featuresRepository.getFeaturesJson())
    .attributesJson(userAttributesJson)
    .build();
if you aren't using the
GBFeaturesRepository
to manage features, you'll need to fetch the features yourself and set them on the
GBContext
manually. you can use the builder meethod
.featuresJson()
when creating the context or the
setFeaturesJson()
method after the context is already created. Docs: https://growthbook.github.io/growthbook-sdk-java/growthbook/sdk/java/GBContext.html Let me know if you had any other questions.
add the bottom of the docs there's also some links to example projects (JVM java with spring, JVM kotlin with ktor, and android java): https://docs.growthbook.io/lib/java#code-examples
h
I am using GBFeaturesRepository for features to automatically refresh as soon as it is updated.
Copy code
GBFeaturesRepository
            .builder()           .endpoint("<https://cdn.growthbook.io/api/features/sdk-key>").ttlSeconds(300)            .build();
This ttl second set here is not working. Features are not getting refreshed. Please let me know if I miss anything
b
are you creating a new instance of GBContext and/or setting setting the features on your existing one? the repository class is a standalone fetcher, you need to pass the features from it to the GBContext
here's a video demo of the expected functionality. please see the
/di
endpoint in the java spring example. https://github.com/growthbook/examples/blob/main/jvm-spring-web/src/main/java/com/example/demo/MainController.java#L288
h
Hi I tried this. It worked as per the example given. My concern is everytime I hit the API why do I need to create a new growthbook context? If I reuse existing context for further API calls, then the refresh is not working. Can you please clarify on this
b
refreshing works independently of the growthbook context. you can use the same instance but you'll need to call setFeaturesJson and setAttributes on the context with the features from the gbfeaturesrepository
also, that would only work if your API gets very little traffic, since there can be race conditions with multiple requests. it's recommended to create a new GrowthBook instance with a new GBContext, however the GBFeaturesRepository class should be a singleton.
Copy code
// Initialize the GrowthBook SDK with the GBContext and features
GBContext context = GBContext
    .builder()
    .featuresJson(featuresRepository.getFeaturesJson()) // This needs to be called every time
    .attributesJson(userAttributesJson) // the new user needs to be added to the context every time
    .build();
as mentioned, the refreshing uses a stale-while-revalidate approach. so the first call to stale features will return the stale features but refresh them for subsequent calls.