diff --git a/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-outbox.md b/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-outbox.md index d674a67063f..01762e53c4e 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-outbox.md +++ b/daprdocs/content/en/developing-applications/building-blocks/state-management/howto-outbox.md @@ -322,30 +322,45 @@ public class Main { public static void main(String[] args) { try (DaprClient client = new DaprClientBuilder().build()) { // Define the first state operation to save the value "2" - StateOperation op1 = new StateOperation<>( - StateOperationType.UPSERT, + State state1 = new State<>( "key1", - "2" + "2", + null, // etag + null // concurrency and consistency options ); // Define the second state operation to publish the value "3" with metadata Map metadata = new HashMap<>(); metadata.put("outbox.projection", "true"); - StateOperation op2 = new StateOperation<>( - StateOperationType.UPSERT, + State state2 = new State<>( "key1", "3", - metadata + null, // etag + metadata, + null // concurrency and consistency options + ); + + TransactionalStateOperation op1 = new TransactionalStateOperation<>( + TransactionalStateOperation.OperationType.UPSERT, state1 + ); + + TransactionalStateOperation op2 = new TransactionalStateOperation<>( + TransactionalStateOperation.OperationType.UPSERT, state2 ); - // Create the list of state operations - List> ops = new ArrayList<>(); + // Create the list of transaction state operations + List> ops = new ArrayList<>(); ops.add(op1); ops.add(op2); + // Configure transaction request setting the state store + ExecuteStateTransactionRequest transactionRequest = new ExecuteStateTransactionRequest(DAPR_STORE_NAME); + + transactionRequest.setOperations(ops); + // Execute the state transaction - client.executeStateTransaction(DAPR_STORE_NAME, ops).block(); + client.executeStateTransaction(transactionRequest).block(); System.out.println("State transaction executed."); } catch (Exception e) { e.printStackTrace(); @@ -595,39 +610,42 @@ public class StateOperationExample { executeStateTransaction(); } - public static void executeStateTransaction() { - // Build Dapr client - try (DaprClient daprClient = new DaprClientBuilder().build()) { - - // Define the value "2" - String value = "2"; - - // Override CloudEvent metadata - Map metadata = new HashMap<>(); - metadata.put("cloudevent.id", "unique-business-process-id"); - metadata.put("cloudevent.source", "CustomersApp"); - metadata.put("cloudevent.type", "CustomerCreated"); - metadata.put("cloudevent.subject", "123"); - metadata.put("my-custom-ce-field", "abc"); - - // Define state operations - List> ops = new ArrayList<>(); - StateOperation op1 = new StateOperation<>( - StateOperationType.UPSERT, - "key1", - value, - metadata - ); - ops.add(op1); - - // Execute state transaction - String storeName = "your-state-store-name"; - daprClient.executeStateTransaction(storeName, ops).block(); - System.out.println("State transaction executed."); - } catch (Exception e) { - e.printStackTrace(); - } + public static void executeStateTransaction() { + // Build Dapr client + try (DaprClient daprClient = new DaprClientBuilder().build()) { + + // Override CloudEvent metadata + Map metadata = new HashMap<>(); + metadata.put("cloudevent.id", "unique-business-process-id"); + metadata.put("cloudevent.source", "CustomersApp"); + metadata.put("cloudevent.type", "CustomerCreated"); + metadata.put("cloudevent.subject", "123"); + metadata.put("my-custom-ce-field", "abc"); + + State state = new State<>( + "key1", // Define the key "key1" + "value1", // Define the value "value1" + null, // etag + metadata, + null // concurrency and consistency options + ); + + // Define state operations + List> ops = new ArrayList<>(); + TransactionalStateOperation op1 = new TransactionalStateOperation<>( + TransactionalStateOperation.OperationType.UPSERT, + state + ); + ops.add(op1); + + // Execute state transaction + String storeName = "your-state-store-name"; + daprClient.executeStateTransaction(storeName, ops).block(); + System.out.println("State transaction executed."); + } catch (Exception e) { + e.printStackTrace(); } + } } ``` {{% /tab %}}