Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,7 @@ jobs:
working-directory: ./examples
run: |
mm.py ./src/main/java/io/dapr/examples/unittesting/README.md
- name: Validate Configuration API example
working-directory: ./examples
run: |
mm.py ./src/main/java/io/dapr/examples/configuration/grpc/README.md
12 changes: 12 additions & 0 deletions .java_header
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
^/\*$
^ \* Copyright \d\d\d\d The Dapr Authors$
^ \* Licensed under the Apache License, Version 2.0 \(the "License"\)\;$
^ \* you may not use this file except in compliance with the License\.$
^ \* You may obtain a copy of the License at$
^ \* http://www.apache.org/licenses/LICENSE-2\.0$
^ \* Unless required by applicable law or agreed to in writing, software$
^ \* distributed under the License is distributed on an "AS IS" BASIS,$
^ \* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied\.$
^ \* See the License for the specific language governing permissions and$
^limitations under the License\.$
^\*/$
5 changes: 2 additions & 3 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
</module>

<module name="Header">
<module name="RegexpHeader">
<property name="fileExtensions" value="java"/>
<!-- We just validate the top 2 lines so this attribute's value does not blow away. -->
<property name="header" value='/*\n * Copyright 2021 The Dapr Authors\n * Licensed under the Apache License, Version 2.0 (the "License");\n'/>
<property name="headerFile" value=".java_header"/>
</module>

<module name="TreeWalker">
Expand Down
12 changes: 12 additions & 0 deletions examples/components/configuration/redis_configstore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: configstore
spec:
type: configuration.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* Copyright 2022 The Dapr Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
limitations under the License.
*/

package io.dapr.examples.configuration.grpc;

import io.dapr.client.DaprClientBuilder;
import io.dapr.client.DaprPreviewClient;
import io.dapr.client.domain.ConfigurationItem;
import io.dapr.client.domain.GetConfigurationRequest;
import io.dapr.client.domain.SubscribeConfigurationRequest;
import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

public class ConfigurationClient {

private static final String CONFIG_STORE_NAME = "configstore";

private static final List<String> keys = new ArrayList<>(Arrays.asList("myconfig1", "myconfig3", "myconfig2"));

/**
* Executes various methods to check the different apis.
* @param args arguments
* @throws Exception throws Exception
*/
public static void main(String[] args) throws Exception {
try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) {
System.out.println("Using preview client...");
getConfigurationForaSingleKey(client);
getConfigurationsUsingVarargs(client);
getConfigurations(client);
subscribeConfigurationRequestWithSubscribe(client);
}
}

/**
* Gets configuration for a single key.
*
* @param client DaprPreviewClient object
*/
public static void getConfigurationForaSingleKey(DaprPreviewClient client) {
System.out.println("*******trying to retrieve configuration given a single key********");
try {
Mono<ConfigurationItem> item = client.getConfiguration(CONFIG_STORE_NAME, keys.get(0));
System.out.println("Value ->" + item.block().getValue() + " key ->" + item.block().getKey());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}

/**
* Gets configurations for varibale no. of arguments.
*
* @param client DaprPreviewClient object
*/
public static void getConfigurationsUsingVarargs(DaprPreviewClient client) {
System.out.println("*******trying to retrieve configurations for a variable no. of keys********");
try {
Mono<List<ConfigurationItem>> items =
client.getConfiguration(CONFIG_STORE_NAME, "myconfig1", "myconfig3");
items.block().forEach(ConfigurationClient::print);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}

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

/**
* Subscribe to a list of keys.Optional to above iterator way of retrieving the changes
*
* @param client DaprPreviewClient object
*/
public static void subscribeConfigurationRequestWithSubscribe(DaprPreviewClient client) {
System.out.println("*****Subscribing to keys using subscribe method: " + keys.toString() + " *****");
AtomicReference<Disposable> disposableAtomicReference = new AtomicReference<>();
SubscribeConfigurationRequest req = new SubscribeConfigurationRequest(CONFIG_STORE_NAME, keys);
Runnable subscribeTask = () -> {
Flux<List<ConfigurationItem>> outFlux = client.subscribeToConfiguration(req);
disposableAtomicReference.set(outFlux
.subscribe(
cis -> cis.forEach(ConfigurationClient::print)
));
};
new Thread(subscribeTask).start();
try {
// To ensure that subscribeThread gets scheduled
Thread.sleep(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
Runnable updateKeys = () -> {
int i = 1;
while (i <= 3) {
executeDockerCommand(i);
i++;
}
};
new Thread(updateKeys).start();
try {
// To ensure main thread does not die before outFlux subscribe gets called
Thread.sleep(10000);
disposableAtomicReference.get().dispose();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

private static void print(ConfigurationItem item) {
System.out.println(item.getValue() + " : key ->" + item.getKey());
}

private static void executeDockerCommand(int postfix) {
String[] command = new String[] {
"docker", "exec", "dapr_redis", "redis-cli",
"SET",
"myconfig" + postfix, "update_myconfigvalue" + postfix + "||2"
};
ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = null;
try {
process = processBuilder.start();
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
113 changes: 113 additions & 0 deletions examples/src/main/java/io/dapr/examples/configuration/grpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
## 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](https://docs.dapr.io/developing-applications/building-blocks/configuration/)
**This API is available in Preview Mode**.

### Using the ConfigurationAPI

The java SDK exposes several methods for this -
* `client.getConfiguration(...)` for getting a configuration for a single/multiple keys.
* `client.subscribeToConfigurations(...)` for subscribing to a list of keys for any change.

## Pre-requisites

* [Dapr and Dapr Cli](https://docs.dapr.io/getting-started/install-dapr/).
* Java JDK 11 (or greater): [Oracle JDK](https://www.oracle.com/technetwork/java/javase/downloads/index.html#JDK11) or [OpenJDK](https://jdk.java.net/13/).
* [Apache Maven](https://maven.apache.org/install.html) version 3.x.

### Checking out the code

Clone this repository:

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

Then build the Maven project:

```sh
# make sure you are in the `java-sdk` directory.
mvn install
```
## Store few dummy configurations in configurationstore
<!-- STEP
name: Set configuration value
expected_stdout_lines:
- "OK"
timeout_seconds: 20
-->

```bash
docker exec dapr_redis redis-cli MSET myconfig1 "val1||1" myconfig2 "val2||1" myconfig3 "val3||1"
```
<!-- END_STEP -->

### Running the example

Get into the examples' directory:
```sh
cd examples
```

Use the following command to run this example-

<!-- STEP
name: Run ConfigurationClient example
Comment thread
pravinpushkar marked this conversation as resolved.
expected_stdout_lines:
- "== APP == Using preview client..."
- "== APP == *******trying to retrieve configuration given a single key********"
- "== APP == Value ->val1 key ->myconfig1"
- "== APP == *******trying to retrieve configurations for a variable no. of keys********"
- "== APP == val1 : key ->myconfig1"
- "== APP == val3 : key ->myconfig3"
- "== 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 keys using subscribe method: [myconfig1, myconfig3, myconfig2] *****"
- "== APP == update_myconfigvalue1 : key ->myconfig1"
- "== APP == update_myconfigvalue2 : key ->myconfig2"
- "== APP == update_myconfigvalue3 : key ->myconfig3"
background: true
sleep: 5
-->

```bash
Comment thread
pravinpushkar marked this conversation as resolved.
dapr run --components-path ./components/configuration --app-id configgrpc --log-level debug -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.configuration.grpc.ConfigurationClient
```

<!-- END_STEP -->

### Sample output
```
== APP == Using preview client...
== APP == *******trying to retrieve configuration given a single key********
== APP == Value ->val1 key ->myconfig1
== APP == *******trying to retrieve configurations for a variable no. of keys********
== APP == val1 : key ->myconfig1
== APP == val3 : key ->myconfig3
== 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 keys using subscribe method: [myconfig1, myconfig3, myconfig2] *****
== APP == update_myconfigvalue1 : key ->myconfig1
== APP == update_myconfigvalue2 : key ->myconfig2
== APP == update_myconfigvalue3 : key ->myconfig3

```
### Cleanup

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

<!-- STEP
name: Cleanup
-->

```bash
dapr stop --app-id configgrpc
```

<!-- END_STEP -->

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<grpc.version>1.39.0</grpc.version>
<protobuf.version>3.13.0</protobuf.version>
<dapr.proto.baseurl>https://raw.githubusercontent.com/dapr/dapr/v1.4.0-rc.6/dapr/proto</dapr.proto.baseurl>
<dapr.proto.baseurl>https://raw.githubusercontent.com/dapr/dapr/v1.5.1/dapr/proto</dapr.proto.baseurl>
<os-maven-plugin.version>1.6.2</os-maven-plugin.version>
<maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
<maven-antrun-plugin.version>1.8</maven-antrun-plugin.version>
Expand Down
12 changes: 12 additions & 0 deletions sdk-tests/components/redisconfigstore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: redisconfigstore
spec:
type: configuration.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""
Loading