From cdecc847576022954bf4f65a8f6052d92b4b33db Mon Sep 17 00:00:00 2001 From: lcm <1137909852@qq.com> Date: Fri, 13 Dec 2019 18:41:40 +0800 Subject: [PATCH 1/2] add async client usage --- README.md | 3 + .../example/DstAsyncUsageExample.java | 112 ++++++++++++++++++ .../dst/client/example/DstUsageExample.java | 1 + 3 files changed, 116 insertions(+) create mode 100644 client/src/main/java/com/distkv/dst/asyncclient/example/DstAsyncUsageExample.java diff --git a/README.md b/README.md index 6b59383b9..2945eabdc 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,9 @@ dst-cli > ok #### 3. Java Client SDK [Java Client SDK Example](https://github.com/dst-project/dst/blob/master/client/src/main/java/org/dst/client/example/DstUsageExample.java) +#### 4. Java Async Client SDK +[Java Async Client SDK Example](https://github.com/dst-project/dst/blob/master/client/src/main/java/org/dst/asyncclient/example/DstAsyncUsageExample) + ## Getting Involved Thank you for your attention to the `Dst` project. If you have any questions, you can create a new issue in our [Issues](https://github.com/dst-project/dst/issues) list. And we welcome you to participate in our `Dst` project, if you want to make some contributions, you can refer the file [CONTRIBUTING.md](https://github.com/dst-project/dst/blob/master/CONTRIBUTING.md). diff --git a/client/src/main/java/com/distkv/dst/asyncclient/example/DstAsyncUsageExample.java b/client/src/main/java/com/distkv/dst/asyncclient/example/DstAsyncUsageExample.java new file mode 100644 index 000000000..3f08df993 --- /dev/null +++ b/client/src/main/java/com/distkv/dst/asyncclient/example/DstAsyncUsageExample.java @@ -0,0 +1,112 @@ +package com.distkv.dst.asyncclient.example; + +import com.distkv.dst.asyncclient.DefaultAsyncClient; +import com.distkv.dst.rpc.protobuf.generated.DictProtocol; +import com.distkv.dst.rpc.protobuf.generated.ListProtocol; +import com.distkv.dst.rpc.protobuf.generated.SetProtocol; +import com.distkv.dst.rpc.protobuf.generated.StringProtocol; +import java.util.HashMap; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +public class DstAsyncUsageExample { + public static void main(String[] args) throws ExecutionException, InterruptedException { + DefaultAsyncClient dstClient = new DefaultAsyncClient("list://127.0.0.1:8082"); + if(dstClient.isConnected()) { + CompletableFuture strPutFuture = + dstClient.strs().put("k1","asd"); + CompletableFuture listPutFuture = + dstClient.lists().put("k1", Arrays.asList("v1","v2","v3")); + CompletableFuture setPutFuture = + dstClient.sets().put("k1", new HashSet<>(Arrays.asList("v1", "v2","v3"))); + Map map = new HashMap<>(); + map.put("k1", "v1"); + map.put("k2", "v2"); + map.put("k3", "v3"); + CompletableFuture dictPutFuture = + dstClient.dicts().put("dict", map); + + strPutFuture.whenComplete((r, t) -> { + if (t != null) { + throw new IllegalStateException(t); + } else { + System.out.println("String put completed."); + } + }); + listPutFuture.whenComplete((r, t) -> { + if (t != null) { + throw new IllegalStateException(t); + } else { + System.out.println("List put completed."); + } + }); + setPutFuture.whenComplete((r, t) -> { + if (t != null) { + throw new IllegalStateException(t); + } else { + System.out.println("Set put completed."); + } + }); + dictPutFuture.whenComplete((r, t) -> { + if (t != null) { + throw new IllegalStateException(t); + } else { + System.out.println("Dict put completed."); + } + }); + + strPutFuture.get(); + listPutFuture.get(); + setPutFuture.get(); + dictPutFuture.get(); + + CompletableFuture strGetFuture = + dstClient.strs().get("k1"); + CompletableFuture listGetFuture = + dstClient.lists().get("k1"); + CompletableFuture setGetFuture = + dstClient.sets().get("k1"); + CompletableFuture dictGetFuture = + dstClient.dicts().get("dict"); + + strGetFuture.whenComplete((r, t) -> { + if (t != null) { + throw new IllegalStateException(t); + } else { + System.out.println("The result of dstClient.strs().get(\"k1\") is: " + r.getValue()); + } + }); + listGetFuture.whenComplete((r, t) -> { + if (t != null) { + throw new IllegalStateException(t); + } else { + System.out.println("The result of dstClient.lists().get(\"k1\") is: " + r.getValuesList()); + } + }); + setGetFuture.whenComplete((r, t) -> { + if (t != null) { + throw new IllegalStateException(t); + } else { + System.out.println("The result of dstClient.sets().get(\"k1\") is: " + r.getValuesList()); + } + }); + dictGetFuture.whenComplete((r, t) -> { + if (t != null) { + throw new IllegalStateException(t); + } else { + System.out.println("The result of dstClient.dicts().get(\"dict1\") is: " + "\n" + r.getDict()); + } + }); + + strGetFuture.get(); + listGetFuture.get(); + setGetFuture.get(); + dictGetFuture.get(); + + dstClient.disconnect(); + } + } +} diff --git a/client/src/main/java/com/distkv/dst/client/example/DstUsageExample.java b/client/src/main/java/com/distkv/dst/client/example/DstUsageExample.java index dc03d80c6..4360262dd 100644 --- a/client/src/main/java/com/distkv/dst/client/example/DstUsageExample.java +++ b/client/src/main/java/com/distkv/dst/client/example/DstUsageExample.java @@ -40,6 +40,7 @@ public static void main(String[] args) { //print dictionary result System.out.println("The result of dstClient.dicts().get(\"dict1\") is: " + mapResult); + dstClient.disconnect(); } } } From db119886b7a45381b667b9cfa2aec180ba65cdd7 Mon Sep 17 00:00:00 2001 From: lcm <1137909852@qq.com> Date: Fri, 13 Dec 2019 21:11:03 +0800 Subject: [PATCH 2/2] fix --- README.md | 2 +- .../example/DstAsyncUsageExample.java | 46 ++++++++++--------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 2945eabdc..6b4171704 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ dst-cli > ok [Java Client SDK Example](https://github.com/dst-project/dst/blob/master/client/src/main/java/org/dst/client/example/DstUsageExample.java) #### 4. Java Async Client SDK -[Java Async Client SDK Example](https://github.com/dst-project/dst/blob/master/client/src/main/java/org/dst/asyncclient/example/DstAsyncUsageExample) +[Java Async Client SDK Example](https://github.com/dst-project/dst/blob/master/client/src/main/java/org/dst/asyncclient/example/DstAsyncUsageExample.java) ## Getting Involved Thank you for your attention to the `Dst` project. If you have any questions, you can create a new issue in our [Issues](https://github.com/dst-project/dst/issues) list. diff --git a/client/src/main/java/com/distkv/dst/asyncclient/example/DstAsyncUsageExample.java b/client/src/main/java/com/distkv/dst/asyncclient/example/DstAsyncUsageExample.java index 3f08df993..4b557d536 100644 --- a/client/src/main/java/com/distkv/dst/asyncclient/example/DstAsyncUsageExample.java +++ b/client/src/main/java/com/distkv/dst/asyncclient/example/DstAsyncUsageExample.java @@ -15,7 +15,7 @@ public class DstAsyncUsageExample { public static void main(String[] args) throws ExecutionException, InterruptedException { DefaultAsyncClient dstClient = new DefaultAsyncClient("list://127.0.0.1:8082"); - if(dstClient.isConnected()) { + if (dstClient.isConnected()) { CompletableFuture strPutFuture = dstClient.strs().put("k1","asd"); CompletableFuture listPutFuture = @@ -73,32 +73,36 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc dstClient.dicts().get("dict"); strGetFuture.whenComplete((r, t) -> { - if (t != null) { - throw new IllegalStateException(t); - } else { - System.out.println("The result of dstClient.strs().get(\"k1\") is: " + r.getValue()); - } + if (t != null) { + throw new IllegalStateException(t); + } else { + System.out.println("The result of dstClient.strs().get(\"k1\") is: " + + r.getValue()); + } }); listGetFuture.whenComplete((r, t) -> { - if (t != null) { - throw new IllegalStateException(t); - } else { - System.out.println("The result of dstClient.lists().get(\"k1\") is: " + r.getValuesList()); - } + if (t != null) { + throw new IllegalStateException(t); + } else { + System.out.println("The result of dstClient.lists().get(\"k1\") is: " + + r.getValuesList()); + } }); setGetFuture.whenComplete((r, t) -> { - if (t != null) { - throw new IllegalStateException(t); - } else { - System.out.println("The result of dstClient.sets().get(\"k1\") is: " + r.getValuesList()); - } + if (t != null) { + throw new IllegalStateException(t); + } else { + System.out.println("The result of dstClient.sets().get(\"k1\") is: " + + r.getValuesList()); + } }); dictGetFuture.whenComplete((r, t) -> { - if (t != null) { - throw new IllegalStateException(t); - } else { - System.out.println("The result of dstClient.dicts().get(\"dict1\") is: " + "\n" + r.getDict()); - } + if (t != null) { + throw new IllegalStateException(t); + } else { + System.out.println("The result of dstClient.dicts().get(\"dict1\") is: " + + "\n" + r.getDict()); + } }); strGetFuture.get();