Skip to content

Latest commit

 

History

History
 
 

configuration

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Retrieve Configurations via Configuration API

This example provides the different capabilities provided by Dapr Java SDK for Configuration. For further information about Configuration APIs please refer to this link

Using the ConfigurationAPI

The Java SDK exposes several methods for this -

  • client.getConfiguration(...) for getting configuration for a single/multiple key(s).
  • client.subscribeConfiguration(...) for subscribing to a list of keys for any change.
  • client.unsubscribeConfiguration(...) for unsubscribing to changes from subscribed items.

Pre-requisites

Checking out the code

Clone this repository:

git clone https://github.com/dapr/java-sdk.git
cd java-sdk

Then build the Maven project:

# make sure you are in the `java-sdk` directory.
mvn install

Then get into the examples directory:

cd examples

Initialize Dapr

Run dapr init to initialize Dapr in Self-Hosted Mode if it's not already initialized.

Store dummy configurations in configuration store

docker exec dapr_redis redis-cli MSET myconfig1 "val1||1" myconfig2 "val2||1" myconfig3 "val3||1"

Running the example

This example uses the Java SDK Dapr client in order to Get, Subscribe and Unsubscribe from configuration items, and utilizes Redis as configuration store. ConfigurationClient.java is the example class demonstrating all 3 features. Kindly check DaprClient.java for a detailed description of the supported APIs.

public class ConfigurationClient {
    // ... 
  /**
   * Executes various methods to check the different apis.
   * @param args arguments
   * @throws Exception throws Exception
   */
  public static void main(String[] args) throws Exception {
    try (DaprClient client = (new DaprClientBuilder()).build()) {
      System.out.println("Using Dapr client...");
      getConfigurations(client);
      subscribeConfigurationRequestWithSubscribe(client);
      unsubscribeConfigurationItems(client);
    }
  }

  /**
   * Gets configurations for a list of keys.
   *
   * @param client DaprClient object
   */
  public static void getConfigurations(DaprClient client) {
    System.out.println("*******trying to retrieve configurations for a list of keys********");
    List<String> keys = new ArrayList<>();
    // ...
    GetConfigurationRequest req = new GetConfigurationRequest(CONFIG_STORE_NAME, keys);
    try {
      Mono<List<ConfigurationItem>> items = client.getConfiguration(req);
      // ..
    } catch (Exception ex) {}
  }

  /**
   * Subscribe to a list of keys.Optional to above iterator way of retrieving the changes
   *
   * @param client DaprClient object
   */
  public static void subscribeConfigurationRequestWithSubscribe(DaprClient client) {
    System.out.println("Subscribing to key: myconfig1");
    // ...
    Runnable subscribeTask = () -> {
      Flux<SubscribeConfigurationResponse> outFlux = client.subscribeConfiguration(req);
      // ...
    };
    // ..
  }

  /**
   * Unsubscribe using subscription id.
   *
   * @param client DaprClient object
   */
  public static void unsubscribeConfigurationItems(DaprClient client) {
    System.out.println("Subscribing to key: myconfig2");
    // ..
    Runnable subscribeTask = () -> {
      Flux<SubscribeConfigurationResponse> outFlux = client.subscribeConfiguration(CONFIG_STORE_NAME, "myconfig2");
      // ...
    };
    // ...

    UnsubscribeConfigurationResponse res = client.unsubscribeConfiguration(
        subscriptionId.get(),
        CONFIG_STORE_NAME
    ).block();
    // ..
  }
}

Get into the examples' directory:

cd examples

Use the following command to run this example-

dapr run --resources-path ./components/configuration --app-id configgrpc --log-level debug -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.configuration.ConfigurationClient

Sample output

== APP == Using Dapr client...
== APP == *******trying to retrieve configurations for a list of keys********
== APP == val1 : key ->myconfig1
== APP == val2 : key ->myconfig2
== APP == val3 : key ->myconfig3
== APP == Subscribing to key: myconfig1
== APP == subscription ID : 82bb8e24-f69d-477a-9126-5ffaf995f498
== APP == subscribing to key myconfig1 is successful


Cleanup

To stop the app, run (or press CTRL+C):

dapr stop --app-id configgrpc