|
| 1 | +/* |
| 2 | + * Copyright 2022 The Dapr Authors |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package io.dapr.examples.configuration.grpc; |
| 15 | + |
| 16 | +import io.dapr.client.DaprClientBuilder; |
| 17 | +import io.dapr.client.DaprPreviewClient; |
| 18 | +import io.dapr.client.domain.ConfigurationItem; |
| 19 | +import io.dapr.client.domain.GetConfigurationRequest; |
| 20 | +import io.dapr.client.domain.SubscribeConfigurationRequest; |
| 21 | +import reactor.core.Disposable; |
| 22 | +import reactor.core.publisher.Flux; |
| 23 | +import reactor.core.publisher.Mono; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.List; |
| 29 | +import java.util.concurrent.atomic.AtomicReference; |
| 30 | + |
| 31 | +public class ConfigurationClient { |
| 32 | + |
| 33 | + private static final String CONFIG_STORE_NAME = "configstore"; |
| 34 | + |
| 35 | + private static final List<String> keys = new ArrayList<>(Arrays.asList("myconfig1", "myconfig3", "myconfig2")); |
| 36 | + |
| 37 | + /** |
| 38 | + * Executes various methods to check the different apis. |
| 39 | + * @param args arguments |
| 40 | + * @throws Exception throws Exception |
| 41 | + */ |
| 42 | + public static void main(String[] args) throws Exception { |
| 43 | + try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) { |
| 44 | + System.out.println("Using preview client..."); |
| 45 | + getConfigurationForaSingleKey(client); |
| 46 | + getConfigurationsUsingVarargs(client); |
| 47 | + getConfigurations(client); |
| 48 | + subscribeConfigurationRequestWithSubscribe(client); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Gets configuration for a single key. |
| 54 | + * |
| 55 | + * @param client DaprPreviewClient object |
| 56 | + */ |
| 57 | + public static void getConfigurationForaSingleKey(DaprPreviewClient client) { |
| 58 | + System.out.println("*******trying to retrieve configuration given a single key********"); |
| 59 | + try { |
| 60 | + Mono<ConfigurationItem> item = client.getConfiguration(CONFIG_STORE_NAME, keys.get(0)); |
| 61 | + System.out.println("Value ->" + item.block().getValue() + " key ->" + item.block().getKey()); |
| 62 | + } catch (Exception ex) { |
| 63 | + System.out.println(ex.getMessage()); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Gets configurations for varibale no. of arguments. |
| 69 | + * |
| 70 | + * @param client DaprPreviewClient object |
| 71 | + */ |
| 72 | + public static void getConfigurationsUsingVarargs(DaprPreviewClient client) { |
| 73 | + System.out.println("*******trying to retrieve configurations for a variable no. of keys********"); |
| 74 | + try { |
| 75 | + Mono<List<ConfigurationItem>> items = |
| 76 | + client.getConfiguration(CONFIG_STORE_NAME, "myconfig1", "myconfig3"); |
| 77 | + items.block().forEach(ConfigurationClient::print); |
| 78 | + } catch (Exception ex) { |
| 79 | + System.out.println(ex.getMessage()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Gets configurations for a list of keys. |
| 85 | + * |
| 86 | + * @param client DaprPreviewClient object |
| 87 | + */ |
| 88 | + public static void getConfigurations(DaprPreviewClient client) { |
| 89 | + System.out.println("*******trying to retrieve configurations for a list of keys********"); |
| 90 | + List<String> keys = new ArrayList<>(); |
| 91 | + keys.add("myconfig1"); |
| 92 | + keys.add("myconfig2"); |
| 93 | + keys.add("myconfig3"); |
| 94 | + GetConfigurationRequest req = new GetConfigurationRequest(CONFIG_STORE_NAME, keys); |
| 95 | + try { |
| 96 | + Mono<List<ConfigurationItem>> items = client.getConfiguration(req); |
| 97 | + items.block().forEach(ConfigurationClient::print); |
| 98 | + } catch (Exception ex) { |
| 99 | + System.out.println(ex.getMessage()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Subscribe to a list of keys.Optional to above iterator way of retrieving the changes |
| 105 | + * |
| 106 | + * @param client DaprPreviewClient object |
| 107 | + */ |
| 108 | + public static void subscribeConfigurationRequestWithSubscribe(DaprPreviewClient client) { |
| 109 | + System.out.println("*****Subscribing to keys using subscribe method: " + keys.toString() + " *****"); |
| 110 | + AtomicReference<Disposable> disposableAtomicReference = new AtomicReference<>(); |
| 111 | + SubscribeConfigurationRequest req = new SubscribeConfigurationRequest(CONFIG_STORE_NAME, keys); |
| 112 | + Runnable subscribeTask = () -> { |
| 113 | + Flux<List<ConfigurationItem>> outFlux = client.subscribeToConfiguration(req); |
| 114 | + disposableAtomicReference.set(outFlux |
| 115 | + .subscribe( |
| 116 | + cis -> cis.forEach(ConfigurationClient::print) |
| 117 | + )); |
| 118 | + }; |
| 119 | + new Thread(subscribeTask).start(); |
| 120 | + try { |
| 121 | + // To ensure that subscribeThread gets scheduled |
| 122 | + Thread.sleep(0); |
| 123 | + } catch (InterruptedException e) { |
| 124 | + e.printStackTrace(); |
| 125 | + } |
| 126 | + Runnable updateKeys = () -> { |
| 127 | + int i = 1; |
| 128 | + while (i <= 3) { |
| 129 | + executeDockerCommand(i); |
| 130 | + i++; |
| 131 | + } |
| 132 | + }; |
| 133 | + new Thread(updateKeys).start(); |
| 134 | + try { |
| 135 | + // To ensure main thread does not die before outFlux subscribe gets called |
| 136 | + Thread.sleep(10000); |
| 137 | + disposableAtomicReference.get().dispose(); |
| 138 | + } catch (InterruptedException e) { |
| 139 | + e.printStackTrace(); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private static void print(ConfigurationItem item) { |
| 144 | + System.out.println(item.getValue() + " : key ->" + item.getKey()); |
| 145 | + } |
| 146 | + |
| 147 | + private static void executeDockerCommand(int postfix) { |
| 148 | + String[] command = new String[] { |
| 149 | + "docker", "exec", "dapr_redis", "redis-cli", |
| 150 | + "SET", |
| 151 | + "myconfig" + postfix, "update_myconfigvalue" + postfix + "||2" |
| 152 | + }; |
| 153 | + ProcessBuilder processBuilder = new ProcessBuilder(command); |
| 154 | + Process process = null; |
| 155 | + try { |
| 156 | + process = processBuilder.start(); |
| 157 | + process.waitFor(); |
| 158 | + } catch (IOException e) { |
| 159 | + e.printStackTrace(); |
| 160 | + } catch (InterruptedException e) { |
| 161 | + e.printStackTrace(); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments