@@ -322,30 +322,45 @@ public class Main {
322322 public static void main(String[] args) {
323323 try (DaprClient client = new DaprClientBuilder().build()) {
324324 // Define the first state operation to save the value "2"
325- StateOperation<String> op1 = new StateOperation<>(
326- StateOperationType.UPSERT,
325+ State<String> state1 = new State<>(
327326 "key1",
328- "2"
327+ "2",
328+ null, // etag
329+ null // concurrency and consistency options
329330 );
330331
331332 // Define the second state operation to publish the value "3" with metadata
332333 Map<String, String> metadata = new HashMap<>();
333334 metadata.put("outbox.projection", "true");
334335
335- StateOperation<String> op2 = new StateOperation<>(
336- StateOperationType.UPSERT,
336+ State<String> state2 = new State<>(
337337 "key1",
338338 "3",
339- metadata
339+ null, // etag
340+ metadata,
341+ null // concurrency and consistency options
342+ );
343+
344+ TransactionalStateOperation<String> op1 = new TransactionalStateOperation<>(
345+ TransactionalStateOperation.OperationType.UPSERT, state1
346+ );
347+
348+ TransactionalStateOperation<String> op2 = new TransactionalStateOperation<>(
349+ TransactionalStateOperation.OperationType.UPSERT, state2
340350 );
341351
342- // Create the list of state operations
343- List<StateOperation <?>> ops = new ArrayList<>();
352+ // Create the list of transaction state operations
353+ List<TransactionalStateOperation <?>> ops = new ArrayList<>();
344354 ops.add(op1);
345355 ops.add(op2);
346356
357+ // Configure transaction request setting the state store
358+ ExecuteStateTransactionRequest transactionRequest = new ExecuteStateTransactionRequest(DAPR_STORE_NAME);
359+
360+ transactionRequest.setOperations(ops);
361+
347362 // Execute the state transaction
348- client.executeStateTransaction(DAPR_STORE_NAME, ops ).block();
363+ client.executeStateTransaction(transactionRequest ).block();
349364 System.out.println("State transaction executed.");
350365 } catch (Exception e) {
351366 e.printStackTrace();
@@ -595,39 +610,42 @@ public class StateOperationExample {
595610 executeStateTransaction();
596611 }
597612
598- public static void executeStateTransaction() {
599- // Build Dapr client
600- try (DaprClient daprClient = new DaprClientBuilder().build()) {
601-
602- // Define the value "2"
603- String value = "2";
604-
605- // Override CloudEvent metadata
606- Map<String, String> metadata = new HashMap<>();
607- metadata.put("cloudevent.id", "unique-business-process-id");
608- metadata.put("cloudevent.source", "CustomersApp");
609- metadata.put("cloudevent.type", "CustomerCreated");
610- metadata.put("cloudevent.subject", "123");
611- metadata.put("my-custom-ce-field", "abc");
612-
613- // Define state operations
614- List<StateOperation<?>> ops = new ArrayList<>();
615- StateOperation<String> op1 = new StateOperation<>(
616- StateOperationType.UPSERT,
617- "key1",
618- value,
619- metadata
620- );
621- ops.add(op1);
622-
623- // Execute state transaction
624- String storeName = "your-state-store-name";
625- daprClient.executeStateTransaction(storeName, ops).block();
626- System.out.println("State transaction executed.");
627- } catch (Exception e) {
628- e.printStackTrace();
629- }
613+ public static void executeStateTransaction() {
614+ // Build Dapr client
615+ try (DaprClient daprClient = new DaprClientBuilder().build()) {
616+
617+ // Override CloudEvent metadata
618+ Map<String, String> metadata = new HashMap<>();
619+ metadata.put("cloudevent.id", "unique-business-process-id");
620+ metadata.put("cloudevent.source", "CustomersApp");
621+ metadata.put("cloudevent.type", "CustomerCreated");
622+ metadata.put("cloudevent.subject", "123");
623+ metadata.put("my-custom-ce-field", "abc");
624+
625+ State<String> state = new State<>(
626+ "key1", // Define the key "key1"
627+ "value1", // Define the value "value1"
628+ null, // etag
629+ metadata,
630+ null // concurrency and consistency options
631+ );
632+
633+ // Define state operations
634+ List<TransactionalStateOperation<?>> ops = new ArrayList<>();
635+ TransactionalStateOperation<String> op1 = new TransactionalStateOperation<>(
636+ TransactionalStateOperation.OperationType.UPSERT,
637+ state
638+ );
639+ ops.add(op1);
640+
641+ // Execute state transaction
642+ String storeName = "your-state-store-name";
643+ daprClient.executeStateTransaction(storeName, ops).block();
644+ System.out.println("State transaction executed.");
645+ } catch (Exception e) {
646+ e.printStackTrace();
630647 }
648+ }
631649}
632650` ` `
633651{{% /tab %}}
0 commit comments