From 1ce09dfecb5e834e7ba02466ec55d1af07fc14f7 Mon Sep 17 00:00:00 2001 From: Hannah Hunter Date: Mon, 2 Oct 2023 14:55:50 -0400 Subject: [PATCH] add java to features doc Signed-off-by: Hannah Hunter --- .../workflow/workflow-features-concepts.md | 102 +++++++++++++++++- 1 file changed, 100 insertions(+), 2 deletions(-) diff --git a/daprdocs/content/en/developing-applications/building-blocks/workflow/workflow-features-concepts.md b/daprdocs/content/en/developing-applications/building-blocks/workflow/workflow-features-concepts.md index e957ade3d56..ce39d4bac96 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/workflow/workflow-features-concepts.md +++ b/daprdocs/content/en/developing-applications/building-blocks/workflow/workflow-features-concepts.md @@ -162,7 +162,7 @@ APIs that generate random numbers, random UUIDs, or the current date are _non-de For example, instead of this: -{{< tabs ".NET" >}} +{{< tabs ".NET" Java >}} {{% codetab %}} @@ -175,11 +175,22 @@ string randomString = GetRandomString(); {{% /codetab %}} +{{% codetab %}} + +```java +// DON'T DO THIS! +Instant currentTime = Instant.now(); +UUID newIdentifier = UUID.randomUUID(); +string randomString = GetRandomString(); +``` + +{{% /codetab %}} + {{< /tabs >}} Do this: -{{< tabs ".NET" >}} +{{< tabs ".NET" Java >}} {{% codetab %}} @@ -192,6 +203,17 @@ string randomString = await context.CallActivityAsync("GetRandomString") {{% /codetab %}} +{{% codetab %}} + +```java +// Do this!! +Instant currentTime = context.getCurrentInstant(); +Guid newIdentifier = context.NewGuid(); +String randomString = context.callActivity(GetRandomString.class.getName(), String.class).await(); +``` + +{{% /codetab %}} + {{< /tabs >}} @@ -202,20 +224,58 @@ Instead, workflows should interact with external state _indirectly_ using workfl For example, instead of this: +{{< tabs ".NET" Java >}} + +{{% codetab %}} + ```csharp // DON'T DO THIS! string configuration = Environment.GetEnvironmentVariable("MY_CONFIGURATION")!; string data = await new HttpClient().GetStringAsync("https://example.com/api/data"); ``` +{{% /codetab %}} + +{{% codetab %}} + +```java +// DON'T DO THIS! +String configuration = System.getenv("MY_CONFIGURATION"); + +HttpRequest request = HttpRequest.newBuilder().uri(new URI("https://postman-echo.com/post")).GET().build(); +HttpResponse response = HttpClient.newBuilder().build().send(request, HttpResponse.BodyHandlers.ofString()); +``` + +{{% /codetab %}} + +{{< /tabs >}} Do this: +{{< tabs ".NET" Java >}} + +{{% codetab %}} + ```csharp // Do this!! string configuation = workflowInput.Configuration; // imaginary workflow input argument string data = await context.CallActivityAsync("MakeHttpCall", "https://example.com/api/data"); ``` +{{% /codetab %}} + +{{% codetab %}} + +```java +// Do this!! +String configuation = ctx.getInput(InputType.class).getConfiguration(); // imaginary workflow input argument +String data = ctx.callActivity(MakeHttpCall.class, "https://example.com/api/data", String.class).await(); +``` + +{{% /codetab %}} + +{{< /tabs >}} + + #### Workflow functions must execute only on the workflow dispatch thread. The implementation of each language SDK requires that all workflow function operations operate on the same thread (goroutine, etc.) that the function was scheduled on. Workflow functions must never: - Schedule background threads, or @@ -225,20 +285,58 @@ Failure to follow this rule could result in undefined behavior. Any background p For example, instead of this: +{{< tabs ".NET" Java >}} + +{{% codetab %}} + ```csharp // DON'T DO THIS! Task t = Task.Run(() => context.CallActivityAsync("DoSomething")); await context.CreateTimer(5000).ConfigureAwait(false); ``` +{{% /codetab %}} + +{{% codetab %}} + +```java +// DON'T DO THIS! +new Thread(() -> { + ctx.callActivity(DoSomethingActivity.class.getName()).await(); +}).start(); +ctx.createTimer(Duration.ofSeconds(5)).await(); +``` + +{{% /codetab %}} + +{{< /tabs >}} Do this: +{{< tabs ".NET" Java >}} + +{{% codetab %}} + ```csharp // Do this!! Task t = context.CallActivityAsync("DoSomething"); await context.CreateTimer(5000).ConfigureAwait(true); ``` +{{% /codetab %}} + +{{% codetab %}} + +```java +// Do this!! +ctx.callActivity(DoSomethingActivity.class.getName()).await(); +ctx.createTimer(Duration.ofSeconds(5)).await(); +``` + +{{% /codetab %}} + +{{< /tabs >}} + + ### Updating workflow code Make sure updates you make to the workflow code maintain its determinism. A couple examples of code updates that can break workflow determinism: