Skip to content

Commit c115e96

Browse files
authored
chore: update readme for events (#507)
Signed-off-by: Todd Baert <[email protected]>
1 parent 40d1f0a commit c115e96

File tree

1 file changed

+76
-29
lines changed

1 file changed

+76
-29
lines changed

README.md

+76-29
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<h2 align="center">OpenFeature Java SDK</h2>
1111

12-
[![Specification](https://img.shields.io/static/v1?label=Specification&message=v0.5.2&color=yellow)](https://github.com/open-feature/spec/tree/v0.5.2)
12+
[![Specification](https://img.shields.io/static/v1?label=Specification&message=v0.6.0&color=yellow)](https://github.com/open-feature/spec/tree/v0.6.0)
1313
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/dev.openfeature/sdk/badge.svg)](https://maven-badges.herokuapp.com/maven-central/dev.openfeature/sdk)
1414
[![javadoc](https://javadoc.io/badge2/dev.openfeature/sdk/javadoc.svg)](https://javadoc.io/doc/dev.openfeature/sdk)
1515
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
@@ -28,13 +28,13 @@
2828

2929
Standardizing feature flags unifies tools and vendors behind a common interface which avoids vendor lock-in at the code level. Additionally, it offers a framework for building extensions and integrations and allows providers to focus on their unique value proposition.
3030

31-
## 🔍 Requirements:
31+
## 🔍 Requirements
3232

3333
- Java 8+ (compiler target is 1.8)
3434

3535
Note that this library is intended to be used in server-side contexts and has not been evaluated for use in mobile devices.
3636

37-
## 📦 Installation:
37+
## 📦 Installation
3838

3939
### Maven
4040

@@ -76,16 +76,14 @@ dependencies {
7676

7777
We publish SBOMs with all of our releases as of 0.3.0. You can find them in Maven Central alongside the artifacts.
7878

79-
## 🌟 Features:
79+
## 🌟 Features
8080

8181
- support for various backend [providers](https://openfeature.dev/docs/reference/concepts/provider)
8282
- easy integration and extension via [hooks](https://openfeature.dev/docs/reference/concepts/hooks)
8383
- bool, string, numeric, and object flag types
8484
- [context-aware](https://openfeature.dev/docs/reference/concepts/evaluation-context) evaluation
8585

86-
## 🚀 Usage:
87-
88-
### Basics:
86+
## 🚀 Usage
8987

9088
```java
9189
public void example(){
@@ -102,7 +100,7 @@ public void example(){
102100
}
103101
```
104102

105-
### Context-aware evaluation:
103+
### Context-aware evaluation
106104

107105
Sometimes the value of a flag must take into account some dynamic criteria about the application or user, such as the user location, IP, email address, or the location of the server.
108106
In OpenFeature, we refer to this as [`targeting`](https://openfeature.dev/specification/glossary#targeting).
@@ -127,9 +125,73 @@ EvaluationContext reqCtx = new ImmutableContext(targetingKey, attributes);
127125
boolean flagValue = client.getBooleanValue("some-flag", false, reqCtx);
128126
```
129127

128+
### Events
129+
130+
Events allow you to react to state changes in the provider or underlying flag management system, such as flag definition changes, provider readiness, or error conditions.
131+
Initialization events (`PROVIDER_READY` on success, `PROVIDER_ERROR` on failure) are dispatched for every provider.
132+
Some providers support additional events, such as `PROVIDER_CONFIGURATION_CHANGED`.
133+
Please refer to the documentation of the provider you're using to see what events are supported.
134+
135+
```java
136+
// add an event handler to a client
137+
client.onProviderConfigurationChanged((EventDetails eventDetails) -> {
138+
// do something when the provider's flag settings change
139+
});
140+
141+
// add an event handler to the global API
142+
OpenFeatureAPI.getInstance().onProviderStale((EventDetails eventDetails) -> {
143+
// do something when the provider's cache goes stale
144+
});
145+
```
146+
147+
### Hooks
148+
149+
A hook is a mechanism that allows for adding arbitrary behavior at well-defined points of the flag evaluation life-cycle.
150+
Use cases include validating the resolved flag value, modifying or adding data to the evaluation context, logging, telemetry, and tracking.
151+
152+
```java
153+
public class MyHook implements Hook {
154+
/**
155+
*
156+
* @param ctx Information about the particular flag evaluation
157+
* @param details Information about how the flag was resolved, including any resolved values.
158+
* @param hints An immutable mapping of data for users to communicate to the hooks.
159+
*/
160+
@Override
161+
public void after(HookContext ctx, FlagEvaluationDetails details, Map hints) {
162+
System.out.println("After evaluation!");
163+
}
164+
}
165+
```
166+
167+
See [here](https://openfeature.dev/ecosystem?instant_search%5BrefinementList%5D%5Btype%5D%5B0%5D=Hook&instant_search%5BrefinementList%5D%5Btechnology%5D%5B0%5D=Java) for a catalog of available hooks.
168+
169+
### Logging:
170+
171+
The Java SDK uses SLF4J. See the [SLF4J manual](https://slf4j.org/manual.html) for complete documentation.
172+
173+
### Named clients
174+
175+
Clients can be given a name.
176+
A name is a logical identifier which can be used to associate clients with a particular provider.
177+
If a name has no associated provider, clients with that name use the global provider.
178+
179+
```java
180+
FeatureProvider scopedProvider = new MyProvider();
181+
182+
// set this provider for clients named "my-name"
183+
OpenFeatureAPI.getInstance().setProvider("my-name", provider);
184+
185+
// create a client bound to the provider above
186+
Client client = OpenFeatureAPI.getInstance().getClient("my-name");
187+
```
188+
130189
### Providers:
131190

132-
To develop a provider, you need to create a new project and include the OpenFeature SDK as a dependency. This can be a new repository or included in [the existing contrib repository](https://github.com/open-feature/java-sdk-contrib) available under the OpenFeature organization. Finally, you’ll then need to write the provider itself. This can be accomplished by implementing the `FeatureProvider` interface exported by the OpenFeature SDK.
191+
To develop a provider, you need to create a new project and include the OpenFeature SDK as a dependency.
192+
This can be a new repository or included in [the existing contrib repository](https://github.com/open-feature/java-sdk-contrib) available under the OpenFeature organization.
193+
Finally, you’ll then need to write the provider itself.
194+
This can be accomplished by implementing the `FeatureProvider` interface exported by the OpenFeature SDK.
133195

134196
```java
135197
public class MyProvider implements FeatureProvider {
@@ -167,31 +229,16 @@ public class MyProvider implements FeatureProvider {
167229

168230
See [here](https://openfeature.dev/ecosystem?instant_search%5BrefinementList%5D%5Btype%5D%5B0%5D=Provider&instant_search%5BrefinementList%5D%5Btechnology%5D%5B0%5D=Java) for a catalog of available providers.
169231

170-
### Hooks:
232+
### Shutdown
171233

172-
A hook is a mechanism that allows for adding arbitrary behavior at well-defined points of the flag evaluation life-cycle. Use cases include validating the resolved flag value, modifying or adding data to the evaluation context, logging, telemetry, and tracking.
234+
The OpenFeature API provides a close function to perform a cleanup of all registered providers.
235+
This should only be called when your application is in the process of shutting down.
173236

174237
```java
175-
public class MyHook implements Hook {
176-
/**
177-
*
178-
* @param ctx Information about the particular flag evaluation
179-
* @param details Information about how the flag was resolved, including any resolved values.
180-
* @param hints An immutable mapping of data for users to communicate to the hooks.
181-
*/
182-
@Override
183-
public void after(HookContext ctx, FlagEvaluationDetails details, Map hints) {
184-
System.out.println("After evaluation!");
185-
}
186-
}
238+
// shut down all providers
239+
OpenFeatureAPI.getInstance().shutdown();
187240
```
188241

189-
See [here](https://openfeature.dev/ecosystem?instant_search%5BrefinementList%5D%5Btype%5D%5B0%5D=Hook&instant_search%5BrefinementList%5D%5Btechnology%5D%5B0%5D=Java) for a catalog of available hooks.
190-
191-
### Logging:
192-
193-
The Java SDK uses SLF4J. See the [SLF4J manual](https://slf4j.org/manual.html) for complete documentation.
194-
195242
### Complete API documentation:
196243

197244
See [here](https://www.javadoc.io/doc/dev.openfeature/sdk/latest/index.html) for the complete API documentation.

0 commit comments

Comments
 (0)