Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an async client example. #333

Merged
merged 3 commits into from
Dec 13, 2019
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.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.
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).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
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<StringProtocol.PutResponse> strPutFuture =
dstClient.strs().put("k1","asd");
CompletableFuture<ListProtocol.PutResponse> listPutFuture =
dstClient.lists().put("k1", Arrays.asList("v1","v2","v3"));
CompletableFuture<SetProtocol.PutResponse> setPutFuture =
dstClient.sets().put("k1", new HashSet<>(Arrays.asList("v1", "v2","v3")));
Map<String, String> map = new HashMap<>();
map.put("k1", "v1");
map.put("k2", "v2");
map.put("k3", "v3");
CompletableFuture<DictProtocol.PutResponse> 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<StringProtocol.GetResponse> strGetFuture =
dstClient.strs().get("k1");
CompletableFuture<ListProtocol.GetResponse> listGetFuture =
dstClient.lists().get("k1");
CompletableFuture<SetProtocol.GetResponse> setGetFuture =
dstClient.sets().get("k1");
CompletableFuture<DictProtocol.GetResponse> 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}