Skip to content

Commit 04e7298

Browse files
Config API : HTTP APIs and Unsubscribe gRPC API changes (#740)
* Config API : Unsubscribe config items changes Signed-off-by: pravinpushkar <[email protected]> * Unit and integration tests changes Signed-off-by: pravinpushkar <[email protected]> * Addressing review comments Signed-off-by: Pravin Pushkar <[email protected]> * GetConfiguration HTTP API impl Signed-off-by: Pravin Pushkar <[email protected]> * Subscribe and Unsubscribe HTTP changes Signed-off-by: Pravin Pushkar <[email protected]> * Subscribe config http changes Signed-off-by: Pravin Pushkar <[email protected]> * Http unsubscribe and unit tests Signed-off-by: Pravin Pushkar <[email protected]> * Integration tests for http config apis Signed-off-by: Pravin Pushkar <[email protected]> * Fixing lint Signed-off-by: Pravin Pushkar <[email protected]> * Chnaging rersponse of HTTP Unsubscribe API Signed-off-by: Pravin Pushkar <[email protected]> * Fix integration test failure Signed-off-by: Pravin Pushkar <[email protected]> * refactor IT tests Signed-off-by: Pravin Pushkar <[email protected]> * code clean up Signed-off-by: Pravin Pushkar <[email protected]> * setting dapr ref Signed-off-by: Pravin Pushkar <[email protected]> * Update readme Signed-off-by: Pravin Pushkar <[email protected]> * Modify Unit tests Signed-off-by: Pravin Pushkar <[email protected]> * Remove failing buggy tests Signed-off-by: Pravin Pushkar <[email protected]> * fix review commnets and integration tests Signed-off-by: Pravin Pushkar <[email protected]> * trigger pr check Signed-off-by: Pravin Pushkar <[email protected]> * Fix for getAll keys Signed-off-by: Pravin Pushkar <[email protected]> * Fix subscribe All Signed-off-by: Pravin Pushkar <[email protected]> * Move http subscribe handle to dapr controller Signed-off-by: Pravin Pushkar <[email protected]> * Simplified examples Signed-off-by: Pravin Pushkar <[email protected]> * change dapr ref, and add unit test Signed-off-by: Pravin Pushkar <[email protected]> * trigger pr checks Signed-off-by: Pravin Pushkar <[email protected]> * trigger pr checks Signed-off-by: Pravin Pushkar <[email protected]> * Use biConsumer to register handlers Signed-off-by: Pravin Pushkar <[email protected]> * Re use args contructors and re add final modifier Signed-off-by: Pravin Pushkar <[email protected]> * put dapr run back Signed-off-by: Pravin Pushkar <[email protected]> * fix nitpick Signed-off-by: Pravin Pushkar <[email protected]> * Remove the springboot HTTP "convenience". Signed-off-by: Artur Souza <[email protected]> Signed-off-by: pravinpushkar <[email protected]> Signed-off-by: Pravin Pushkar <[email protected]> Signed-off-by: Artur Souza <[email protected]> Co-authored-by: Artur Souza <[email protected]>
1 parent 44f80c9 commit 04e7298

23 files changed

+1433
-210
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ jobs:
2525
GOARCH: amd64
2626
GOPROXY: https://proxy.golang.org
2727
JDK_VER: ${{ matrix.java }}
28-
DAPR_CLI_VER: 1.8.0-rc.1
29-
DAPR_RUNTIME_VER: 1.8.0-rc.3
28+
DAPR_CLI_VER: 1.8.1
29+
DAPR_RUNTIME_VER: 1.8.4
3030
DAPR_INSTALL_URL: https://github.com/raw/dapr/cli/v1.8.0-rc.1/install/install.sh
3131
DAPR_CLI_REF:
32-
DAPR_REF: 683a436ff1367e4ff2d27da2d79069c04ec2c46d
32+
DAPR_REF: 0e34de1086c697d7d74ac2af14ddb11f9346b57e
3333
steps:
3434
- uses: actions/checkout@v3
3535
- name: Set up OpenJDK ${{ env.JDK_VER }}

.github/workflows/validate.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ jobs:
3636
GOARCH: amd64
3737
GOPROXY: https://proxy.golang.org
3838
JDK_VER: ${{ matrix.java }}
39-
DAPR_CLI_VER: 1.8.0-rc.1
40-
DAPR_RUNTIME_VER: 1.8.0-rc.3
39+
DAPR_CLI_VER: 1.8.1
40+
DAPR_RUNTIME_VER: 1.8.4
4141
DAPR_INSTALL_URL: https://github.com/raw/dapr/cli/v1.8.0-rc.1/install/install.sh
4242
DAPR_CLI_REF:
43-
DAPR_REF: 683a436ff1367e4ff2d27da2d79069c04ec2c46d
43+
DAPR_REF: 0e34de1086c697d7d74ac2af14ddb11f9346b57e
4444
steps:
4545
- uses: actions/checkout@v3
4646
- name: Set up OpenJDK ${{ env.JDK_VER }}
@@ -145,10 +145,14 @@ jobs:
145145
working-directory: ./examples
146146
run: |
147147
mm.py ./src/main/java/io/dapr/examples/unittesting/README.md
148-
- name: Validate Configuration API example
148+
- name: Validate Configuration gRPC API example
149149
working-directory: ./examples
150150
run: |
151151
mm.py ./src/main/java/io/dapr/examples/configuration/grpc/README.md
152+
- name: Validate Configuration HTTP API example
153+
working-directory: ./examples
154+
run: |
155+
mm.py ./src/main/java/io/dapr/examples/configuration/http/README.md
152156
- name: Validate query state HTTP example
153157
working-directory: ./examples
154158
run: |

examples/src/main/java/io/dapr/examples/configuration/grpc/ConfigurationClient.java

Lines changed: 19 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
import io.dapr.client.domain.ConfigurationItem;
1919
import io.dapr.client.domain.GetConfigurationRequest;
2020
import io.dapr.client.domain.SubscribeConfigurationRequest;
21+
import io.dapr.client.domain.SubscribeConfigurationResponse;
22+
import io.dapr.client.domain.UnsubscribeConfigurationResponse;
2123
import reactor.core.Disposable;
2224
import reactor.core.publisher.Flux;
2325
import reactor.core.publisher.Mono;
2426

2527
import java.io.IOException;
2628
import java.util.ArrayList;
2729
import java.util.Arrays;
30+
import java.util.Collections;
2831
import java.util.List;
2932
import java.util.Map;
3033
import java.util.concurrent.atomic.AtomicReference;
@@ -43,41 +46,8 @@ public class ConfigurationClient {
4346
public static void main(String[] args) throws Exception {
4447
try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) {
4548
System.out.println("Using preview client...");
46-
getConfigurationForaSingleKey(client);
47-
getConfigurationsUsingVarargs(client);
4849
getConfigurations(client);
49-
subscribeConfigurationRequestWithSubscribe(client);
50-
}
51-
}
52-
53-
/**
54-
* Gets configuration for a single key.
55-
*
56-
* @param client DaprPreviewClient object
57-
*/
58-
public static void getConfigurationForaSingleKey(DaprPreviewClient client) {
59-
System.out.println("*******trying to retrieve configuration given a single key********");
60-
try {
61-
Mono<ConfigurationItem> item = client.getConfiguration(CONFIG_STORE_NAME, keys.get(0));
62-
System.out.println("Value ->" + item.block().getValue() + " key ->" + item.block().getKey());
63-
} catch (Exception ex) {
64-
System.out.println(ex.getMessage());
65-
}
66-
}
67-
68-
/**
69-
* Gets configurations for varibale no. of arguments.
70-
*
71-
* @param client DaprPreviewClient object
72-
*/
73-
public static void getConfigurationsUsingVarargs(DaprPreviewClient client) {
74-
System.out.println("*******trying to retrieve configurations for a variable no. of keys********");
75-
try {
76-
Mono<Map<String, ConfigurationItem>> items =
77-
client.getConfiguration(CONFIG_STORE_NAME, "myconfig1", "myconfig3");
78-
items.block().forEach((k,v) -> print(v, k));
79-
} catch (Exception ex) {
80-
System.out.println(ex.getMessage());
50+
subscribeConfigurationRequest(client);
8151
}
8252
}
8353

@@ -106,36 +76,25 @@ public static void getConfigurations(DaprPreviewClient client) {
10676
*
10777
* @param client DaprPreviewClient object
10878
*/
109-
public static void subscribeConfigurationRequestWithSubscribe(DaprPreviewClient client) {
110-
System.out.println("*****Subscribing to keys using subscribe method: " + keys.toString() + " *****");
111-
AtomicReference<Disposable> disposableAtomicReference = new AtomicReference<>();
112-
SubscribeConfigurationRequest req = new SubscribeConfigurationRequest(CONFIG_STORE_NAME, keys);
79+
public static void subscribeConfigurationRequest(DaprPreviewClient client) {
80+
System.out.println("Subscribing to key: myconfig1");
81+
SubscribeConfigurationRequest req = new SubscribeConfigurationRequest(
82+
CONFIG_STORE_NAME, Collections.singletonList("myconfig1"));
83+
Flux<SubscribeConfigurationResponse> outFlux = client.subscribeConfiguration(req);
11384
Runnable subscribeTask = () -> {
114-
Flux<Map<String, ConfigurationItem>> outFlux = client.subscribeToConfiguration(req);
115-
disposableAtomicReference.set(outFlux
116-
.subscribe(
117-
cis -> cis.forEach((k,v) -> print(v, k))
118-
));
85+
outFlux.subscribe(cis -> {
86+
System.out.println("subscription ID : " + cis.getSubscriptionId());
87+
System.out.println("subscribing to key myconfig1 is successful");
88+
});
11989
};
12090
new Thread(subscribeTask).start();
91+
// To ensure main thread does not die before outFlux subscribe gets called
92+
inducingSleepTime(5000);
93+
}
94+
95+
private static void inducingSleepTime(int timeInMillis) {
12196
try {
122-
// To ensure that subscribeThread gets scheduled
123-
Thread.sleep(0);
124-
} catch (InterruptedException e) {
125-
e.printStackTrace();
126-
}
127-
Runnable updateKeys = () -> {
128-
int i = 1;
129-
while (i <= 3) {
130-
executeDockerCommand(i);
131-
i++;
132-
}
133-
};
134-
new Thread(updateKeys).start();
135-
try {
136-
// To ensure main thread does not die before outFlux subscribe gets called
137-
Thread.sleep(10000);
138-
disposableAtomicReference.get().dispose();
97+
Thread.sleep(timeInMillis);
13998
} catch (InterruptedException e) {
14099
e.printStackTrace();
141100
}
@@ -144,22 +103,4 @@ public static void subscribeConfigurationRequestWithSubscribe(DaprPreviewClient
144103
private static void print(ConfigurationItem item, String key) {
145104
System.out.println(item.getValue() + " : key ->" + key);
146105
}
147-
148-
private static void executeDockerCommand(int postfix) {
149-
String[] command = new String[] {
150-
"docker", "exec", "dapr_redis", "redis-cli",
151-
"SET",
152-
"myconfig" + postfix, "update_myconfigvalue" + postfix + "||2"
153-
};
154-
ProcessBuilder processBuilder = new ProcessBuilder(command);
155-
Process process = null;
156-
try {
157-
process = processBuilder.start();
158-
process.waitFor();
159-
} catch (IOException e) {
160-
e.printStackTrace();
161-
} catch (InterruptedException e) {
162-
e.printStackTrace();
163-
}
164-
}
165106
}

examples/src/main/java/io/dapr/examples/configuration/grpc/README.md

Lines changed: 86 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ This example provides the different capabilities provided by Dapr Java SDK for C
77

88
The java SDK exposes several methods for this -
99
* `client.getConfiguration(...)` for getting a configuration for a single/multiple keys.
10-
* `client.subscribeToConfigurations(...)` for subscribing to a list of keys for any change.
10+
* `client.subscribeConfiguration(...)` for subscribing to a list of keys for any change.
11+
* `client.unsubscribeConfiguration(...)` for unsubscribing to changes from subscribed items.
1112

1213
## Pre-requisites
1314

@@ -48,6 +49,81 @@ docker exec dapr_redis redis-cli MSET myconfig1 "val1||1" myconfig2 "val2||1" my
4849

4950
### Running the example
5051

52+
This example uses the Java SDK Dapr client in order to **Get, Subscribe and Unsubscribe** from configuration items and utilizes `Redis` as configuration store.
53+
`ConfigurationClient.java` is the example class demonstrating all 3 features.
54+
Kindly check [DaprPreviewClient.java](https://github.com/dapr/java-sdk/blob/master/sdk/src/main/java/io/dapr/client/DaprPreviewClient.java) for detailed description of the supported APIs.
55+
56+
```java
57+
public class ConfigurationClient {
58+
// ...
59+
/**
60+
* Executes various methods to check the different apis.
61+
* @param args arguments
62+
* @throws Exception throws Exception
63+
*/
64+
public static void main(String[] args) throws Exception {
65+
try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) {
66+
System.out.println("Using preview client...");
67+
getConfigurations(client);
68+
subscribeConfigurationRequestWithSubscribe(client);
69+
unsubscribeConfigurationItems(client);
70+
}
71+
}
72+
73+
/**
74+
* Gets configurations for a list of keys.
75+
*
76+
* @param client DaprPreviewClient object
77+
*/
78+
public static void getConfigurations(DaprPreviewClient client) {
79+
System.out.println("*******trying to retrieve configurations for a list of keys********");
80+
List<String> keys = new ArrayList<>();
81+
// ...
82+
GetConfigurationRequest req = new GetConfigurationRequest(CONFIG_STORE_NAME, keys);
83+
try {
84+
Mono<List<ConfigurationItem>> items = client.getConfiguration(req);
85+
// ..
86+
} catch (Exception ex) {}
87+
}
88+
89+
/**
90+
* Subscribe to a list of keys.Optional to above iterator way of retrieving the changes
91+
*
92+
* @param client DaprPreviewClient object
93+
*/
94+
public static void subscribeConfigurationRequestWithSubscribe(DaprPreviewClient client) {
95+
System.out.println("Subscribing to key: myconfig1");
96+
// ...
97+
Runnable subscribeTask = () -> {
98+
Flux<SubscribeConfigurationResponse> outFlux = client.subscribeConfiguration(req);
99+
// ...
100+
};
101+
// ..
102+
}
103+
104+
/**
105+
* Unsubscribe using subscription id.
106+
*
107+
* @param client DaprPreviewClient object
108+
*/
109+
public static void unsubscribeConfigurationItems(DaprPreviewClient client) {
110+
System.out.println("Subscribing to key: myconfig2");
111+
// ..
112+
Runnable subscribeTask = () -> {
113+
Flux<SubscribeConfigurationResponse> outFlux = client.subscribeConfiguration(CONFIG_STORE_NAME, "myconfig2");
114+
// ...
115+
};
116+
// ...
117+
118+
UnsubscribeConfigurationResponse res = client.unsubscribeConfiguration(
119+
subscriptionId.get(),
120+
CONFIG_STORE_NAME
121+
).block();
122+
// ..
123+
}
124+
}
125+
```
126+
51127
Get into the examples' directory:
52128
```sh
53129
cd examples
@@ -59,21 +135,16 @@ Use the following command to run this example-
59135
name: Run ConfigurationClient example
60136
expected_stdout_lines:
61137
- "== APP == Using preview client..."
62-
- "== APP == *******trying to retrieve configuration given a single key********"
63-
- "== APP == Value ->val1 key ->myconfig1"
64-
- "== APP == *******trying to retrieve configurations for a variable no. of keys********"
65-
- "== APP == val1 : key ->myconfig1"
66-
- "== APP == val3 : key ->myconfig3"
67138
- "== APP == *******trying to retrieve configurations for a list of keys********"
68139
- "== APP == val1 : key ->myconfig1"
69140
- "== APP == val2 : key ->myconfig2"
70141
- "== APP == val3 : key ->myconfig3"
71-
- "== APP == *****Subscribing to keys using subscribe method: [myconfig1, myconfig3, myconfig2] *****"
72-
- "== APP == update_myconfigvalue1 : key ->myconfig1"
73-
- "== APP == update_myconfigvalue2 : key ->myconfig2"
74-
- "== APP == update_myconfigvalue3 : key ->myconfig3"
142+
- "== APP == Subscribing to key: myconfig1"
143+
- "== APP == subscription ID :"
144+
- "== APP == subscribing to key myconfig1 is successful"
75145
background: true
76-
sleep: 5
146+
output_match_mode: substring
147+
sleep: 10
77148
-->
78149

79150
```bash
@@ -85,19 +156,14 @@ dapr run --components-path ./components/configuration --app-id configgrpc --log-
85156
### Sample output
86157
```
87158
== APP == Using preview client...
88-
== APP == *******trying to retrieve configuration given a single key********
89-
== APP == Value ->val1 key ->myconfig1
90-
== APP == *******trying to retrieve configurations for a variable no. of keys********
91-
== APP == val1 : key ->myconfig1
92-
== APP == val3 : key ->myconfig3
93159
== APP == *******trying to retrieve configurations for a list of keys********
94160
== APP == val1 : key ->myconfig1
95161
== APP == val2 : key ->myconfig2
96162
== APP == val3 : key ->myconfig3
97-
== APP == *****Subscribing to keys using subscribe method: [myconfig1, myconfig3, myconfig2] *****
98-
== APP == update_myconfigvalue1 : key ->myconfig1
99-
== APP == update_myconfigvalue2 : key ->myconfig2
100-
== APP == update_myconfigvalue3 : key ->myconfig3
163+
== APP == Subscribing to key: myconfig1
164+
== APP == subscription ID : 82bb8e24-f69d-477a-9126-5ffaf995f498
165+
== APP == subscribing to key myconfig1 is successful
166+
101167
102168
```
103169
### Cleanup

0 commit comments

Comments
 (0)