-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* support distributed lock API Signed-off-by: crazyhzm <crazyhzm@gmail.com> * add Distributed Lock API Example Signed-off-by: crazyhzm <crazyhzm@gmail.com> * fix bash Signed-off-by: crazyhzm <crazyhzm@gmail.com> * fix bash Signed-off-by: crazyhzm <crazyhzm@gmail.com> * update dapr proto version Signed-off-by: crazyhzm <crazyhzm@gmail.com> * revert update dapr proto version Signed-off-by: crazyhzm <crazyhzm@gmail.com> * changed to json serializable Signed-off-by: crazyhzm <crazyhzm@gmail.com> * fix checkstyle Signed-off-by: crazyhzm <crazyhzm@gmail.com> * Fix compilation error due to Mono version change. Signed-off-by: Artur Souza <asouza.pro@gmail.com> --------- Signed-off-by: crazyhzm <crazyhzm@gmail.com> Signed-off-by: Artur Souza <asouza.pro@gmail.com> Co-authored-by: Artur Souza <asouza.pro@gmail.com>
- Loading branch information
1 parent
b442ba4
commit 86893a0
Showing
15 changed files
with
1,615 additions
and
736 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: lockstore | ||
spec: | ||
type: lock.redis | ||
version: v1 | ||
metadata: | ||
- name: redisHost | ||
value: localhost:6379 | ||
- name: redisPassword | ||
value: "" |
75 changes: 75 additions & 0 deletions
75
examples/src/main/java/io/dapr/examples/lock/grpc/DistributedLockGrpcClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* 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.lock.grpc; | ||
|
||
|
||
import io.dapr.client.DaprClientBuilder; | ||
import io.dapr.client.DaprPreviewClient; | ||
import io.dapr.client.domain.LockRequest; | ||
import io.dapr.client.domain.UnlockRequest; | ||
import io.dapr.client.domain.UnlockResponseStatus; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* DistributedLockGrpcClient. | ||
*/ | ||
public class DistributedLockGrpcClient { | ||
private static final String LOCK_STORE_NAME = "lockstore"; | ||
|
||
/** | ||
* 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..."); | ||
tryLock(client); | ||
unlock(client); | ||
} | ||
} | ||
|
||
/** | ||
* Trying to get lock. | ||
* | ||
* @param client DaprPreviewClient object | ||
*/ | ||
public static void tryLock(DaprPreviewClient client) { | ||
System.out.println("*******trying to get a free distributed lock********"); | ||
try { | ||
LockRequest lockRequest = new LockRequest(LOCK_STORE_NAME, "resouce1", "owner1", 5); | ||
Mono<Boolean> result = client.tryLock(lockRequest); | ||
System.out.println("Lock result -> " + (Boolean.TRUE.equals(result.block()) ? "SUCCESS" : "FAIL")); | ||
} catch (Exception ex) { | ||
System.out.println(ex.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* Unlock a lock. | ||
* | ||
* @param client DaprPreviewClient object | ||
*/ | ||
public static void unlock(DaprPreviewClient client) { | ||
System.out.println("*******unlock a distributed lock********"); | ||
try { | ||
UnlockRequest unlockRequest = new UnlockRequest(LOCK_STORE_NAME, "resouce1", "owner1"); | ||
Mono<UnlockResponseStatus> result = client.unlock(unlockRequest); | ||
System.out.println("Unlock result ->" + result.block().name()); | ||
} catch (Exception ex) { | ||
System.out.println(ex.getMessage()); | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
examples/src/main/java/io/dapr/examples/lock/grpc/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
## Distributed Lock API Example | ||
|
||
This example provides the different capabilities provided by Dapr Java SDK for Distributed Lock. For further information about Distributed Lock APIs please refer to [this link](https://docs.dapr.io/developing-applications/building-blocks/distributed-lock/) | ||
**This API is available in Preview Mode**. | ||
|
||
### Using the Distributed Lock API | ||
|
||
The java SDK exposes several methods for this - | ||
* `client.tryLock(...)` for getting a free distributed lock | ||
* `client.unlock(...)` for unlocking a lock | ||
|
||
## Pre-requisites | ||
|
||
* [Dapr and Dapr Cli](https://docs.dapr.io/getting-started/install-dapr/). | ||
* Java JDK 11 (or greater): | ||
* [Microsoft JDK 11](https://docs.microsoft.com/en-us/java/openjdk/download#openjdk-11) | ||
* [Oracle JDK 11](https://www.oracle.com/technetwork/java/javase/downloads/index.html#JDK11) | ||
* [OpenJDK 11](https://jdk.java.net/11/) | ||
* [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 | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
||
### Running the example | ||
|
||
Get into the examples' directory: | ||
```sh | ||
cd examples | ||
``` | ||
|
||
Use the following command to run this example- | ||
|
||
<!-- STEP | ||
name: Run DistributedLockGrpcClient example | ||
expected_stdout_lines: | ||
- "== APP == Using preview client..." | ||
- "== APP == *******trying to get a free distributed lock********" | ||
- "== APP == Lock result -> SUCCESS" | ||
- "== APP == *******unlock a distributed lock********" | ||
- "== APP == Unlock result -> SUCCESS" | ||
background: true | ||
sleep: 5 | ||
--> | ||
|
||
```bash | ||
dapr run --components-path ./components/lock --app-id lockgrpc --log-level debug -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.lock.grpc.DistributedLockGrpcClient | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
||
### Sample output | ||
``` | ||
== APP == Using preview client... | ||
== APP == *******trying to get a free distributed lock******** | ||
== APP == Lock result -> SUCCESS | ||
== APP == *******unlock a distributed lock******** | ||
== APP == Unlock result -> SUCCESS | ||
``` | ||
### Cleanup | ||
|
||
To stop the app, run (or press CTRL+C): | ||
|
||
<!-- STEP | ||
name: Cleanup | ||
--> | ||
|
||
```bash | ||
dapr stop --app-id lockgrpc | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
75 changes: 75 additions & 0 deletions
75
examples/src/main/java/io/dapr/examples/lock/http/DistributedLockHttpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* 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.lock.http; | ||
|
||
|
||
import io.dapr.client.DaprClientBuilder; | ||
import io.dapr.client.DaprPreviewClient; | ||
import io.dapr.client.domain.LockRequest; | ||
import io.dapr.client.domain.UnlockRequest; | ||
import io.dapr.client.domain.UnlockResponseStatus; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* DistributedLockGrpcClient. | ||
*/ | ||
public class DistributedLockHttpClient { | ||
private static final String LOCK_STORE_NAME = "lockstore"; | ||
|
||
/** | ||
* 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..."); | ||
tryLock(client); | ||
unlock(client); | ||
} | ||
} | ||
|
||
/** | ||
* Trying to get lock. | ||
* | ||
* @param client DaprPreviewClient object | ||
*/ | ||
public static void tryLock(DaprPreviewClient client) { | ||
System.out.println("*******trying to get a free distributed lock********"); | ||
try { | ||
LockRequest lockRequest = new LockRequest(LOCK_STORE_NAME, "resouce1", "owner1", 5); | ||
Mono<Boolean> result = client.tryLock(lockRequest); | ||
System.out.println("Lock result -> " + (Boolean.TRUE.equals(result.block()) ? "SUCCESS" : "FAIL")); | ||
} catch (Exception ex) { | ||
System.out.println(ex.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* Unlock a lock. | ||
* | ||
* @param client DaprPreviewClient object | ||
*/ | ||
public static void unlock(DaprPreviewClient client) { | ||
System.out.println("*******unlock a distributed lock********"); | ||
try { | ||
UnlockRequest unlockRequest = new UnlockRequest(LOCK_STORE_NAME, "resouce1", "owner1"); | ||
Mono<UnlockResponseStatus> result = client.unlock(unlockRequest); | ||
System.out.println("Unlock result ->" + result.block().name()); | ||
} catch (Exception ex) { | ||
System.out.println(ex.getMessage()); | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
examples/src/main/java/io/dapr/examples/lock/http/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
## Distributed Lock API Example | ||
|
||
This example provides the different capabilities provided by Dapr Java SDK for Distributed Lock. For further information about Distributed Lock APIs please refer to [this link](https://docs.dapr.io/developing-applications/building-blocks/distributed-lock/) | ||
**This API is available in Preview Mode**. | ||
|
||
### Using the Distributed Lock API | ||
|
||
The java SDK exposes several methods for this - | ||
* `client.tryLock(...)` for getting a free distributed lock | ||
* `client.unlock(...)` for unlocking a lock | ||
|
||
## Pre-requisites | ||
|
||
* [Dapr and Dapr Cli](https://docs.dapr.io/getting-started/install-dapr/). | ||
* Java JDK 11 (or greater): | ||
* [Microsoft JDK 11](https://docs.microsoft.com/en-us/java/openjdk/download#openjdk-11) | ||
* [Oracle JDK 11](https://www.oracle.com/technetwork/java/javase/downloads/index.html#JDK11) | ||
* [OpenJDK 11](https://jdk.java.net/11/) | ||
* [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 | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
||
### Running the example | ||
|
||
Get into the examples' directory: | ||
```sh | ||
cd examples | ||
``` | ||
|
||
Use the following command to run this example- | ||
|
||
<!-- STEP | ||
name: Run DistributedLockHttpClient example | ||
expected_stdout_lines: | ||
- "== APP == Using preview client..." | ||
- "== APP == *******trying to get a free distributed lock********" | ||
- "== APP == Lock result -> SUCCESS" | ||
- "== APP == *******unlock a distributed lock********" | ||
- "== APP == Unlock result -> SUCCESS" | ||
background: true | ||
sleep: 5 | ||
--> | ||
|
||
```bash | ||
dapr run --components-path ./components/lock --app-id lockhttp --log-level debug -- java -Ddapr.api.protocol=HTTP -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.lock.http.DistributedLockHttpClient | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
||
### Sample output | ||
``` | ||
== APP == Using preview client... | ||
== APP == *******trying to get a free distributed lock******** | ||
== APP == Lock result -> SUCCESS | ||
== APP == *******unlock a distributed lock******** | ||
== APP == Unlock result -> SUCCESS | ||
``` | ||
### Cleanup | ||
|
||
To stop the app, run (or press CTRL+C): | ||
|
||
<!-- STEP | ||
name: Cleanup | ||
--> | ||
|
||
```bash | ||
dapr stop --app-id lockhttp | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.