Skip to content
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
25 changes: 19 additions & 6 deletions examples/src/main/java/io/dapr/examples/state/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> 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<List<State<MyClass>>> retrievedDeletedMessageMono = client.getStates(STATE_STORE_NAME,
Arrays.asList(FIRST_KEY_NAME, SECOND_KEY_NAME), MyClass.class);
Expand All @@ -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.
Expand Down
24 changes: 19 additions & 5 deletions examples/src/main/java/io/dapr/examples/state/StateClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Void> 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<List<State<MyClass>>> retrievedDeletedMessageMono = client.getBulkState(STATE_STORE_NAME,
Arrays.asList(FIRST_KEY_NAME, SECOND_KEY_NAME), MyClass.class);
Expand Down
Binary file modified examples/src/main/resources/img/state.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion sdk/src/main/java/io/dapr/client/domain/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class State<T> {

/**
* 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;

Expand Down