diff --git a/examples/src/main/java/io/dapr/examples/state/README.md b/examples/src/main/java/io/dapr/examples/state/README.md index f0a7f6b119..5caeb61049 100644 --- a/examples/src/main/java/io/dapr/examples/state/README.md +++ b/examples/src/main/java/io/dapr/examples/state/README.md @@ -80,17 +80,30 @@ public class StateClient { retrievedMessagesMono.block().forEach(System.out::println); System.out.println("Deleting states..."); - + + System.out.println("Verify delete key request is aborted if an etag different from stored is passed."); // delete state API - Mono mono = client.deleteState(STATE_STORE_NAME, FIRST_KEY_NAME); - mono.block(); + try { + client.deleteState(STATE_STORE_NAME, FIRST_KEY_NAME, "100", null).block(); + } catch (DaprException ex) { + if (ex.getErrorCode().equals(Status.Code.ABORTED.toString())) { + // Expected error due to etag mismatch. + System.out.println(String.format("Expected failure. %s ", ex.getMessage())); + } else { + System.out.println("Unexpected exception."); + throw ex; + } + } + + System.out.println("Trying to delete again with correct etag."); + String storedEtag = client.getState(STATE_STORE_NAME, FIRST_KEY_NAME, MyClass.class).block().getEtag(); + client.deleteState(STATE_STORE_NAME, FIRST_KEY_NAME, storedEtag, null).block(); // Delete operation using transaction API operationList.clear(); operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.DELETE, new State<>(SECOND_KEY_NAME))); - mono = client.executeStateTransaction(STATE_STORE_NAME, operationList); - mono.block(); + client.executeStateTransaction(STATE_STORE_NAME, operationList).block(); Mono>> retrievedDeletedMessageMono = client.getStates(STATE_STORE_NAME, Arrays.asList(FIRST_KEY_NAME, SECOND_KEY_NAME), MyClass.class); @@ -112,7 +125,7 @@ This example performs multiple operations: * `client.getState(...)` operation in order to retrieve back the persisted state using the same key. * `client.executeStateTransaction(...)` operation in order to update existing state and add new state. * `client.getBulkState(...)` operation in order to retrieve back the persisted states using the same keys. -* `client.deleteState(...)` operation to remove one of the persisted states. +* `client.deleteState(...)` operation to remove one of the persisted states. An example of etag mismatch error if a different than current etag is added to request. * `client.executeStateTransaction(...)` operation in order to remove the other persisted state. Finally, the code tries to retrieve the deleted states, which should not be found. diff --git a/examples/src/main/java/io/dapr/examples/state/StateClient.java b/examples/src/main/java/io/dapr/examples/state/StateClient.java index c75f8e9ff9..b135d69226 100644 --- a/examples/src/main/java/io/dapr/examples/state/StateClient.java +++ b/examples/src/main/java/io/dapr/examples/state/StateClient.java @@ -9,6 +9,8 @@ import io.dapr.client.DaprClientBuilder; import io.dapr.client.domain.State; import io.dapr.client.domain.TransactionalStateOperation; +import io.dapr.exceptions.DaprException; +import io.grpc.Status; import reactor.core.publisher.Mono; import java.util.ArrayList; @@ -84,16 +86,28 @@ public static void main(String[] args) throws Exception { System.out.println("Deleting states..."); + System.out.println("Verify delete key request is aborted if an etag different from stored is passed."); // delete state API - Mono mono = client.deleteState(STATE_STORE_NAME, FIRST_KEY_NAME); - mono.block(); - + try { + client.deleteState(STATE_STORE_NAME, FIRST_KEY_NAME, "100", null).block(); + } catch (DaprException ex) { + if (ex.getErrorCode().equals(Status.Code.ABORTED.toString())) { + // Expected error due to etag mismatch. + System.out.println(String.format("Expected failure. %s ", ex.getMessage())); + } else { + System.out.println("Unexpected exception."); + throw ex; + } + } + + System.out.println("Trying to delete again with correct etag."); + String storedEtag = client.getState(STATE_STORE_NAME, FIRST_KEY_NAME, MyClass.class).block().getEtag(); + client.deleteState(STATE_STORE_NAME, FIRST_KEY_NAME, storedEtag, null).block(); // Delete operation using transaction API operationList.clear(); operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.DELETE, new State<>(SECOND_KEY_NAME))); - mono = client.executeStateTransaction(STATE_STORE_NAME, operationList); - mono.block(); + client.executeStateTransaction(STATE_STORE_NAME, operationList).block(); Mono>> retrievedDeletedMessageMono = client.getBulkState(STATE_STORE_NAME, Arrays.asList(FIRST_KEY_NAME, SECOND_KEY_NAME), MyClass.class); diff --git a/examples/src/main/resources/img/state.png b/examples/src/main/resources/img/state.png index f07ae1108a..852874abf0 100644 Binary files a/examples/src/main/resources/img/state.png and b/examples/src/main/resources/img/state.png differ diff --git a/sdk/src/main/java/io/dapr/client/domain/State.java b/sdk/src/main/java/io/dapr/client/domain/State.java index 7938d8c44e..2f7d5083a5 100644 --- a/sdk/src/main/java/io/dapr/client/domain/State.java +++ b/sdk/src/main/java/io/dapr/client/domain/State.java @@ -26,7 +26,7 @@ public class State { /** * The ETag to be used - * Keep in mind that for some state stores (like reids) only numbers are supported. + * Keep in mind that for some state stores (like redis) only numbers are supported. */ private final String etag;