Added full code snippets of state management

Signed-off-by: Amulya Varote <amulyavarote@Amulyas-MacBook-Pro.local>
This commit is contained in:
Amulya Varote 2021-12-05 07:43:13 -08:00
parent 08048e58d7
commit 1f879395a9
1 changed files with 194 additions and 152 deletions

View File

@ -81,7 +81,15 @@ Below are code examples that leverage Dapr SDKs for saving and retrieving a sing
```csharp ```csharp
//dependencies //dependencies
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Dapr.Client; using Dapr.Client;
using Microsoft.AspNetCore.Mvc;
using System.Threading;
using System.Text.Json;
//code //code
namespace EventService namespace EventService
@ -91,17 +99,20 @@ namespace EventService
static async Task Main(string[] args) static async Task Main(string[] args)
{ {
string DAPR_STORE_NAME = "statestore"; string DAPR_STORE_NAME = "statestore";
while(true) {
int orderId = 100; System.Threading.Thread.Sleep(5000);
//Using Dapr SDK to save and get state using var client = new DaprClientBuilder().Build();
using var client = new DaprClientBuilder().Build(); Random random = new Random();
await client.SaveStateAsync(DAPR_STORE_NAME, "order_1", orderId.ToString()); int orderId = random.Next(1,1000);
await client.SaveStateAsync(DAPR_STORE_NAME, "order_2", orderId.ToString()); //Using Dapr SDK to save and get state
var result = await client.GetStateAsync<string>(DAPR_STORE_NAME, orderId.ToString()); await client.SaveStateAsync(DAPR_STORE_NAME, "order_1", orderId.ToString());
await client.SaveStateAsync(DAPR_STORE_NAME, "order_2", orderId.ToString());
var result = await client.GetStateAsync<string>(DAPR_STORE_NAME, orderId.ToString());
Console.WriteLine("Result after get: " + result);
}
} }
} }
} }
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
@ -116,12 +127,17 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
{{% codetab %}} {{% codetab %}}
```java ```java
//dependencies //dependencies
import io.dapr.client.DaprClient; import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder; import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.State; import io.dapr.client.domain.State;
import io.dapr.client.domain.TransactionalStateOperation;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.util.Random;
import java.util.concurrent.TimeUnit;
//code //code
@SpringBootApplication @SpringBootApplication
@ -129,18 +145,23 @@ public class OrderProcessingServiceApplication {
private static final Logger log = LoggerFactory.getLogger(OrderProcessingServiceApplication.class); private static final Logger log = LoggerFactory.getLogger(OrderProcessingServiceApplication.class);
public static void main(String[] args) throws InterruptedException { private static final String STATE_STORE_NAME = "statestore";
String STATE_STORE_NAME = "statestore";
int orderId = 100; public static void main(String[] args) throws InterruptedException{
//Using Dapr SDK to save and get state while(true) {
DaprClient client = new DaprClientBuilder().build(); TimeUnit.MILLISECONDS.sleep(5000);
client.saveState(STATE_STORE_NAME, "order_1", Integer.toString(orderId)).block(); Random random = new Random();
client.saveState(STATE_STORE_NAME, "order_2", Integer.toString(orderId)).block(); int orderId = random.nextInt(1000-1) + 1;
Mono<State<String>> result = client.getState(STATE_STORE_NAME, "order_1", String.class); DaprClient client = new DaprClientBuilder().build();
//Using Dapr SDK to save and get state
client.saveState(STATE_STORE_NAME, "order_1", Integer.toString(orderId)).block();
client.saveState(STATE_STORE_NAME, "order_2", Integer.toString(orderId)).block();
Mono<State<String>> result = client.getState(STATE_STORE_NAME, "order_1", String.class);
log.info("Result after get" + result);
}
} }
}
}
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
@ -155,22 +176,26 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
{{% codetab %}} {{% codetab %}}
```python ```python
#dependencies #dependencies
import random
from time import sleep
import requests
import logging
from dapr.clients import DaprClient from dapr.clients import DaprClient
from dapr.clients.grpc._state import StateItem
from dapr.clients.grpc._request import TransactionalStateOperation, TransactionOperationType
#code #code
logging.basicConfig(level = logging.INFO) logging.basicConfig(level = logging.INFO)
DAPR_STORE_NAME = "statestore" DAPR_STORE_NAME = "statestore"
while True:
orderId = 100 sleep(random.randrange(50, 5000) / 1000)
#Using Dapr SDK to save and get state orderId = random.randint(1, 1000)
with DaprClient() as client: with DaprClient() as client:
client.save_state(DAPR_STORE_NAME, "order_1", str(orderId)) #Using Dapr SDK to save and get state
result = client.get_state(DAPR_STORE_NAME, "order_1") client.save_state(DAPR_STORE_NAME, "order_1", str(orderId))
logging.info('Result after get: ' + str(result)) result = client.get_state(DAPR_STORE_NAME, "order_1")
logging.info('Result after get: ' + str(result))
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
@ -185,7 +210,6 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
{{% codetab %}} {{% codetab %}}
```go ```go
//dependencies //dependencies
import ( import (
"context" "context"
@ -194,34 +218,32 @@ import (
"time" "time"
"strconv" "strconv"
dapr "github.com/dapr/go-sdk/client" dapr "github.com/dapr/go-sdk/client"
) )
//code //code
func main() { func main() {
for i := 0; i < 10; i++ {
STATE_STORE_NAME := "statestore" time.Sleep(5000)
orderId := rand.Intn(1000-1) + 1
orderId := 100 client, err := dapr.NewClient()
STATE_STORE_NAME := "statestore"
//Using Dapr SDK to save and get state if err != nil {
client, err := dapr.NewClient() panic(err)
if err != nil { }
panic(err) defer client.Close()
} ctx := context.Background()
defer client.Close() //Using Dapr SDK to save and get state
ctx := context.Background() if err := client.SaveState(ctx, STATE_STORE_NAME, "order_1", []byte(strconv.Itoa(orderId))); err != nil {
panic(err)
if err := client.SaveState(ctx, STATE_STORE_NAME, "order_1", []byte(strconv.Itoa(orderId))); err != nil { }
panic(err) result, err := client.GetState(ctx, STATE_STORE_NAME, "order_2")
} if err != nil {
panic(err)
result, err := client.GetState(ctx, STATE_STORE_NAME, "order_1") }
if err != nil { log.Println("Result after get: ")
panic(err) log.Println(result)
} }
} }
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
@ -236,19 +258,26 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
{{% codetab %}} {{% codetab %}}
```javascript ```javascript
//dependencies //dependencies
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client'; import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
//code //code
const daprHost = "127.0.0.1"; const daprHost = "127.0.0.1";
var main = function() { var main = function() {
const STATE_STORE_NAME = "statestore"; for(var i=0;i<10;i++) {
sleep(5000);
var orderId = Math.floor(Math.random() * (1000 - 1) + 1);
start(orderId).catch((e) => {
console.error(e);
process.exit(1);
});
}
}
var orderId = 100; async function start(orderId) {
//Using Dapr SDK to save and get state
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
const STATE_STORE_NAME = "statestore";
//Using Dapr SDK to save and get state
await client.state.save(STATE_STORE_NAME, [ await client.state.save(STATE_STORE_NAME, [
{ {
key: "order_1", key: "order_1",
@ -260,10 +289,14 @@ var main = function() {
} }
]); ]);
var result = await client.state.get(STATE_STORE_NAME, "order_1"); var result = await client.state.get(STATE_STORE_NAME, "order_1");
console.log("Result after get: " + result);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
} }
main(); main();
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
@ -329,7 +362,6 @@ Below are code examples that leverage Dapr SDKs for deleting the state.
{{% codetab %}} {{% codetab %}}
```csharp ```csharp
//dependencies //dependencies
using Dapr.Client; using Dapr.Client;
@ -347,7 +379,6 @@ namespace EventService
} }
} }
} }
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
@ -362,10 +393,10 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
{{% codetab %}} {{% codetab %}}
```java ```java
//dependencies //dependencies
import io.dapr.client.DaprClient; import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder; import io.dapr.client.DaprClientBuilder;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//code //code
@SpringBootApplication @SpringBootApplication
@ -379,7 +410,6 @@ public class OrderProcessingServiceApplication {
client.deleteState(STATE_STORE_NAME, "order_1", storedEtag, null).block(); client.deleteState(STATE_STORE_NAME, "order_1", storedEtag, null).block();
} }
} }
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
@ -394,19 +424,16 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
{{% codetab %}} {{% codetab %}}
```python ```python
#dependencies #dependencies
from dapr.clients.grpc._request import TransactionalStateOperation, TransactionOperationType from dapr.clients.grpc._request import TransactionalStateOperation, TransactionOperationType
#code #code
logging.basicConfig(level = logging.INFO) logging.basicConfig(level = logging.INFO)
DAPR_STORE_NAME = "statestore" DAPR_STORE_NAME = "statestore"
#Using Dapr SDK to delete the state #Using Dapr SDK to delete the state
with DaprClient() as client: with DaprClient() as client:
client.delete_state(store_name=DAPR_STORE_NAME, key="order_1") client.delete_state(store_name=DAPR_STORE_NAME, key="order_1")
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
@ -421,7 +448,6 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
{{% codetab %}} {{% codetab %}}
```go ```go
//dependencies //dependencies
import ( import (
"context" "context"
@ -431,9 +457,7 @@ import (
//code //code
func main() { func main() {
STATE_STORE_NAME := "statestore" STATE_STORE_NAME := "statestore"
//Using Dapr SDK to delete the state //Using Dapr SDK to delete the state
client, err := dapr.NewClient() client, err := dapr.NewClient()
if err != nil { if err != nil {
@ -446,7 +470,6 @@ func main() {
panic(err) panic(err)
} }
} }
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
@ -461,23 +484,19 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
{{% codetab %}} {{% codetab %}}
```javascript ```javascript
//dependencies //dependencies
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client'; import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
//code //code
const daprHost = "127.0.0.1"; const daprHost = "127.0.0.1";
var main = function() { var main = function() {
const STATE_STORE_NAME = "statestore"; const STATE_STORE_NAME = "statestore";
//Using Dapr SDK to save and get state //Using Dapr SDK to save and get state
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
await client.state.delete(STATE_STORE_NAME, "order_1"); await client.state.delete(STATE_STORE_NAME, "order_1");
} }
main(); main();
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
@ -515,11 +534,11 @@ Below are code examples that leverage Dapr SDKs for saving and retrieving multip
{{% codetab %}} {{% codetab %}}
```java ```java
//dependencies //dependencies
import io.dapr.client.DaprClient; import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder; import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.State; import io.dapr.client.domain.State;
import java.util.Arrays;
//code //code
@SpringBootApplication @SpringBootApplication
@ -529,15 +548,12 @@ public class OrderProcessingServiceApplication {
public static void main(String[] args) throws InterruptedException{ public static void main(String[] args) throws InterruptedException{
String STATE_STORE_NAME = "statestore"; String STATE_STORE_NAME = "statestore";
int orderId = 100;
//Using Dapr SDK to retrieve multiple states //Using Dapr SDK to retrieve multiple states
DaprClient client = new DaprClientBuilder().build(); DaprClient client = new DaprClientBuilder().build();
Mono<List<State<String>>> resultBulk = client.getBulkState(STATE_STORE_NAME, Mono<List<State<String>>> resultBulk = client.getBulkState(STATE_STORE_NAME,
Arrays.asList("order_1", "order_2"), String.class); Arrays.asList("order_1", "order_2"), String.class);
} }
} }
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
@ -548,27 +564,22 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
{{% /codetab %}} {{% /codetab %}}
{{% codetab %}} {{% codetab %}}
```python ```python
#dependencies #dependencies
from dapr.clients import DaprClient from dapr.clients import DaprClient
from dapr.clients.grpc._state import StateItem from dapr.clients.grpc._state import StateItem
#code #code
logging.basicConfig(level = logging.INFO) logging.basicConfig(level = logging.INFO)
DAPR_STORE_NAME = "statestore" DAPR_STORE_NAME = "statestore"
orderId = 100 orderId = 100
#Using Dapr SDK to save and retrieve multiple states #Using Dapr SDK to save and retrieve multiple states
with DaprClient() as client: with DaprClient() as client:
client.save_bulk_state(store_name=DAPR_STORE_NAME, states=[StateItem(key="order_2", value=str(orderId))]) client.save_bulk_state(store_name=DAPR_STORE_NAME, states=[StateItem(key="order_2", value=str(orderId))])
result = client.get_bulk_state(store_name=DAPR_STORE_NAME, keys=["order_1", "order_2"], states_metadata={"metakey": "metavalue"}).items result = client.get_bulk_state(store_name=DAPR_STORE_NAME, keys=["order_1", "order_2"], states_metadata={"metakey": "metavalue"}).items
logging.info('Result after get bulk: ' + str(result)) logging.info('Result after get bulk: ' + str(result))
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
@ -579,21 +590,16 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
{{% /codetab %}} {{% /codetab %}}
{{% codetab %}} {{% codetab %}}
```javascript ```javascript
//dependencies //dependencies
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client'; import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
//code //code
const daprHost = "127.0.0.1"; const daprHost = "127.0.0.1";
var main = function() { var main = function() {
const STATE_STORE_NAME = "statestore"; const STATE_STORE_NAME = "statestore";
var orderId = 100; var orderId = 100;
//Using Dapr SDK to save and retrieve multiple states //Using Dapr SDK to save and retrieve multiple states
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
@ -611,7 +617,6 @@ var main = function() {
} }
main(); main();
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
@ -662,9 +667,16 @@ Below are code examples that leverage Dapr SDKs for performing state transaction
{{% codetab %}} {{% codetab %}}
```csharp ```csharp
//dependencies //dependencies
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Dapr.Client; using Dapr.Client;
using Microsoft.AspNetCore.Mvc;
using System.Threading;
using System.Text.Json;
//code //code
namespace EventService namespace EventService
@ -674,22 +686,26 @@ namespace EventService
static async Task Main(string[] args) static async Task Main(string[] args)
{ {
string DAPR_STORE_NAME = "statestore"; string DAPR_STORE_NAME = "statestore";
while(true) {
int orderId = 100; System.Threading.Thread.Sleep(5000);
//Using Dapr SDK to perform the state transactions Random random = new Random();
using var client = new DaprClientBuilder().Build(); int orderId = random.Next(1,1000);
var requests = new List<StateTransactionRequest>() using var client = new DaprClientBuilder().Build();
{ var requests = new List<StateTransactionRequest>()
new StateTransactionRequest("order_3", JsonSerializer.SerializeToUtf8Bytes(orderId.ToString()), StateOperationType.Upsert), {
new StateTransactionRequest("order_2", null, StateOperationType.Delete) new StateTransactionRequest("order_3", JsonSerializer.SerializeToUtf8Bytes(orderId.ToString()), StateOperationType.Upsert),
}; new StateTransactionRequest("order_2", null, StateOperationType.Delete)
CancellationTokenSource source = new CancellationTokenSource(); };
CancellationToken cancellationToken = source.Token; CancellationTokenSource source = new CancellationTokenSource();
await client.ExecuteStateTransactionAsync(DAPR_STORE_NAME, requests, cancellationToken: cancellationToken); CancellationToken cancellationToken = source.Token;
//Using Dapr SDK to perform the state transactions
await client.ExecuteStateTransactionAsync(DAPR_STORE_NAME, requests, cancellationToken: cancellationToken);
Console.WriteLine("Order requested: " + orderId);
Console.WriteLine("Result: " + result);
}
} }
} }
} }
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
@ -704,13 +720,19 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
{{% codetab %}} {{% codetab %}}
```java ```java
//dependencies //dependencies
import io.dapr.client.DaprClient; import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder; import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.State; import io.dapr.client.domain.State;
import io.dapr.client.domain.TransactionalStateOperation; import io.dapr.client.domain.TransactionalStateOperation;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
//code //code
@SpringBootApplication @SpringBootApplication
@ -718,21 +740,26 @@ public class OrderProcessingServiceApplication {
private static final Logger log = LoggerFactory.getLogger(OrderProcessingServiceApplication.class); private static final Logger log = LoggerFactory.getLogger(OrderProcessingServiceApplication.class);
private static final String STATE_STORE_NAME = "statestore";
public static void main(String[] args) throws InterruptedException{ public static void main(String[] args) throws InterruptedException{
String STATE_STORE_NAME = "statestore"; while(true) {
TimeUnit.MILLISECONDS.sleep(5000);
int orderId = 100; Random random = new Random();
//Using Dapr SDK to perform the state transactions int orderId = random.nextInt(1000-1) + 1;
DaprClient client = new DaprClientBuilder().build(); DaprClient client = new DaprClientBuilder().build();
List<TransactionalStateOperation<?>> operationList = new ArrayList<>(); List<TransactionalStateOperation<?>> operationList = new ArrayList<>();
operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.UPSERT, operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.UPSERT,
new State<>("order_3", Integer.toString(orderId), ""))); new State<>("order_3", Integer.toString(orderId), "")));
operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.DELETE, operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.DELETE,
new State<>("order_2"))); new State<>("order_2")));
client.executeStateTransaction(STATE_STORE_NAME, operationList).block(); //Using Dapr SDK to perform the state transactions
client.executeStateTransaction(STATE_STORE_NAME, operationList).block();
log.info("Order requested: " + orderId);
}
} }
}
}
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
@ -743,37 +770,42 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
{{% /codetab %}} {{% /codetab %}}
{{% codetab %}} {{% codetab %}}
```python ```python
#dependencies #dependencies
import random
from time import sleep
import requests
import logging
from dapr.clients import DaprClient from dapr.clients import DaprClient
from dapr.clients.grpc._state import StateItem from dapr.clients.grpc._state import StateItem
from dapr.clients.grpc._request import TransactionalStateOperation, TransactionOperationType from dapr.clients.grpc._request import TransactionalStateOperation, TransactionOperationType
#code #code
logging.basicConfig(level = logging.INFO) logging.basicConfig(level = logging.INFO)
DAPR_STORE_NAME = "statestore" DAPR_STORE_NAME = "statestore"
while True:
sleep(random.randrange(50, 5000) / 1000)
orderId = random.randint(1, 1000)
with DaprClient() as client:
#Using Dapr SDK to perform the state transactions
client.execute_state_transaction(store_name=DAPR_STORE_NAME, operations=[
TransactionalStateOperation(
operation_type=TransactionOperationType.upsert,
key="order_3",
data=str(orderId)),
TransactionalStateOperation(key="order_3", data=str(orderId)),
TransactionalStateOperation(
operation_type=TransactionOperationType.delete,
key="order_2",
data=str(orderId)),
TransactionalStateOperation(key="order_2", data=str(orderId))
])
orderId = 100 client.delete_state(store_name=DAPR_STORE_NAME, key="order_1")
#Using Dapr SDK to perform the state transactions logging.basicConfig(level = logging.INFO)
with DaprClient() as client: logging.info('Order requested: ' + str(orderId))
client.execute_state_transaction(store_name=DAPR_STORE_NAME, operations=[ logging.info('Result: ' + str(result))
TransactionalStateOperation(
operation_type=TransactionOperationType.upsert,
key="order_3",
data=str(orderId)),
TransactionalStateOperation(key="order_3", data=str(orderId)),
TransactionalStateOperation(
operation_type=TransactionOperationType.delete,
key="order_2",
data=str(orderId)),
TransactionalStateOperation(key="order_2", data=str(orderId))
])
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application:
@ -788,38 +820,48 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g
{{% codetab %}} {{% codetab %}}
```javascript ```javascript
//dependencies //dependencies
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client'; import { DaprClient, HttpMethod, CommunicationProtocolEnum } from 'dapr-client';
//code //code
const daprHost = "127.0.0.1"; const daprHost = "127.0.0.1";
var main = function() { var main = function() {
const STATE_STORE_NAME = "statestore"; for(var i=0;i<10;i++) {
sleep(5000);
var orderId = Math.floor(Math.random() * (1000 - 1) + 1);
start(orderId).catch((e) => {
console.error(e);
process.exit(1);
});
}
}
var orderId = 100; async function start(orderId) {
//Using Dapr SDK to save and retrieve multiple states
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP); const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
const STATE_STORE_NAME = "statestore";
//Using Dapr SDK to save and retrieve multiple states
await client.state.transaction(STATE_STORE_NAME, [ await client.state.transaction(STATE_STORE_NAME, [
{ {
operation: "upsert", operation: "upsert",
request: { request: {
key: "order_3", key: "order_3",
value: orderId.toString() value: orderId.toString()
} }
}, },
{ {
operation: "delete", operation: "delete",
request: { request: {
key: "order_2" key: "order_2"
} }
} }
]); ]);
} }
main(); function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
main();
``` ```
Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: Navigate to the directory containing the above code, then run the following command to launch a Dapr sidecar and run the application: